#!/usr/bin/perl

# workaround for dzil
package download_mandiri;
BEGIN {
  $download_mandiri::VERSION = '0.16';
}

use 5.010;
use strict;
use warnings;

use Cwd;
use Data::Rmap qw(:all);
use File::HomeDir;
use File::Path;
use File::Slurp;
use Finance::Bank::ID::Mandiri;
use YAML;

use App::Options (
    option => {
        profile   => { type => 'string', required => 0, default => 'default' },
        account   => { type => 'string', required => 0, default => "" },
        username  => { type => 'string', required => 1 },
        password  => { type => 'string', required => 1 },
        data_dir  => { type => 'string', required => 1, default => File::HomeDir->my_home . "/mandiri" },
        log_dir   => { type => 'string', required => 1, default => File::HomeDir->my_home . "/mandiri/logs" },
        days      => { type => 'string', required => 0, default => 31 },
    },
);

use vars qw($Log_Level);
BEGIN { $Log_Level = "debug" }

use Log::Any::App '$log',
    -category_level => {Dumps=>'off'},
    -file           => {
        path           => "$App::options{log_dir}/main.log",
    },
    -dir            => {
        path           => "$App::options{log_dir}/dumps",
        level          => 'off',
        # currently we always dump, Log::Any::App > 0.23 will support specifying
        # sub { ... } to refer to general level
        category_level => {Dumps => 'trace'},
    };

mkpath($App::options{data_dir})                       unless (-d $App::options{data_dir});
die "Can't create data_dir `$App::options{data_dir}'" unless (-d $App::options{data_dir});

my $ibank = Finance::Bank::ID::Mandiri->new(
    username     => $App::options{username},
    password     => $App::options{password},
    logger       => $log,
    logger_dump  => Log::Any->get_logger(category => "Dumps"),
    verify_https => 1,
);

eval {
    my $bal = $ibank->check_balance;
    $log->debug("Balance: ".$bal);

    my $stmt = $ibank->get_statement(
        account => $App::options{account},
        days => $App::options{days},
    );
    my $filename = sprintf "%s/mandiri.statement.%s.%s.to.%s.yaml",
        $App::options{data_dir},
        $stmt->{account},
        $stmt->{start_date}->ymd,
        $stmt->{end_date}->ymd;
    $log->info("Writing statements to YAML file `$filename' ...");

    # insert balance to statement (since mandiri statement doesn't contain any
    # balance information)
    $stmt->{_balance} = $bal;

    # stringify DateTime objects so dump becomes simpler and shorter, the
    # $_[0]{seen} = {} is a trick to allow multiple places which mention the same
    # object to be converted (defeat circular checking)
    rmap_ref { $_[0]{seen} = {}; $_ = $_->ymd if UNIVERSAL::isa($_, "DateTime") } $stmt;

    write_file($filename, Dump($stmt));
};

if ($@) {
    $log->error("die: $@");
}

# no matter what, try to logout so we're not locked out for 10 minutes, annoying
eval { $ibank->logout; };

$log->info("End session");



=pod

=head1 NAME

download_mandiri

=head1 VERSION

version 0.16

=head1 SYNOPSIS

First, put your username and password in ~/.app/download-mandiri.conf, e.g.:

 [ALL]
 username = ABCDEF0123
 password = 123456

Then:

 $ download-mandiri

For quieter output:

 $ QUIET=1 download-mandiri

=head1 DESCRIPTION

This is a command-line script which you can run from cron or whatever to
conveniently download Bank Mandiri statements. By default, it downloads a
month's worth of statements to ~/mandiri/. Future version will allow options.

=head1 SEE ALSO

L<Finance::Bank::ID::Mandiri>

=head1 AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Steven Haryanto.

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


__END__

