#!/usr/bin/perl

# this is a shell command the user can use to deal with his homebanking

use FindBin;
use Data::Dumper;
use lib "$FindBin::Bin/lib"; # load modules from "lib" subdir relative to this script
use Finance::Bank::IE::PermanentTSB;

sub usage {
  use Pod::Usage;
  pod2usage(-verbose =>1);
}

# main program
sub main {

    # default config file is in ~/.ptsbrc
    my $file = $ENV{HOME}."/.ptsbrc";

    # parse options

    # validate options
    
    # read config file
    my ($open24_num, $pass, $pan) = parse_configfile($file);
    # die if some of the arguments are undef
    foreach $i ($open24_num, $pass, $pan) {
        if(not defined $i) {
            die ("invalid configuration file!");
        }
    }

    my %config = (
        "open24numba" => $open24_num,
        "password" => $pass,
        "pan" => $pan,
        "debug" => 1,
    );

    # execute options

#---------------------------------------------------------------------------
#  Temporary code
#---------------------------------------------------------------------------
    my @statement = Finance::Bank::IE::PermanentTSB->account_statement(
        \%config, SWITCH_ACCOUNT, '0220','2008/08/01','2009/01/25');

    foreach my $row (@statement) {
        printf("%s | %s | %s | %s\n",
            $row->{date},
            $row->{description},
            $row->{euro_amount},
            $row->{balance});
    }

#---------------------------------------------------------------------------
#  End of temporary code
#---------------------------------------------------------------------------

    # exit
    exit 0;

}

main;

# parse config file to retrieve acc_no, password and pan
sub parse_configfile {
    my $cfgfile = shift;
    open(FILE, $cfgfile) or 
        die "Cannot open config file $cfgfile. Error was: \"$!\"\n";
    while(<FILE>) {
        if(not /^\s{0,}#/ or not /^\s{0,}/) {
            my ($key, $val) = split '=';
            $val =~ s/^\s+//;
            $val =~ s/\s+$//;
            $user = $val if($key eq 'open24_number');
            $pass = $val if($key eq 'password');
            $pan = $val if($key eq 'pan');
        }
    }
    close FILE;
    return ($user, $pass, $pan);
}

__END__

=head1 NAME

ptsb - Interact with you Permanent TSB homebanking!

=head1 SYNOPSIS

ptsb [options] 

=head2 Print account balance:

=over 2

ptsb [ -F F</path/of/conf/file> ] -b -a c -n <4digits>

=back

=head2 Print account statement:

=over 2

ptsb [ -F F</path/of/conf/file> ] -s -a c -n <4digits>
       -f dd/mm/yyyy  -t dd/mm/yyyy

=back

=head1 OPTIONS

=over 8

=item B<-h | --help>

this help usage message!

=item B<-F F</path/to/config/file> | --file F</path/to/config/file>>      

filename configuration file path (default: F<~/.ptsbrc>).
Please refer to manual page to see how this file is built.

=item B<-b | --balance>
                     
print account balance

=item B<-s | --statement>

print account statement

=item B<-f | --from--date dd/mm/yyyy>

from date

=item B<-t | --to-date dd/mm/yyyy>

to date

=item B<-a <lt>c | v<gt> | --account-type <lt>c | v<gt>>

account type: can be 'c' or 'v'

=item B<-n digits>

4 digits representing the last 4 digits of the account number or visa
card number.

=back

=head1 CONFIGURATION FILE

Configuration file default location is F<~/.ptsbrc> and it looks like this:

-------8< -- ~/.ptsbrc -->8----------

open24_number=your_open24_number

password=your_internet_password

pan=your_personal_access_number

-------8< --------------->8----------

However you can put wherever you want. Just remember to use then the
option -F F</path/to/your/location>.

Lines beginning with # will be interpreted as comment.

=head1 SEE ALSO

=item * B<Official CPAN page for Finance::Bank::IE::PermanentTSB>

L<http://search.cpan.org/~pallotron/Finance-Bank-IE-PermanentTSB/lib/Finance/Bank/IE/PermanentTSB.pm>

=item * B<Author homepage>

L<http://www.pallotron.net>

=item * B<Author Blog (italian)>

L<http://www.vitadiunsysadmin.net>

=item * B<Author homepage on CPAN>

L<http://search.cpan.org/~pallotron/>

=head1 AUTHOR

Angelo "pallotron" Failla - <pallotron@freaknet.org>

=head1 BUGS

Please report bugs to the author, no bug tracking system is set up yet.

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009 by Angelo "pallotron" Failla

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.
