#!/usr/bin/perl

############################################
##                                        ##
##                 WebCal                 ##
##           by Darryl Burgdorf           ##
##       (e-mail burgdorf@awsd.com)       ##
##                                        ##
##             version:  1.11             ##
##         last modified: 1/30/98         ##
##           copyright (c) 1998           ##
##                                        ##
##    latest version is available from    ##
##        http://awsd.com/scripts/        ##
##                                        ##
############################################

# COPYRIGHT NOTICE:
#
# Copyright 1998 Darryl C. Burgdorf.  All Rights Reserved.
#
# This program is being distributed as shareware.  It may be used and
# modified by anyone, so long as this copyright notice and the header
# above remain intact, but any usage should be registered.  (See the
# program documentation for registration information.)  By using this
# program you agree to indemnify Darryl C. Burgdorf from any liability.
#
# Selling the code for this program without prior written consent is
# expressly forbidden.  Obtain permission before redistributing this
# program over the Internet or in any other medium.  In all cases
# copyright and header must remain intact.

# VERSION HISTORY:
#
# 1.12  08/09/99  Modified for D0 by D. Casey
# 1.11  01/30/98  Corrected small problem with some versions of Perl 5
# 1.10  01/29/98  FIRST SHAREWARE RELEASE
#                 Moved configuration variables to separate file
#                 Added configurable table colors
#                 Added "Small Table" option (with or without text)
#                 Stripped "empty" entries from text listing
#                 Allowed for "Monday-Sunday" weeks
#                 Allowed for two-digit year entry (1900s)
#                 Allowed for "date range" entries
#                 Added option to allow HTML in calendar data entries
#                 Limited basic event entries to 80 characters
#                 Added optional "data dir" for more extensive entries
#                 Allowed disabling of user choice of style
#                 Fixed bug that made some entries "undeletable"
# 1.00  01/05/98  Initial "public" release

require "/www-d0/home/WWW/docs/atwork/general/d0webcal.old/d0webcal.config.pl";

# NOTHING BELOW THIS LINE NEEDS TO BE ALTERED!

$DefaultUsed = 0;

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
	($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$INPUT{$name} = $value;
}

if (($INPUT{'Year'} < 1601) || ($INPUT{'Year'} > 2899)) {
	&Header("Date Out of Range!");
	print "<H1 ALIGN=CENTER>Date Out of Range!</H1>\n";
	print "<P ALIGN=CENTER>The date you provided is out of range.";
	print "<BR>It must be between 1601 and 2899 A.D.</P>\n";
	&Footer;
	exit;
}

open (DATA,$datafile);
@data = <DATA>;
close (DATA);

foreach $line (@data) {
  if ($line =~ /\n$/) { chop ($line); }
  ($date,$textstyle,$textcolor,$textsize,$cellColor,$desc,$URL) =  split (/\|/, $line);
  ($dateyear,$datemonth,$dateday) = 
    $date =~ m#(\d\d\d\d)(\d\d)(\d\d)#o;
  
  if ((int($dateyear) == int($INPUT{'Year'}))
      || (int($dateyear) < 1)) {
    if (int($datemonth) == int($INPUT{'Month'})) {
      $BgColor_Cell{int($dateday)} = $cellColor;
      $textdesc = $desc;
      $textdesc =~ s/<([^>]|\n)*>//g;
      $Table{int($dateday)} .= 	"<font color=$textcolor><font size=$textsize><$textstyle><P>";
      $SmallTable{int($dateday)} .= "<font color=$textcolor>";
      if ($URL) {
	$Table{int($dateday)} .= "<A HREF=\"$URL\">";
	$Table{int($dateday)} .= 
	  "$desc</A></$textstyle></FONT></FONT>";
	$SmallTable{int($dateday)} .= "<BR><A ";
	$SmallTable{int($dateday)} .= "HREF=\"$URL\">";
	$SmallTable{int($dateday)} .= "$desc</A></FONT>";
	$Text{int($dateday)} .= "                    ";
	$Text{int($dateday)} .= "<A HREF=\"$URL\">";
	$Text{int($dateday)} .= "$textdesc</A>\n";
      }
      else {
	$Table{int($dateday)} .= 
	  "$desc</textstyle></FONT></FONT>";
	$SmallTable{int($dateday)} .= "<BR>$desc";
	$Text{int($dateday)} .= "                    ";
	$Text{int($dateday)} .= "$textdesc\n";
      }
    }
  }
  $SmallTable{int($dateday)} =~ s/^<BR>//;
  $Text{int($dateday)} =~ s/^                    //;
}

&PerpetualCalendar(int($INPUT{'Month'}),1,int($INPUT{'Year'}));

$xmonth = @months[int($INPUT{'Month'})-1];
$heading = "$xmonth $INPUT{'Year'}";
&Header("$heading");

if ($INPUT{'Type'} eq "Text") {
	print "<H1 ALIGN=CENTER>$heading</H1>\n";
	print "<P><PRE>\n";
	foreach $key (1..$perp_eom) {
		$weekday = ($key+$perp_dow)-(int(($key+$perp_dow)/7)*7);
		if ($weekday < 1) { $weekday = 7; }
		if ($Text{$key}) {
			if ($key < 10) { print "0"; }
			print "$key ";
			$xshortmonth = @shortmonths[$INPUT{'Month'}-1];
			print "$xshortmonth ";
			print "$INPUT{'Year'} ";
			$xshortday = @shortdays[$weekday-1];
			print "($xshortday)   ";
			print "$Text{$key}";
		}
		if (($weekday == 7) && !($key == $perp_eom)) {
			print "\n                    ---------------\n\n";
		}
	}
	print "</PRE></P>\n";
}
elsif ($INPUT{'Type'} eq "Table") {
	print "<P><CENTER><TABLE BORDER=6 CELLPADDING=6><TR>\n";
	print "<TH COLSPAN=7 ";
	print "BGCOLOR=\"#$bgcolor_header\">";
	print "<FONT COLOR=\"#$textcolor_header\">";
	print "<H1>$heading</H1></FONT></TH>\n";
	print "</TR><TR>\n";
	foreach $key (1..7) {
		print "<TH ";
		print "BGCOLOR=\"#$bgcolor_normal\">";
		print "<FONT COLOR=\"#$textcolor_normal\">";
		$xday = @days[$key-1];
		print "<BIG>$xday</BIG></FONT></TH>";
	}
	print "\n</TR><TR>\n";
	if ($perp_dow > 0) {
		print "<TD COLSPAN=$perp_dow ";
		print "BGCOLOR=\"#$bgcolor_normal\">";
		print "<P>&nbsp</TD>";
	}
	foreach $key (1..$perp_eom) {
		print "<TD VALIGN=TOP BGCOLOR=\"#";
                $color = $BgColor_Cell{int($key)};
		if ($Table{$key}) { print "$color\">"; }
		else { print "$bgcolor_normal\">"; }
		if (($INPUT{'Year'} == $year)
		  && ($INPUT{'Month'} == $month)
		  && ($key == $mday)) {
			print "<FONT COLOR=\"#$textcolor_today\">";
			print "<P><BIG><STRONG>$key</STRONG></BIG>";
		}
		else {
			print "<FONT COLOR=\"#$textcolor_normal\">";
			print "<P><BIG>$key</BIG>";
		}
		#print "<BR><SMALL>($perp_sofar/$perp_togo)</SMALL>";
		$perp_sofar++;
		$perp_togo -= 1;
		print "</FONT>";
		if ($Table{$key}) {
#
# DPC
#
#			print "<FONT COLOR=\"#$textcolor_normal\">";
			print "$Table{$key}";
#			print "</FONT>";
		}
		else { print "<P>&nbsp;"; }
		print "</TD>";
		$weekday = ($key+$perp_dow)-(int(($key+$perp_dow)/7)*7);
		if (($weekday == 0) && !($key == $perp_eom)) {
			print "\n</TR><TR>\n";
		}
	}
	if ($weekday > 0) {
		$leftover = 7-$weekday;
		print "<TD COLSPAN=$leftover ";
		print "BGCOLOR=\"#$bgcolor_normal\">";
		print "<P>&nbsp;</TD>";
	}
	print "</TR></TABLE></CENTER></P>\n";
}
else {
	if ($SmallTableText) {
		print "<P><CENTER><TABLE CELLPADDING=12><TR>";
		print "<TD VALIGN=TOP>\n";
		print "<P><CENTER><TABLE CELLPADDING=3>\n";
		foreach $key (1..$perp_eom) {
			next unless ($SmallTable{$key});
			print "<TR><TD BGCOLOR=\"#$bgcolor_normal\">";
			print "<STRONG><FONT COLOR=\"#$textcolor_normal\">";
			$xshortmonth = @shortmonths[$INPUT{'Month'}-1];
			print "$key $xshortmonth ";
			print "$INPUT{'Year'} ";
			$weekday =
			  ($key+$perp_dow)-(int(($key+$perp_dow)/7)*7);
			if ($weekday < 1) { $weekday = 7; }
			$xshortday = @shortdays[$weekday-1];
			print "($xshortday):";
			print "</FONT></STRONG></TD>";
			print "</TR><TR>";
                        $color = $BgColor_Cell{int($key)}; 
			print "<TD BGCOLOR=\"#$color\">";
			print "<FONT COLOR=\"#$textcolor_special\">";
			print "$SmallTable{$key}</FONT></TD>";
			print "</TR>";
		}
		print "</TABLE></CENTER></P>";
		print "</TD><TD VALIGN=TOP>\n";
	}
	print "<P><CENTER><TABLE BORDER=3 CELLPADDING=3><TR>\n";
	print "<TH COLSPAN=7 ";
	print "BGCOLOR=\"#$bgcolor_header\">";
	print "<FONT COLOR=\"#$textcolor_header\">";
	print "<BIG>$heading</BIG></FONT></TH>\n";
	print "</TR><TR>\n";
	foreach $key (1..7) {
		print "<TH ";
		print "BGCOLOR=\"#$bgcolor_normal\">";
		print "<FONT COLOR=\"#$textcolor_normal\">";
		$xshortday = @shortdays[$key-1];
		print "$xshortday</FONT></TH>";
	}
	print "\n</TR><TR>\n";
	if ($perp_dow > 0) {
		print "<TD COLSPAN=$perp_dow ";
		print "BGCOLOR=\"#$bgcolor_normal\">";
		print "<P>&nbsp</TD>";
	}
	foreach $key (1..$perp_eom) {
		print "<TD VALIGN=TOP ALIGN=RIGHT BGCOLOR=\"#";
		$color = $BgColor_Cell{int($key)}; 
		if ($SmallTable{$key}) { print "$color\">"; }
		else { print "$bgcolor_normal\">"; }
		if (($INPUT{'Year'} == $year)
		  && ($INPUT{'Month'} == $month)
		  && ($key == $mday)) {
			print "<FONT COLOR=\"#$textcolor_today\">";
			print "<P>$key";
		}
		else {
			print "<FONT COLOR=\"#$textcolor_normal\">";
			print "<P>$key";
		}
		print "</FONT></TD>";
		$weekday = ($key+$perp_dow)-(int(($key+$perp_dow)/7)*7);
		if (($weekday == 0) && !($key == $perp_eom)) {
			print "\n</TR><TR>\n";
		}
	}
	if ($weekday > 0) {
		$leftover = 7-$weekday;
		print "<TD COLSPAN=$leftover ";
		print "BGCOLOR=\"#$bgcolor_normal\">";
		print "<P>&nbsp;</TD>";
	}
	print "</TR></TABLE></CENTER></P>\n";
}

print "<P><CENTER><TABLE><TR>\n";

$LastYear = int($INPUT{'Year'});
$LastMonth = int($INPUT{'Month'})-1;
if ($LastMonth == 0) {
	$LastMonth = 12;
	$LastYear -= 1;
}
print "<TD><FORM METHOD=POST ACTION=$cgiurl>";
print "<INPUT TYPE=HIDDEN NAME=Month VALUE=$LastMonth>";
print "<INPUT TYPE=HIDDEN NAME=Year VALUE=$LastYear>";
print "<INPUT TYPE=HIDDEN NAME=Type VALUE=\"$INPUT{'Type'}\">";
print "<INPUT TYPE=SUBMIT VALUE=\"Previous Month\">";
print "</FORM></TD>\n";

$NextYear = int($INPUT{'Year'});
$NextMonth = int($INPUT{'Month'})+1;
if ($NextMonth == 13) {
	$NextMonth = 1;
	$NextYear += 1;
}
print "<TD><FORM METHOD=POST ACTION=$cgiurl>";
print "<INPUT TYPE=HIDDEN NAME=Month VALUE=$NextMonth>";
print "<INPUT TYPE=HIDDEN NAME=Year VALUE=$NextYear>";
print "<INPUT TYPE=HIDDEN NAME=Type VALUE=\"$INPUT{'Type'}\">";
print "<INPUT TYPE=SUBMIT VALUE=\"Next Month\">";
print "</FORM></TD>\n";

print "</TR></TABLE></CENTER></P>\n";

&Footer;
exit;

sub PerpetualCalendar {
	# This perpetual calendar routine provides accurate day/date
	# correspondences for dates from 1601 to 2899 A.D.  It is based on
	# the Gregorian calendar, so be aware that early correspondences
	# may not always be historically accurate.  The Gregorian calendar
	# was adopted by the Italian states, Portugal and Spain in 1582,
	# and by the Catholic German states in 1583.  However, it was not
	# adopted by the Protestant German states until 1699, by England
	# and its colonies until 1752, by Sweden until 1753, by Japan
	# until 1873, by China until 1912, by the Soviet Union until 1918,
	# and by Greece until 1923.
	($perp_mon,$perp_day,$perp_year) = @_;
	%day_counts =
	  (1,0,2,31,3,59,4,90,5,120,6,151,7,181,
	  8,212,9,243,10,273,11,304,12,334);
	$perp_days = (($perp_year-1601)*365)+(int(($perp_year-1601)/4));
	$perp_days += $day_counts{$perp_mon};
	$perp_days += $perp_day;
	$perp_sofar = $day_counts{$perp_mon};
	$perp_sofar += $perp_day;
	$perp_togo = 365-$perp_sofar;
	if (int(($perp_year-1600)/4) eq (($perp_year-1600)/4)) {
		$perp_togo++;
		if ($perp_mon > 2) {
			$perp_days++;
			$perp_sofar++;
			$perp_togo -= 1;
		}
	}
	foreach $key (1700,1800,1900,2100,2200,2300,2500,2600,2700) {
		if ((($perp_year == $key) && ($perp_mon > 2))
		  || ($perp_year > $key)) {
			$perp_days -= 1;
		}
	}
	$perp_dow = $perp_days - (int($perp_days/7)*7);
	if ($perp_dow == 7) { $perp_dow = 0; }
	if ($MonSunWeek) {
		$perp_dow -= 1;
		if ($perp_dow == -1) { $perp_dow = 6; }
	}
	$perp_eom = 31;
	if (($perp_mon == 4) || ($perp_mon == 6)
	  || ($perp_mon == 9) || ($perp_mon == 11)) {
		$perp_eom = 30;
	}
	if (($perp_mon == 2)) {
		$perp_eom = 28;
	}
	if ((int(($perp_year-1600)/4) eq (($perp_year-1600)/4))
	  && ($perp_mon == 2)) {
		$perp_eom = 29;
	}
	foreach $key (1700,1800,1900,2100,2200,2300,2500,2600,2700) {
		if ($perp_year == $key) {
			if ($perp_mon == 1) {
				$perp_togo -= 1;
			}
			elsif ($perp_mon == 2) {
				$perp_togo -= 1;
				$perp_eom = 28;
			}
			else {
				$perp_sofar -= 1;
			}
		}
	}
}