#!/usr/bin/perl 

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell

use strict;
use warnings;
use boolean qw(true false);

use DateTime::Format::Natural;
use Getopt::Long qw(:config no_auto_abbrev no_ignore_case);
use Term::ReadLine;

use constant LANG_DEFAULT => 'en';

my %args;
my $lang;
my @supported_languages = qw(en);
my $trace;
my %valid_languages = map { $_ => true } @supported_languages;

{
    my $opts = {};
    $opts = parse_switches() if @ARGV;
    set_values($opts);
    process();
}

sub parse_switches
{
    my %opts;
    GetOptions(\%opts, qw(f|format=s
                          h|help
                          l|lang=s
                          p|prefer_future
                          s|supported
                          t|time_zone=s
                          T|trace
                          V|version)) or usage();

    usage()     if $opts{h};
    version()   if $opts{V};
    supported() if $opts{s};

    return \%opts;
}

sub set_values
{
    my $opts = shift;

    $lang  = $opts->{l} || LANG_DEFAULT;
    $trace = $opts->{T} || false;

    my %table = (
        l => 'lang',
        f => 'format',
        p => 'prefer_future',
        t => 'time_zone',
    );

    foreach my $opt (keys %$opts) {
        if (exists $table{$opt}) {
            $args{$table{$opt}} = $opts->{$opt};
        }
    }
}

sub usage
{
    print <<USAGE;
Usage: $0 [switches]
  -f, --format           format of numeric dates
  -h, --help             this help screen
  -l, --lang             language code
  -p, --prefer_future    use futuristic dates (when possible)
  -s, --supported        list of supported languages
  -t, --time_zone        time zone string
  -T, --trace            print trace after processing
  -V, --version          print version
USAGE
    exit;
}

sub version
{
    print "  DateTime::Format::Natural $DateTime::Format::Natural::VERSION\n";
    exit;
}

sub supported
{
    print "$_\n" foreach @supported_languages;
    exit;
}

sub process
{
    unless ($valid_languages{$lang}) {
        warn "Language [$lang] isn't supported, switching to default [", LANG_DEFAULT, "]\n";
        $lang = $args{lang} = LANG_DEFAULT;
    }

    my $term = Term::ReadLine->new('dateparse');
    my $prompt = 'dateparse> ';

    while (defined(my $input = $term->readline($prompt))) {
        $term->addhistory($input) if $input =~ /\S/;
        last if $input =~ /^(?:q(?:uit)?|exit)$/;

        if ($input =~ /^(?:\?|help)$/i) {
            print <<EOT;

Commands
 ?, help                this help screen
 exit, q, quit          leave dateparse
 everything else        datetime string

EOT
            next;
        }

        my $parse = DateTime::Format::Natural->new(%args);
        my @dt = $parse->parse_datetime_duration(string => $input);

        if ($parse->success) {
            foreach my $dt (@dt) {
                printf("%02d.%02d.%4d %02d:%02d:%02d\n", $dt->day, $dt->month, $dt->year, $dt->hour, $dt->min, $dt->sec);
            }
        }
        else {
            warn $parse->error, "\n";
        }

        if ($trace) {
            print $parse->trace, "\n";
        }
    }
}

=head1 NAME

dateparse - frontend to DateTime::Format::Natural

=head1 SYNOPSIS

 Usage: dateparse [switches]
   -f, --format           format of numeric dates
   -h, --help             this help screen
   -l, --lang             language code
   -p, --prefer_future    use futuristic dates (when possible)
   -s, --supported        list of supported languages
   -t, --time_zone        time zone string
   -T, --trace            print trace after processing
   -V, --version          print version

=head1 AUTHOR

Steven Schubiger <schubiger@cpan.org>

=head1 LICENSE

This program is free software; you may redistribute it and/or
modify it under the same terms as Perl itself.

See L<http://www.perl.com/perl/misc/Artistic.html>

=cut
