#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;
use autodie;
use Getopt::Std;

# ABSTRACT: Command line interface to idonethis.com

our $VERSION = '0.07'; # VERSION: Generated by DZP::OurPkg::Version

# PODNAME: idone


use WebService::Idonethis;
use Config::Tiny;
use POSIX qw(tzset strftime);
use constant ONE_DAY => 86400;      # Seconds in a day

my %opts = (
    'l' => undef,       # List
    'y' => undef,       # yesterday
);

getopts('ly', \%opts);

unless ($opts{l} or @ARGV) {
    die "Usage: $0 [-l] [-y] [done item description]\n";
}

my $config = Config::Tiny->read( "$ENV{HOME}/.idonethisrc" );

unless ($config->{auth}{user}) {
    die "Cannot find credentials in $ENV{HOME}/.idonethisrc";
}

my $idt = WebService::Idonethis->new(
    user => $config->{auth}{user},
    pass => $config->{auth}{pass},
);

my $time = $opts{y} ? time() - ONE_DAY : time();

my $date = strftime("%Y-%m-%d", localtime($time));

if ($opts{l}) {
    my $dones = $idt->get_day($date);

    foreach my $item (@$dones) {
        say "* $item->{text}";
    }
}
else {
    $idt->set_done( text => "@ARGV", date => $date );
}

__END__

=pod

=head1 NAME

idone - Command line interface to idonethis.com

=head1 VERSION

version 0.07

=head1 SYNOPSIS

    # Submit an item to idonethis.com
    $ idone "Installed some cool software today."

    # See what I've done today.
    $ idone -l

    # In ~/.idonethisrc
    [auth]
    user=someuser
    pass=somepass

=head1 DESCRIPTION

By default, this submits items to your personal calendar on idonethis.  All
arguments are concatenated together to form a single string, which
is then submitted to your calendar for the current day (using the
local timezone on your machine).

The C<-l> switch can be used to show what you've done today (in which
case all remaining arguments are ignored).

The C<-y> switch allows operations (listing and adding) to yesteday's
done list, rather than today's (the default).

Patches are extremely welcome.  L<https://github.com/pfenwick/idonethis-perl>

=head1 AUTHOR

Paul Fenwick <pjf@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Paul Fenwick.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
