#!/usr/bin/perl
$color{F}="ff4444";
$color{R}="00aa88";
$color{C}="eeee00";
$color{E}="0000ff";
$mon{JANUARY}="01";
$mon{FEBRUARY}="02";
$mon{MARCH}="03";
$mon{APRIL}="04";
$mon{MAY}="05";
$mon{JUNE}="06";
$mon{JULY}="07";
$mon{AUGUST}="08";
$mon{SEPTEMBER}="09";
$mon{OCTOBER}="10";
$mon{NOVEMBER}="11";
$mon{DECEMBER}="12";
@mon=("", "JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPEMBER","OCTOBER","NOVEMBER","DECEMBER");

    @datetime=localtime(time);
    $monimplant='';
    $dayimplant='';
    if($datetime[4]+1 < 10)
    {
	$monimplant='0';
    }
    if($datetime[3]<10)
    {
	$dayimplant='0';
    }
    $today=join('', ($datetime[5]+1900),$monimplant,($datetime[4]+1),$dayimplant,($datetime[3]));

$thisyear=substr $today,0,4;
$thismonth=substr $today,4,2;
$thisday=substr $today,6,2;

   #get the query string
if($ENV{REQUEST_METHOD} eq 'GET')
{
    $query=$ENV{QUERY_STRING};
}
if($ENV{REQUEST_METHOD} eq 'POST')
{
    read(STDIN,$query,$ENV{CONTENT_LENGTH});
}

##### set defalt values #####
$var{year}=$thisyear;


##### decode query #########
if (defined($query))
{
    @list=split('&', $query);
    foreach (@list)
    {
	my ($key,$value) = split /=/;
	$var{$key}=$value;
	$var{$key}=~s/\+/ /g;
	$var{$key}=~ s/%(..)/chr(hex($1))/ge ;
    }
}
if(defined($var{edit}))
{
    $var{whichcalendar}=$var{edit};
}

sub dayofweek
{
#########################################################
## This program takes a year month and day and returns ##
## the day of the week                                 ##
#########################################################
    ($year,$month,$day)=@_;   # ie. 1990 or 2008
                              # ie. JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
                              # ie. 29 or 1
if($month eq "JAN" or $month eq "FEB")
{
    $year--;
}
$y=substr $year, -2;
$c=substr $year, 0,2; #### c=century
$m{MAR}=1; ## thats the way uhhu uhhu i like it!
$m{APR}=2;
$m{MAY}=3;
$m{JUN}=4;
$m{JUL}=5;
$m{AUG}=6;
$m{SEP}=7;
$m{OCT}=8;
$m{NOV}=9;
$m{DEC}=10;
$m{JAN}=11;
$m{FEB}=12;

$w=($day + int(2.6*$m{uc $month} -.2) -2*$c +$y + int($y/4) +int($c/4));
$w=$w%7;

$wday{1}="MONDAY";
$wday{2}="TEUSDAY";
$wday{3}="WEDNESDAY";
$wday{4}="THURSDAY";
$wday{5}="FRIDAY";
$wday{6}="SATURDAY";
$wday{0}="SUNDAY";

return($wday{$w});


}



############## make web page ################

print("Content-type: text/html\n\n");
    
print'<HTML><HEAD><TITLE>HiRes  CALENDAR</TITLE></HEAD>';
print '<BODY bgcolor=ffffff><TABLE bgcolor=ffaaff>';

################ Help Bar #########################
open(HELP, "<helpbar");
foreach(<HELP>)
{
    chomp;
    if(!($_=~m/calendar/)or !($_=~m/href/))
    { 
	print"$_";
    }
    elsif($_ =~m/calendar/)#make calendar link red
    {
	($first,$second)=split('>',$_);
	print"$first ><FONT color=ff0000>$second ></FONT>";
    }
}
close(HELP);
#################################   HINTS

print"<h3>Did you know? You can add a comment to <a href=calendar.pl?function=multiple>multiple calendar dates</a></h3>";


print'</FORM></TABLE>';
print'</FORM></TABLE><TABLE>';
print"\n";
if(defined($var{whichcalendar}))
{
    my $month=substr $var{whichcalendar}, -6,2;
    my $year=substr $var{whichcalendar},0,4;
    if($month>1)
    {
	$premonth=$month-1;
	$preyear=$year;
	if($premonth<10)
	{
	    $premonth="0$premonth";
	}
    }
    else
    {
	$premonth=12;
	$preyear=$year-1;
    }
    if($month <12)
    {
	$postmonth=$month+1;
	if($postmonth<10)
	{
	    $postmonth="0$postmonth";
	}
	$postyear=$year;
    }
    else
    {
	$postmonth="01";
	$postyear=$year+1;
    }########## all that crazy stuff was to makek a month++ and a month-- button
    
    print"<h3><a href=calendar.pl?whichcalendar=$preyear-$premonth.cal&function=view>previous month</a><---  --->";
    print"<a href=calendar.pl?whichcalendar=$postyear-$postmonth.cal&function=view>next month</a></h3>";
}
if(!defined($var{function}))
{
    print'<FORM method=GET ACTION="calendar.pl">';
    system"ls calendar/*.cal > junk";
    open(CAL, "<junk");
    @months=("","JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
    foreach(<CAL>)
    {
	chomp;
	$_=substr $_, 9;
	#($date, $junk)=split('.',$_);
	($y, $m)=split('-', $_);
	$m=substr $m, 0,-4;
	#$month--;
	if($y == $var{year})
	{
	    print"<INPUT type=radio name=whichcalendar value=$_>$months[$m] $y<BR>";
	}
    }
    close(CAL);
    print"<INPUT type=submit value=view name=function>\n";
    print"</FORM></TABLE><TABLE><TR><TD><FORM method=GET ACTION=calendar.pl>\n";
    $preyear=$var{year}-1;
    $postyear=$var{year}+1;
    print"The selected year is $var{year}. To change the year, see below.<BR>\n";
    print"<INPUT type=submit name=year value=$preyear><-$var{year}-><INPUT type=submit name=year value=$postyear>\n"; 


    print"</FORM></TD></TR></TABLE>";
}

if($var{function} eq "SAVE")
{
    if(defined($var{calinfo}))
    {
	$found=0;
	open(CAL, "<calendar/$var{edit}");
	open(TMP, ">junk");
	foreach(<CAL>)
	{
	    if(($_ =~ m/calday=$var{calday}/) and ($_ =~ m/ALIGN=BOTTOM/) and !$found)
	    {
		$found=1;
		
		print TMP $_;
		$end=substr $var{calinfo}, -2;
		if($end !~ "\n")  ## this adds a carriage return if none found
		{
		    $var{calinfo}="$var{calinfo}\n";
		}
		print TMP "$var{calinfo}";
	    }
	    
	    elsif(($_ =~ m/calday=$var{calday}/) and $found)
	    {
		$found=0;
		
		print TMP $_;
	    }
	    elsif(!$found)
	    {
		
		print TMP "$_";
	    }
	}
	
	close(CAL);
	close(TPM);
	system"cp junk calendar/$var{edit}";
	
    }
    $var{function}="view";
    $var{whichcalendar}=$var{edit};
}
if($var{function} eq "view") 
{
    
    
    open(CALND, "<calendar/$var{whichcalendar}");  #open the file for calnd data in html format
    foreach(<CALND>)           #and print it out
    {
	print"$_";
    }
    close(CALND);
   
}
if($var{function} eq "multiple")
{
    print"<FONT FACE=arial COLOR=ff0000>Space is limited - keep notes short, concise, and brief.<BR><BR>Avoid repeats, redundancies, repetitions, or excess, unnecessary wording. Thank you.<BR></FONT><BR>";
    print"</FORM></TABLE><TABLE>";
    print"<FORM method=GET ACTION=calendar.pl>";
    print"<h3>This function allows you to add a message to multiple dates:</h3>"; 
    print"Begin date(inclusive):<SELECT name=begmonth>\n";
    for($i=1;$i<13;$i++)
    {
	if($mon[$i] eq $mon[$thismonth])
	{
	    print"<OPTION value=$i SELECTED>$mon[$i]\n";
	}
	else
	{
	    print"<OPTION value=$i>$mon[$i]\n";
	}
    }
    print"</SELECT>";
    
    print"<INPUT type=text name=begdate size=2 maxlength=2 value=$thisday>";
    print"<INPUT type=text name=begyear size=4 maxlength=4 value=$thisyear>";
    
    print"End date(inclusive):<SELECT name=endmonth>";
    for($i=1;$i<13;$i++)
    {
	if($mon[$i] eq $mon[$thismonth])
	{
	    print"<OPTION value=$i SELECTED>$mon[$i]\n";
	}
	else
	{
	    print"<OPTION value=$i>$mon[$i]\n";
	}
    }
    print"</SELECT>";
    
    print"<INPUT type=text name=enddate size=2 maxlength=2 value=$thisday>";
    print"<INPUT type=text name=endyear size=4 maxlength=4 value=$thisyear>";
    print"<BR><TEXTAREA name=multical ROWS=15 COLS=30>type your comment here.";
    print"</TEXTAREA>";
    
    print"<INPUT type=submit name=function value=ADD> to the calendar.";
    print"<INPUT type=submit name=function value=DELETE> phrase from the calendar.";
    print"</FORM>";
}
if($var{function} eq "ADD")
{
    @calendars=split("\n",`ls calendar/*.cal`);
    foreach(@calendars)
    {
	$y=substr $_, 9,4;
	$m=substr $_, 14,2;
	if($y<=$var{endyear} and $y>=$var{begyear} and $m<=$var{endmonth} and $m>=$var{begmonth})
	{
	   
	    open(CALN, "<$_");
	    open(TMP, ">junk");
	    foreach(<CALN>)
	    {
		
		$thiscalday=substr $_,-5,3;
		
		if($_ =~ m/TOP/ and $_ !~ m/calday/)
		{
		    print"$_";
		    print TMP "$_";

		    @junk=split(">",$_);
		    $calday=substr $junk[0],-3;        #this stuff will help in a minute 
		    $DATE=$junk[3];                    #to figure out where we are in the 
		    ($DATE,$junk)=split("<", $DATE);   #calendar
		    ($dt,$dy)=split(" ", $DATE);       #
		    $day{$calday}=$dy;                 #
		    $date{$calday}=$dt;                #
		}
		elsif($_ =~ m/ALIGN=BOTTOM/ and $date{$thiscalday} ne "")
		{
		    if( ($m == $var{begmonth} and $date{$thiscalday}<$var{begdate}) or ($m == $var{endmonth} and $date{$thiscalday}>$var{enddate}) ) ### places where we dont want multical info
		    {
			print"$_";
			print TMP "$_";
		    }
		    else     #### places where we do want multical info
		    {
			print"$_";
			print"$var{multical}\n";
			print TMP "$_";
			print TMP "$var{multical}\n";
			
			(@junk,$end)=split("calday",$_);
			$calday=substr $end, 0,3;
		    }
		}
		else
		{
		   print"$_"; 
		   print TMP "$_"; 
		}
		
	    }
	    ##### need to do more stuff here
	    close(CALN);
	    close(TMP);
	    system "cp junk $_";
	}
    }

    
}
if($var{function} eq "DELETE")
{
    @calendars=split("\n",`ls calendar/*.cal`);
    foreach(@calendars)
    {
	$y=substr $_, 9,4;
	$m=substr $_, 14,2;
	if($y<=$var{endyear} and $y>=$var{begyear} and $m<=$var{endmonth} and $m>=$var{begmonth})
	{
	    $found=0;
	    open(CALN, "<$_");
	    open(TMP, ">junk");
	    foreach(<CALN>)
	    {
		
		$thiscalday=substr $_,-5,3;
		
		if($_ =~ m/ALIGN=TOP/ and $_ !~ m/calday/)
		{
		    print"$_";
		    print TMP "$_";

		    @junk=split(">",$_);
		    $calday=substr $junk[0],-3;        #this stuff will help in a minute 
		    $DATE=$junk[3];                    #to figure out where we are in the 
		    ($DATE,$junk)=split("<", $DATE);   #calendar
		    ($dt,$dy)=split(" ", $DATE);       #
		    $day{$calday}=$dy;                 #
		    $date{$calday}=$dt;                #
		}
		elsif($_ =~ m/ALIGN=BOTTOM/ and $date{$thiscalday} ne "")
		{
		    if( ($m == $var{begmonth} and $date{$thiscalday}<$var{begdate}) or ($m == $var{endmonth} and $date{$thiscalday}>$var{enddate}) ) ### places where we dont want multical info
		    {
			print"$_";
			print TMP "$_";
		    }
		    else     #### places where we do want multical info
		    {
			$found=1;
			print"$_";
			print TMP "$_";
			
			(@junk,$end)=split("calday",$_);
			$calday=substr $end, 0,3;
		    }
		}
		elsif($_=~ m/calday=/)
		{
		    print"$_";
		    print TMP "$_";
		    $found=0;
		}
		elsif($found)
		{
		    if($_ !~ m/</ and $_ !~ m/>/)
		    {
			$_ =~ s/$var{multical}//;
		    }
		    print"$_";
		    print TMP "$_"; 
		}
		else
		{
		   print"$_"; 
		   print TMP "$_"; 
		}
		
	    }
	    ##### need to do more stuff here
	    close(CALN);
	    close(TMP);
	    system "cp junk $_";
	}
    }

    
}
if($var{function} eq "edit")
{
    $found=0;
    open(CALN, "<calendar/$var{edit}");
    foreach(<CALN>)
    {
	if( ($_ !~ /INPUT/) and ($_ !~ /FORM/) )
	{
	    if($found and ($_ !~m/calday=$var{calday}/))
	    {
		print"$_";
	    }
	    elsif( ($_ =~m/calday=$var{calday}/) and !$found )
	    {
		$found=1;
		print"$_";
		print"<FORM method=GET ACTION=calendar.pl>\n";
		print"<INPUT type=hidden name=calday value=$var{calday}>\n";
		print"<INPUT type=hidden name=edit value=$var{edit}>\n";
		print"<TEXTAREA name=calinfo ROWS=8 COLS=16>";
	    }
	    elsif(($_ =~m/calday=$var{calday}/) and $found)
	    {
		$found=0;
		print"</TEXTAREA>\n";
		print"<INPUT type=submit name=function value=SAVE>";
		print"</FORM>\n";  ############# 
	    }
	    else
	    {
		
		print"$_";
	    }
	}
    }

    close(CALN);
}


if(defined($var{whichcalendar}))  #### at the end
{
    my $month=substr $var{whichcalendar}, -6,2;
    my $year=substr $var{whichcalendar},0,4;
    if($month>1)
    {
	$premonth=$month-1;
	$preyear=$year;
	if($premonth<10)
	{
	    $premonth="0$premonth";
	}
    }
    else
    {
	$premonth=12;
	$preyear=$year-1;
    }
    if($month <12)
    {
	$postmonth=$month+1;
	if($postmonth<10)
	{
	    $postmonth="0$postmonth";
	}
	$postyear=$year;
    }
    else
    {
	$postmonth="01";
	$postyear=$year+1;
    }
    
    print"<h3><a href=calendar.pl?whichcalendar=$preyear-$premonth.cal&function=view>previous calendar</a><---  --->";
    print"<a href=calendar.pl?whichcalendar=$postyear-$postmonth.cal&function=view>next calendar</a></h3>";
}


















