#!/usr/local/bin/perl -w
##
###########################################################################
##
##  Program:  analog-fiscal-year.pl
##
##  Purpose:  Generate the command line options for Analog to automatically
##            generate reports based on fiscal year date restrictions
##
##  Version:  1.1  22-Jul-2002
##
##  Author:   Peter Murray, 
##            University of Connecticut School of Law
##            pmurray@law.uconn.edu
##
##  Legalities:
##            Copyright 2002 University of Connecticut.
##            All rights reserved.
##
##  Revision History:
##    19-Jul-2002  Initial Releae
##    22-Jul-2002  Ooops.  Fixed some uninitialized value problems
##
##
## DOCUMENTATION, in PERL POD format, is at the end of the program.
## Running the program `perldoc <programname>` should output the manual.
##

use strict;
use vars qw($opt_e $opt_d);
use Getopt::Std;

getopts('e:d:');

my ($month, $year);
if ((defined $opt_d) && ($opt_d =~ m#(\d+)/(\d+)#)) {
  $month = $1;
  $year = $2;
  $year += 2000 if $year < 100;  # Make the year 4 digit if it isn't supplied as such
} else {
  ## Get the date information and 'make it real'
  ($month,$year) = (localtime(time()))[4,5];
  $month += 1;
  $year += 1900;
}

$opt_e = '' if !defined $opt_e;

## If the month is 1-6, the fiscal year label is this year, otherwise
## it is next year.  Also store variables for the beginning year and
## ending year.
my ($fiscalYear, $beginYear, $endYear);
if ($month <= 6) {
  $fiscalYear = $year; $beginYear = $year-1; $endYear = $year;
} else {
  $fiscalYear = $year+1; $beginYear = $year; $endYear = $year+1;
}

## Get the last two digits of the begining and ending years
$beginYear = substr($beginYear, -2, 2);
$endYear = substr($endYear, -2, 2);

printf("%4.4d%s +F%2.2d0701 +T%2.2d0630\n", $fiscalYear, $opt_e, $beginYear, $endYear);

exit;

__END__

=head1 NAME

analog-fiscal-year.pl - Generate command line options for fiscal year reports in Analog

=head1 SYNOPSIS

  analog -Ostats/fy`analog-fiscal-year.pl -e .html`

=head1 DESCRIPTION

This program outputs a string of date ranges so that Analog will generate a web
server report based on a fiscal year criteria.  Since our fiscal years do not
match calendar years, Analog's time-based restrictions can't do this for us.
Use this program to supply commandline parameters to analog as it runs to generate
a statistics file.  On July 1st of each year, the output automatically rolls to the
next fiscal year.

Fiscal years are assumed to start on July 1st, and the label for the fiscal year
is assumed to be the last in the range (e.g. July 1st, 2002 is in fiscal year 2003).

=head1 OPTIONS

=over 4

=item -e < suffix >

Use this option to add a suffix (such as a '.html' extension) to the end of the 
fiscal year filename.  See C<EXAMPLES>.

=item -d < mm/yyyy >

Use this option to force a particular fiscal year.  The format is month and year 
separated with a slash.  If the year is two digits, it is assumed to be in the 
21st century.

=back

=head1 EXAMPLES

To generate a report for this fiscal year, use this command line:

   analog -Ostats/fy`analog-fiscal-year.pl -e .html`
   
If today is July 19th, 2002, the program will generate this string:

   2003.html +F020701 +T030630
   
Resulting in the final analog command line of:

   analog -Ostats/fy2003.html +F020701 +T030630
   
To generate a report for a previous fiscal year, use this command line:

   analog -Ostats/fy`analog-fiscal-year.pl -e .html -d 10/2001`

=head1 SEE ALSO

analog(1)

Updates to this program can be found at http://www.pandc.org/peter/work/projects/analog-fiscal-year/

=head1 AUTHORS

Peter Murray, University of Connecticut School of Law, http://www.pandc.org/peter/work/

=cut
