#!/usr/bin/perl

# Copyright 1994 David C. Kaplowitz  All Rights Reserved
# Permission is given to freely distribute and modify this product so long
# as it is done without charge and so long as this Copyright text remains in
# place.
# Certain designs inherent may be copyrighted elsewhere.
# David C. Kaplowitz 1994
#
# This perl script does two fairly different things.  First
# if it is invoked with the environment variable PATH_INFO set
# to "display" it prints itself.  Otherwise it will return with this month
# and year submitted to the cal program.

#require "ctime.pl";
require "timelocal.pl";

&arraysetup;

#for if zone is reported by ctime.
#(($wday, $month_name, $mday, $time, $year) = split(/\s+/,&ctime(time)));
#(($wday, $month_name, $mday, $time, $zone, $year) = split(/\s+/,&ctime(time)));

#moved to localtime/timelocal
($sec, $min, $hours, $mday, $unixmonth, $unixyear, $wday, $yday, $isdst) = localtime(time);
$month=$unixmonth + 1;
if ($month < 10) {$month = "0" . $month;}
if ($mday < 10) {$mday = "0" . $mday;}
$year=$unixyear + 1900;
$rtoday=$year . $month . $mday;
$today=$rtoday;

#Check to see if it is changed
$gn_arg = $ENV{PATH_INFO};  # Get the argument from the URL
if ($ENV{QUERY_STRING} =~ /^([0-9]?[0-9]?[0-9][0-9])([0-9][0-9])([0-9][0-9])/) {
   $nyear=$1;
   $nmonth=$2;
   $nmday=$3;
   if ($nyear > 1900) {$nyear-=1900;}
   $nmonth--;
   ($sec, $min, $hours, $mday, $unixmonth, $unixyear, $wday, $yday, $isdst) = 
      localtime(timelocal($sec, $min, $hours, $nmday, $nmonth, $nyear,));
   $month=$unixmonth + 1;
   if ($month < 10) {$month = "0" . $month;}
   if ($mday < 10) {$mday = "0" . $mday;}
   $year=$unixyear + 1900;
   $today=$year . $month . $mday;
 }

#Set the week number
if ($mday < 8) {$wnum="1st";} 
 elsif ($mday < 15) {$wnum="2nd";}
 elsif ($mday < 22) {$wnum="3rd";}
 elsif ($mday < 28) {$wnum="4th";}
 else {$wnum="5th";}

$where=`/bin/pwd`;
chomp $where;
@pathname=split(/\//,$where);

if (-e "$where/BODY") {
   open (BODYFILE, "$where/BODY");
   while (<BODYFILE>) {$htmlbody = $_;}
   close(BODYFILE);
 } else {$htmlbody = "<BODY>";}

if (-e "$where/TITLE") {
   open (TITLEFILE, "$where/TITLE");
   while (<TITLEFILE>) {$title = $_;}
   close(TITLEFILE);
 } else {$title=$pathname[$#pathname];}

# Switch the following lines for static, as opposed to dynamic pages
#open (INDEXFILE, "> index.html") || die "Can't open index.html: $!\n";

*INDEXFILE=*STDOUT;
print INDEXFILE "Content-type: text/html\n\n";

print INDEXFILE <<EOH;
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<HTML><HEAD>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META name="ROBOTS" content="NOINDEX, NOFOLLOW">
<META http-equiv="Content-Language" content="en-us">
<META name="author" content="Auto-Generated">
<TITLE>$title</TITLE>
</HEAD>
EOH

print INDEXFILE "$htmlbody\n";
if ($title) {print INDEXFILE "<H1>$title</H1>\n";}

# read in and get rid of the files that we don't normally serve:
opendir(HERE, $where) || warn "Can't open $where: $!\n";
@files = grep(!/^\./, readdir HERE);
closedir(HERE);
@files = grep(!/~$/, @files);
@files = grep(!/#$/, @files);
@files = grep(!/^RCS/, @files);
@files = grep(!/^CVS/, @files);
@files = grep(!/^index/i, @files);
@files = grep(!/^HEADER/, @files);
@files = grep(!/^README/, @files);
@files = grep(!/^BODY/, @files);
@files = grep(!/^TITLE/, @files);
@files = grep(!/^core/, @files);

if (-e "$where/HEADER.html") {
   open (HEADER, "$where/HEADER.html");
   while (<HEADER>) {print INDEXFILE;}
   close(HEADER);
 }

if (-e "$where/HEADER.shtml") {
   open (HEADER, "$where/HEADER.shtml");
   while (<HEADER>) {
      if ($_ =~ /^<!--#include file="(.*)" -->/) {
         open (INCLUDE, "$where/$1");
         while (<INCLUDE>) {print INDEXFILE ;}
         close(INCLUDE);
       } elsif ($_ =~ /^<!--#include virtual="(.*)" -->/) {
         open (INCLUDE, "$where/$1");
         while (<INCLUDE>) {print INDEXFILE ;}
         close(INCLUDE);
       } else {print INDEXFILE ;}
    }
   close(HEADER);
 }

#seperate by type
foreach $file (@files) {
   if ($file =~ /^(\d\d\d\d\d\d\d\d)/) {
      if ($1 < $today) {push(@past,$file);} else {push(@future,$file);}
      if ($file =~ /^$today/) {push(@today,$file);}
    } elsif ($file =~ /^X?X?XXXXXX-([^_]*)/) {
      if ($1 =~ /^$wnum-$wname[$wday]/) {push(@today,$file);}
      if ($1 =~ /^$wname[$wday]/) {push(@today,$file);}
      push(@regular,$file);
    } elsif ($file =~ /^X?X?XX[X\d][X\d]\d\d_/) {
      push(@regular,$file);
    } elsif ($file =~ /\d\d\d\d_Events$/) {
      push(@oldyear,$file);
    } else {push(@general,$file);}
 }

print INDEXFILE "<hr>\nTo set this page for a particular day, enter it below in the form of YYYYMMDD. <ISINDEX>\n";
print INDEXFILE "<br>";
if (defined @today) {print INDEXFILE " <A HREF=#today>Today's Unique Events</A>";}
if (defined @general) {print INDEXFILE " <A HREF=#general>General Information</A>";}
if (defined @regular) {print INDEXFILE " <A HREF=#regular>Regular Events</A>";}
if (defined @future) {print INDEXFILE " <A HREF=#future>Upcoming Events</A>";}
if (defined @past) {print INDEXFILE " <A HREF=#past>Past Events</A>";}
if (defined @oldyear) {print INDEXFILE " <A HREF=#YearsPast>Years ago Event calendars</A>";}
print INDEXFILE "<br>\n";

if (defined @today) {
   if ($rtoday == $today) {
      print INDEXFILE "<hr><H2><A NAME=today> Today's Scheduled Events:</A></H2>\n";
    } else {
      print INDEXFILE "<hr><H2><A NAME=today> Events Scheduled for $today:</A></H2>\n";
    }
   &s_and_p("forward",@today);
 }

if (defined @general) {
   print INDEXFILE "<hr><H2><A NAME=general> General Information:</A></H2>\n";
   &s_and_p("forward",@general);
 }

if (defined @regular) {
   print INDEXFILE "<hr><H2><A NAME=regular> Regular Events:";
   if ($rtoday == $today) {
      print INDEXFILE " <I>(Hint: Today is the $wnum $wname[$wday])</I></A></H2>\n";
    } else {
      print INDEXFILE "</A></H2>\n";
    }
   &s_and_p("forward",@regular);
 }

if (defined @future) {
   if ($rtoday == $today) {
      print INDEXFILE "<hr><H2><A NAME=future> Upcoming Events: <I>(Hint: Today is $today)</I></A></H2>\n";
    } else {
      print INDEXFILE "<hr><H2><A NAME=future> Events after: $today</A></H2>\n";
    }
   &s_and_p("forward",@future);
 }

if (defined @past) {
   if ($rtoday == $today) {
      print INDEXFILE "<hr><H2><A NAME=past> Past Events: <I>(Hint: Today is $today)</I></A></H2>\n";
    } else {
      print INDEXFILE "<hr><H2><A NAME=past> Events before: $today</A></H2>\n";
    }
   &s_and_p("reverse",@past);
 }

if (defined @oldyear) {
   print INDEXFILE "<hr><H2><A NAME=YearsPast> Years ago Event calendars:</A></H2>\n";
   &s_and_p("reverse",@oldyear);
 }

if (-e "$where/README.html") {
   print INDEXFILE "<hr>\n";
   open (README, "$where/README.html");
   while (<README>) {print INDEXFILE ;}
   close(README);
 }

if (-e "$where/README.shtml") {
   print INDEXFILE "<hr>\n";
   open (README, "$where/README.shtml");
   while (<README>) {
      if ($_ =~ /^<!--#include file="(.*)" -->/) {
         open (INCLUDE, "$where/$1");
         while (<INCLUDE>) {print INDEXFILE ;}
         close(INCLUDE);
       } else {print INDEXFILE ;}
    }
   close(README);
 }

print INDEXFILE "\n</BODY></HTML>\n";

sub s_and_p {
   my($order,@filelist)=@_;
   my($file);

   print INDEXFILE "\n<UL>\n";
   if ($order eq "reverse") {
      foreach $file (sort {$b cmp $a;} @filelist) {
         print INDEXFILE "<LI><A HREF=$file>$file</A>\n";
       }
    } else {
      foreach $file (sort @filelist) {
         print INDEXFILE "<LI><A HREF=$file>$file</A>\n";
       }
    }
   print INDEXFILE "</UL>\n";
}

sub s_and_p_full {
   my($order,@filelist)=@_;
   my($file);

   print INDEXFILE "\n<UL>\n";
   if ($order eq "reverse") {
      foreach $file (sort {$b cmp $a;} @filelist) {
         print INDEXFILE "<LI><A HREF=$file>$file</A>\n";
       }
    } else {
      foreach $file (sort @filelist) {
         print INDEXFILE "<LI><A HREF=$file>$file</A>\n";
         if ($file !~ /html$/) {print INDEXFILE "<PRE>\n";}
         open(LISTFULL, "$where/$file") || warn "Can't open $where/$file: $!\n";
         while(<LISTFULL>){print INDEXFILE $_;}
         close(LISTFULL);
         if ($file !~ /html$/) {print INDEXFILE "</PRE>\n";}
       }
    }
   print INDEXFILE "</UL>\n";
}

sub arraysetup {
   my($i);

   $mo[0] ="Jan";
   $mo[1] ="Feb";
   $mo[2] ="Mar";
   $mo[3] ="Apr";
   $mo[4] ="May";
   $mo[5] ="Jun";
   $mo[6] ="Jul";
   $mo[7] ="Aug";
   $mo[8] ="Sep";
   $mo[9] ="Oct";
   $mo[10] ="Nov";
   $mo[11] ="Dec";

   $wname[0] ="Sunday";
   $wname[1] ="Monday";
   $wname[2] ="Tuesday";
   $wname[3] ="Wednesday";
   $wname[4] ="Thursday";
   $wname[5] ="Friday";
   $wname[6] ="Saturday";

   for ($i=0;$i<=$#mo;$i++) {
      $om{$mo[$i]} = $i;
      if ($om{$mo[$i]} < 10) {
         $om{$mo[$i]}="0" . $om{$mo[$i]};
       }
    }
 }
