#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
use strict;
use warnings;
use utf8;
use encoding ':locale';

use WWW::OrangeHRM::Client;
use Term::ReadKey;
use File::Spec;
use Config::Tiny;
use Getopt::Long;
use Pod::Usage;

=encoding utf-8

=head1 NAME

orangehrm - Command line client for OrangeHRM

=head1 SYNOPSYS

orangehrm [OPTION...]

=head1 DESCRIPTION

Set or show time sheet in OrangeHRM system.

=head1 OPTIONS

Without any options, it will show time sheet for current month.

=head2 Options for configuration

These options can be retrieved from configuration file.

=over 8

=item B<--url URL>

Base URL where OrangeHRM instance is running.

=item B<--username USER>

User name to log in as.

=item B<--password PASSWORD>

Password to use. If not specified, it will be asked for.

=back

=head2 Options for filling a time sheet

=over 8

=item B<--day N>

Day of month, counts from 1, default is today.

=item B<--from HH::MM>

Begining of working time.

=item B<--to HH::MM>

End of working time.

=item B<--break HH:MM>

Vacant part in the working time for purpose of working break.

=item B<--doctor HH:MM>

Vacant part in the working time for purpose of medical check.

=item B<--comment STRING>

Free comment to the day.

=item B<--trip>

Specify if you are on bussines trip on this day.

=back

=head2 Options for debugging

=over 8

=item B<--debug>

Dump sent HTTP requests and other useful data.

=back

=head1 FILES

=head2 F<~/.orangehrm>

User configuration:

    url = https://redhat.orangehrm.com/
    username = ppisar@redhat.com
    #password = Foo

=cut

my $config_file = File::Spec->catfile($ENV{HOME}//'/', '.orangehrm');

my %configuration = (
    url => 'https://redhat.orangehrm.com/',
    username => undef,
    password => undef
);


# Main code
# Default arguments
my ($help, $debug, $day, $from, $to, $break, $doctor, $comment, $trip);
($day) = (localtime)[3];

# Load configuration
if (-f $config_file) {
    my $cfg = Config::Tiny->new->read($config_file);
    if (!defined $cfg) {
        print STDERR 'Could not parse `', $config_file,
            ' configuration file: ', $Config::Tiny::errstr, "\n";
        exit 1;
    }
    for (keys %{$cfg->{_}}) {
        $configuration{$_} = $cfg->{_}->{$_};
    }
}

GetOptions( 
    'url=s' => \$configuration{url},
    'username=s' => \$configuration{username},
    'password=s' => \$configuration{password},
    'day=i' => \$day,
    'from=s' => \$from,
    'to=s' => \$to,
    'break=s' => \$break,
    'doctor=s' => \$doctor,
    'comment=s' => \$comment,
    'trip' => \$trip,
    'help' => \$help,
    'debug' => \$debug
) or pod2usage(-exitstatus => 1, -verbose => 1);
if ($help) {
    pod2usage(-exitstatus => 0, -verbose => 2);
}
if (!defined $configuration{url} || !defined $configuration{username} ||
        (!defined $from xor !defined $to)) {
    pod2usage(-exitstatus => 1, -verbose => 1,
        -mesage => "Missing options!\n");
}

if (!defined $configuration{password}) {
    print 'Password for ', $configuration{username}, ' at <',
        $configuration{url},'>: ';
    ReadMode('noecho');
    my $value = ReadLine(0);
    ReadMode('restore');
    print "\n";
    chomp $value if (defined $value);
    $configuration{password} = $value;
}


my $automaton = WWW::Mechanize->new();
if ($debug) {
    WWW::OrangeHRM::Client::debug_http($automaton);
}

# Log-in
if (!WWW::OrangeHRM::Client::log_in($automaton, \%configuration)) {
    WWW::OrangeHRM::Client::fatal_error($automaton, $debug,
        'Could not log in!');
}
print "Logged in.\n";

if (!WWW::OrangeHRM::Client::time_sheet($automaton)) {
    WWW::OrangeHRM::Client::fatal_error($automaton, $debug,
        'Could not get time sheet!');
}

if (defined $from) {
    if (!WWW::OrangeHRM::Client::time_sheet_set($automaton, $day, $from, $to,
            $break, $doctor, $comment, $trip)) {
        WWW::OrangeHRM::Client::fatal_error($automaton, $debug,
            "Could not fill day #$day into time sheet!");
    }
} else {
    WWW::OrangeHRM::Client::time_sheet_show($automaton);
}

# Log-out
if (!WWW::OrangeHRM::Client::log_out($automaton)) {
    WWW::OrangeHRM::Client::fatal_error($automaton, $debug,
        'Could not log out!');
}
print "Logged out.\n";

exit 0;

__END__

=head1 COPYRIGHT

Copyright © 2012  Petr Písař <ppisar@redhat.com>.

=head1 LICENSE

This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

=cut
