#!/usr/local/bin/perl
# Calls the system 'cal' function to display a calendar: ISINDEX
# File: calendar2.pl

$CAL='/usr/bin/cal';

# Print the header
print "Content-type: text/html\n\n";

# Print the top of the document, including an <ISINDEX> tag
print <<END;
<HTML><HEAD><TITLE>Calendar</TITLE></HEAD>
<BODY>
<H1>Calendar</H1>
Enter the years you want to display calendars for, separating
each year by a space.  For example "1992 1993".
<ISINDEX>
END

# Try to fetch the list of years from the
# query string.  This is a keyword-list style
# script, so we can find it in @ARGV.  For
# sanity-checking, make sure that everything
# in @ARGV looks like a year and ignore everything else.
foreach (@ARGV) {
    next unless /(\d{4})/;	# pattern match for 4 digits
    push(@years,$1);		# push the year onto an array
}

# If @years is empty, then either the query string was
# empty or it contained nothing that looked like a year. 
# Fetch the current year using the Unix date command
unless (@years) {
    chop($year=`date +%Y`);
    push(@years,$year);
}

# Loop over the years.
foreach $year (@years) {
    chop($calendar_text=`$CAL $year`);
    print <<END;
<H2>Calendar for Year $year</H2>
<PRE>
$calendar_text
</PRE>
END
    ;
}

# End it now
print <<END;
<HR>
<A HREF="/">Welcome Page</A>
</BODY></HTML>
END
