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: Send reminders of what you were doing a while ago.

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

# PODNAME: idonethis-memories


use Getopt::Std;
use Pod::Usage;
use WebService::Idonethis;
use Config::Tiny;
use POSIX qw(tzset strftime);

use constant ONE_WEEK   => 7   * 86400;
use constant ONE_MONTH  => 30  * 86400;     # It's about a month...
use constant SIX_MONTHS => 365 * 86400 / 2; # About six months...
use constant ONE_YEAR   => 365 * 86400;     # TODO: Leapyears

binmode STDOUT, ':encoding(UTF-8)';    # Printing unicode is fine, Perl...

$Getopt::Std::STANDARD_HELP_VERSION = 1;
sub HELP_MESSAGE { pod2usage(1); }

my %opts = (
    'f' => "$ENV{HOME}/.idonethisrc",
);

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

$ENV{TZ} = "Australia/Melbourne";      # I'm usually here.
tzset();

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

send_memories($idt);

sub send_memories {
    my ($idt) = @_;

    my $now = time;

    foreach my $timeframe (ONE_YEAR, SIX_MONTHS, ONE_MONTH, ONE_WEEK) {

        my $then = $now - $timeframe;

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

        my $dones = $idt->get_day($date);

        if ($dones->[0]) {

            say "Here's what you were doing on $date:\n";

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

            return;
        }
    }

    say "I have no idea what you were doing. (Probably a bug.)";

}

__END__

=pod

=head1 NAME

idonethis-memories - Send reminders of what you were doing a while ago.

=head1 VERSION

version 0.19

=head1 SYNOPSIS

    # On the cmdline, or via cron
    $ idonethis-memories

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

=head1 DESCRIPTION

This is a simple proof-of-concept for iDoneThis's excellent (but
now defunct) memory service, which would send reminders as to what
one was doing a year ago by email.

The C<-f> switch can be used to specify a location for the configuration
file (by default F<~/.idonethisrc>).

Patches are extremely welcome.  L<https://github.com/pjf/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