#!/usr/bin/env perl

use 5.008;

use strict;
use warnings;

use Astro::Coord::ECI::Utils qw{ SECSPERDAY };
use Getopt::Long 2.33 qw{ :config auto_version };
use Pod::Usage;
use POSIX qw{ strftime };

our $VERSION = '0.006';

my %opt = (
    days	=> 30,
    format	=> '%d-%b-%Y %H:%M:%S %Z',
);

GetOptions( \%opt,
    qw{ days=i format=s gmt! start=s },
    help => sub { pod2usage( { -verbose => 2 } ) },
) or pod2usage( { -verbose => 0 } );

my @planets = @ARGV ? @ARGV : qw{
    Sun Mercury Venus Mars Jupiter Saturn Uranus Neptune
};

my $start;
if ( $opt{start} ) {
    require Date::Manip;
    $start = Date::Manip::UnixDate( '%s', $opt{start} )
	or die "Invalid start date $opt{start}\n";
} else {
    $start = time;
}
my $finish = $start + SECSPERDAY * $opt{days};

my @events;

foreach my $body ( @planets ) {
    eval {
	$body =~ m/ \A _ /smx
	    and die "Invalid planet name '$body'\n";
	$body = ucfirst lc $body;
	my $class = join '::', qw{ Astro Coord ECI VSOP87D }, $body;
	( my $fn = "$class.pm" ) =~ s< :: ></>smxg;
	require $fn;
	my $obj = $class->new()->universal( $start );
	while ( 1 ) {
	    my ( $time, $quarter, $desc ) = $obj->next_quarter();
	    $time > $finish
		and last;
	    push @events, [ $time, $desc ];
	}
	1;
    } or warn $@;
}

foreach (
    sort { $a->[0] <=> $b->[0] } @events
) {
    local $\ = "\n";
    $opt{gmt}
	and local $ENV{TZ} = 'GMT';
    print strftime( $opt{format}, localtime $_->[0] ), '  ', $_->[1];
}

__END__

=head1 TITLE

quarters - Show quarters for the next month

=head1 SYNOPSIS

 quarters
 quarters venus mars
 quarters -help
 quarters -version

=head1 OPTIONS

=head2 -days

 -days 60

This option specifies the number of days to cover.

The default is C<-days 30>.

=head2 -format

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

This option specifies the C<strftime (3)> format used to display the
time.

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

=head2 -gmt

If asserted, this Boolean option attempts to display the time in GMT.
All that it really does is

 local $ENV{TZ} = 'GMT';

which may or may not have the desired effect.

The default is C<-nogmt>.

=head2 -help

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

=head2 -start

 -start tomorrow

This option specifies when the computation starts, as a date and time
understood by L<Date::Manip|Date::Manip>.

The default is the current date and time.

=head2 -version

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

=head1 DETAILS

This Perl script calculates the quarters for the bodies specified on the
command line, or for all bodies if none are specified. The command line
is not case-sensitive, but must specify bodies supported by
L<Astro::Coord::ECI::VSOP87D|Astro::Coord::ECI::VSOP87D>.

The times displayed are local unless C<-gmt> is asserted.

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2018-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 :
