The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;
use autodie;

# ABSTRACT: Command line interface to idonethis.com

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

# PODNAME: idone


use Getopt::Std;
use Pod::Usage;
use WebService::Idonethis;
use Config::Tiny;
use POSIX qw(tzset strftime);
use constant ONE_DAY => 86400;      # Seconds in a day
binmode STDOUT,':encoding(UTF-8)';  # Printing unicode is fine, Perl.

# Make Getopt less wrong.
$Getopt::Std::STANDARD_HELP_VERSION = 1;
sub VERSION_MESSAGE { pod2usage(1); }

my %opts = (
    'c' => undef,                            # STDIN
    'd' => strftime("%Y-%m-%d",localtime()), # Date
    'f' => "$ENV{HOME}/.idonethisrc",        # Config
    'h' => undef,                            # Help
    'l' => undef,                            # List
    'y' => undef,                            # Yesterday
);

getopts('cd:f:hly', \%opts);

if ($opts{h}) {
    pod2usage(1);
}

my $config = Config::Tiny->read( $opts{f} );

unless ($config->{auth}{user}) {
    die "Cannot find credentials in $opts{f}\n";
}

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

my $date = $opts{d};

# Handle yesterday if '-y' switch is turned on.
if ($opts{y}) {
    $date = strftime("%Y-%m-%d", localtime(time() - ONE_DAY));
}

# List items, if required.

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

    say "\nDone tasks for $date:\n";

    foreach my $item (@$dones) {
        say "* $item->{text}";
    }
}

if ($opts{c} or not $opts{l}) {
    if (@ARGV) {
        $idt->set_done( text => "@ARGV", date => $date );
    }
    else {
        say "\nReading done items for $date (one per line).\n";
        while (<STDIN>) {
            chomp;
            $idt->set_done( text => $_, date => $date );
        }
    }
}

__END__

=pod

=head1 NAME

idone - Command line interface to idonethis.com

=head1 VERSION

version 0.12

=head1 SYNOPSIS

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

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

    # See what I've done yesterday.
    $ idone -ly

    # See what I did some time ago.
    $ idone -ld 2012-01-01
    
    # See what I've done today, and read new items from STDIN.
    idone -lc

    # See what I've done yesterday, and add more items from STDIN.
    idone -lcy

    # 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).

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

=head1 OPTIONS

=over

=item B<-c>

Read items from STDIN and add them to your done list, with one item per
line. This is the default behaviour if C<-l> is not specified, nor any
done item on the command line.

=item B<-d date>

Specify the date upon which actions should be taken, in YYYY-MM-DD format.
Defaults to today. This switch is silently ignored if used with C<-y>.

=item B<-f config>

Specify the location of the configuration file.  Defaults to ~/.idonethisrc

=item B<-h>

Displays this help.

=item B<-l>

List items that you've done.  Combine this with the C<-c> switch
to list done items, and let you add new ones to the end.

=item B<-y>

Perform all operations (adding and listing) using yesterday's date.
This switch overrides the C<-d> switch, if given.

=back

=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