#!/usr/bin/env perl

use 5.010;

use strict;
use warnings;

use open qw{ :std :encoding(utf-8) };

use Date::ManipX::Almanac::Date;
use Encode qw{ decode };
use File::Basename qw{ basename };
use FindBin qw{ $Bin };
use Getopt::Long 2.33 qw{ :config auto_version };
use Pod::Usage;

our $VERSION = '0.001';

my %opt = (
    almanacconfigfile	=> "$Bin/@{[ basename( $0 ) ]}.cfg",
    format		=> '%d-%b-%Y %H:%M:%S',
);

@ARGV = map { decode( 'utf-8', $_ ) } @ARGV;

GetOptions( \%opt,
    qw{
	almanacconfigfile|almanac-config-file|configfile|config-file=s
	format=s gmt! language=s twilight=s value!
    },
    help => sub { pod2usage( { -verbose => 2 } ) },
) and @ARGV or pod2usage( { -verbose => 0 } );

-e $opt{almanacconfigfile}
    or die "Almanac config file $opt{almanacconfigfile} not found\n";

my $dmad = Date::ManipX::Almanac::Date->new();
# NOTE that the configuration file needs to be handled after (at least)
# language, because setting the language clobbers any holiday
# descriptions that may have been loaded.
foreach my $name ( qw{ language twilight almanacconfigfile } ) {
    defined $opt{$name}
	or next;
    $dmad->config( $name => $opt{$name} )
	and die $dmad->err();
}

if ( my $sta = $dmad->get_config( 'location' ) ) {
    if ( my $name = $sta->get( 'name' ) ) {
	say $name;
    }
} else {
    die "ERROR: no location specified in config file.\n";
}


my $errs = 0;

foreach my $date ( @ARGV ) {
    if ( $dmad->parse( $date ) ) {
	warn $dmad->err(), " '$date'\n";
	$errs++;
    } else {
	$opt{gmt}
	    and $dmad->convert( 'GMT' );
	say "$date is ", $opt{value} ?
	    scalar $dmad->value() :
	    $dmad->printf( $opt{format} );
    }
}
exit $errs;

__END__

=head1 TITLE

dmd-almanac - Parse dates using Date::ManipX::Almanac::Date

=head1 SYNOPSIS

 dmd-almanac 'the rising of the Sun'
 dmd-almanac --help
 dmd-almanac --version

=head1 OPTIONS

=head2 --almanac-config-file

 --almanac-config-file .dmd-almanacrc

This option specifies the configuration file to read.

The default is F<dmd-almanac.cfg> in the same directory as this
script.

=head2 --config-file

This is a synonym for L<--almanac-config-file|/--almanac-config-file>.

=head2 --format

 --format '%Y-%m-%dT%H:%M:%S'

This option specifies the output format for the time.

The default is C<%d-%b-%Y %H:%M:%S>.

=head2 --gmt

If this Boolean option is asserted, the date is displayed in GMT;
otherwise it is displayed in the local zone.

The default is C<--no-gmt>.

=head2 --help

This option displays the documentation for this script. The script then
exits.

=head2 --language

 --language english

This option specifies the language. It overrides the language specified
in the configuration file, if any.

The default (in the absence of a language setting in the configuration
file) is L<Date::Manip|Date::Manip>'s default, i.e. C<English>.

=head2 --twilight

 --twilight 9
 --twilight nautical

This option specifies the definition of twilight in degrees or one of
the accepted strings. It overrides that specified in the configuration
file, if any.

The default (in the absence of a language setting in the configuration
file) is L<Astro::Coord::ECI|Astro::Coord::ECI>'s default, i.e. civil
twilight.

=head2 --value

If this Boolean option is asserted, the results of the C<value()> method
are displayed; otherwise the results of the C<printf()> method are
displayed, using the specified (or defaulted) format.

=head2 --version

This option displays the version of this script. The script then exits.

=head1 DETAILS

This Perl script feeds its command-line arguments, one at a time, to the
L<Date::ManipX::Almanac::Date|Date::ManipX::Almanac::Date>
L<parse()|Date::ManipX::Almanac::Date/parse> method, and prints the
parsed time. If the parse fails, the error is displayed on F<STDERR>.

The command-line arguments are assumed to be encoded in C<UTF-8>.

The exit status is the number of parse failures.

=head1 AUTHOR

Thomas R. Wyant, III F<wyant at cpan dot org>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2021 by Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5.10.0. For more details, see the full text
of the licenses in the directory LICENSES.

This program is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.

=cut

# ex: set textwidth=72 :
