#!/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.08'; # 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 = (
    'c' => undef,       # STDIN
    'l' => undef,       # List
    'y' => undef,       # yesterday
);

getopts('cly', \%opts);

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));

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

=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 today, and read new items from STDIN.
    idone -lc

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

Without the C<-l> swich, or with the C<-c> switch, will read done items from
STDIN (one per line).

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
