#!/usr/local/bin/perl 
#
# 13 July 1995
# 	Modified to create a calendar
#
# 30 Aug 1996
#   Modified by Justin Legakis for purposes of 6.838


    # Print this out no matter what
    #print "Content-type: text/html\n\n";

    if ($ENV{'REQUEST_METHOD'} eq 'POST')
    {
      # How many bytes are we supposed to receive?
      read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

      @field = split(/&/, $buffer);

      foreach $next_field (@field)
      {
          ($name, $value) = split(/=/, $next_field);
          $value =~ tr/+/ /;
          $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
          $value =~ s/~!/ ~!/g;   # Stops people using subshells apparently

          $USER_INPUT{$name} = $value;

		  print "name: ".$name.", value: ".$value
      }
    }


# The following three lines are the parameters to this script.  They
# really should be read in from the command line, but for now, they
# are not.  Change these to create the calendar for each month, or to
# change the input filename.

	$USER_INPUT{Month} =  4;
	$USER_INPUT{Year} = 1998;
	$USER_INPUT{Inputfile} = "cal_input.txt";

	$USER_INPUT{FirstMonth} = 2;
	$USER_INPUT{LastMonth} = 5;

##### My Code Starts Here.  Written by Scott S. McCosh #####

##### I modified it to automagicaly include TODAY on the current date and 
##### allow one to include other files with a #include <URL> command.
##### Mike jones@lal.cs.byu.edu 2/13/96

##### (TODAY code removed by jsl)

%months = (
   'jan', '1',
   'feb', '2',
   'mar', '3',
   'apr', '4', 
   'may', '5',
   'jun', '6', 
   'jul', '7',
   'aug', '8',
   'sep', '9',
   'oct', '10',
   'nov', '11',
   'dec', '12'
	   );
%monthback = (
    '1', 'jan',
    '2', 'feb',
    '3', 'mar',
    '4', 'apr',
    '5', 'may',
    '6', 'jun',
    '7', 'jul',
    '8', 'aug',
    '9', 'sep',
    '10','oct',
    '11','nov',
    '12','dec'
	   );

if($USER_INPUT{Year})
{ 
   $month = $USER_INPUT{Month};
   $year =  $USER_INPUT{Year};
}
else
{   
    $_ = `date +%m%Y`;
    /0?(1?[0-9])([1-2][0-9]{3})/;
    if($USER_INPUT{Month})
    {  $month = $USER_INPUT{Month};  }
    else
    {  $month = $1;  }
    $year = $2;  
}

if($ARGV[0])
{
print "The input was: ".$ARGV[0]."<br>";
  @inputline = split(/,/, $ARGV[0]);
print "The input file was: ".$inputline[0]."<br>";
  if($inputline[1])
  { $inputline[1] =~ /\s*([A-Za-z]{3}).*/;
    $monthName = $1;
    $monthName =~ y/A-Z/a-z/;
    $month = $months{$monthName};
  } 
  
  if($inputline[2])
  { $year = $inputline[2]; }

  `/net/leopard/lal2/web/www-tools/libwww-perl-0.40/get -e $inputline[0] > /net/leopard/lal2/web/www-tools/libwww-perl-0.40/events.txt`;
  `echo '\n' >> /net/leopard/lal2/web/www-tools/libwww-perl-0.40/events.txt`;
  `date +'%e %b %Y| <b>Today</b>' >> /net/leopard/lal2/web/www-tools/libwww-perl-0.40/events.txt`;
  open (INPUT, "/net/leopard/lal2/web/www-tools/libwww-perl-0.40/events.txt") || print "Couldn't open input file\n";
}
else
{
  if($USER_INPUT{Inputfile})
  {
#    `/net/leopard/lal2/web/www-tools/libwww-perl-0.40/get -e $USER_INPUT{Inputfile} > /net/leopard/lal2/web/www-tools/libwww-perl-0.40/events.txt`;
#    `echo '\n' >> /net/leopard/lal2/web/www-tools/libwww-perl-0.40/events.txt`;
#    `date +'%e %b %Y| <b>Today</b>' >> /net/leopard/lal2/web/www-tools/libwww-perl-0.40/events.txt`;
#    open (INPUT, "/net/leopard/lal2/web/www-tools/libwww-perl-0.40/events.txt") || print "Couldn't open input file\n";
  
    open (INPUT, $USER_INPUT{Inputfile}) || print "Couldn't open input file\n";

    $inputline[0] = $USER_INPUT{Inputfile};
  }
  else
  { $inputline[0] = "/people/mccosh/empty.txt"; 
#The above line may need to be modified in the future
    `echo '\n' > /net/leopard/lal2/web/www-tools/libwww-perl-0.40/events.txt`;
    `date +'%e %b %Y| <b>Today</b>' >> /net/leopard/lal2/web/www-tools/libwww-perl-0.40/events.txt`;
    open (INPUT, "/net/leopard/lal2/web/www-tools/libwww-perl-0.40/events.txt") || print "Couldn't open input file\n";
  }

}

%days = (
   'sun', '1',
   'mon', '2',
   'tue', '3',
   'wed', '4',
   'thu', '5',
   'fri', '6',
   'sat', '7'
        );
while (<INPUT>)
{
   
    # this matches a #include <URL> command that will include the given URL as another calender file.  MDJ 2/13/96  
    if (/^#include\s*(.*)/i)
    {
	# print "oh my, time to include something<p>";
	$include_file = $1;
	# print $include_file;
#        `echo '\n' >> /net/leopard/lal2/web/www-tools/libwww-perl-0.40/events.txt`;
	`/net/leopard/lal2/web/www-tools/libwww-perl-0.40/get -e $include_file >> /net/leopard/lal2/web/www-tools/libwww-perl-0.40/events.txt`;
    }
    else
    { 
	if(/^\s*([123]?[0-9])\s*([A-Za-z]{3})?[A-Za-z]*\s*([12][0-9]{3})?\s*\|([^\|]*)\|?(.*)/i)
	{  
#print "It matched #1<p>";
	    $daynum = $1;
	    $yearnum = $3;
	    $message = $4;
	    $url = $5;   
#print $daynum." day<p>";
#print $yearnum." year<p>";
#print $message." message<p>";
#print $url." url<p>";
	    
	    if($2)
	    {   $monthName = $2;
		$monthName =~ y/A-Z/a-z/;
	    } 
	    else
	    {   @cal = `cal $month $year`;
		$cal[0] =~ /\s*([A-Za-z]{3}).*/;
		$monthName = $1;
		$monthName =~ y/A-Z/a-z/;
	    }
	    
	    if ($yearnum)
	    {  $key = $daynum.'-'.$months{$monthName}.'-'.$yearnum;  }
	    else
	    {  $key = $daynum.'-'.$months{$monthName}.'-'.$year;  }
	    
	    if($url)
	    { $dates{$key} .= " </h3> <A HREF=\"".$url."\">".$message."</A>";}
	    else  
	    { $dates{$key} .= " </h3> ".$message; }  
	}
	else 
	{
	    if(/^\s*([A-Za-z]{3})?[A-Za-z]*,?\s*([A-Za-z]{3})?[A-Za-z]*,?\s*([A-Za-z]{3})?[A-Za-z]*,?\s*([A-Za-z]{3})?[A-Za-z]*,?\s*([A-Za-z]{3})?[A-Za-z]*,?\s*([A-Za-z]{3})?[A-Za-z]*,?\s*([A-Za-z]{3})?[A-Za-z]*\s*([A-Za-z]{3})?[A-Za-z]*\s*([12][0-9]{3})?\s*\|([^\|]*)\|?(.*)/i)
	    {
#print "It matched #2<p>";
		$yearnum = $9;
		$message = $10;
		$url = $11;
		$day1 = $1;  $day1 =~ y/A-Z/a-z/;
		$day2 = $2;  $day2 =~ y/A-Z/a-z/;
		$day3 = $3;  $day3 =~ y/A-Z/a-z/;
		$day4 = $4;  $day4 =~ y/A-Z/a-z/;
		$day5 = $5;  $day5 =~ y/A-Z/a-z/;
		$day6 = $6;  $day6 =~ y/A-Z/a-z/;
		$day7 = $7;  $day7 =~ y/A-Z/a-z/;
		
#print $day1." 1\n";
#print $day2." 2\n";
#print $day3." 3\n";
#print $day4." 4\n";
#print $day5." 5\n";
#print $day6." 6\n";
#print $day7." 7\n";
		
		@cal = `cal $month $year`;
		if($8)
		{   $monthName = $8;
		    $monthName =~ y/A-Z/a-z/;
		} 
		else
		{   
		    $cal[0] =~ /\s*([A-Za-z]{3}).*/;
		    $monthName = $1;
		    $monthName =~ y/A-Z/a-z/;
		}
		if($months{$day1}){ $monthName = $day1; }
		if($months{$day2}){ $monthName = $day2; }
		if($months{$day3}){ $monthName = $day3; }
		if($months{$day4}){ $monthName = $day4; }
		if($months{$day5}){ $monthName = $day5; }
		if($months{$day6}){ $monthName = $day6; }
		if($months{$day7}){ $monthName = $day7; }
#print $8;
#print $monthName;
		shift(@cal);
		shift(@cal);
		shift(@cal);
		chop($cal[0]);
		
		$cal[0] =~ /(1?[0-9]) +(1?[0-9]) +(1?[0-9]) +(1?[0-9]) +(1?[0-9]) +(1?[0-9]) +(1?[0-9])/;
		
		if($1 > 7){ $one = $1 - 7;   }else{ $one = $1; }
		if($2 > 7){ $two = $2 - 7;   }else{ $two = $2; }
		if($3 > 7){ $three = $3 - 7; }else{ $three = $3; }
		if($4 > 7){ $four = $4 - 7;  }else{ $four = $4; }
		if($5 > 7){ $five = $5 - 7;  }else{ $five = $5; }
		if($6 > 7){ $six = $6 - 7;   }else{ $six = $6; }
		if($7 > 7){ $seven = $7 - 7; }else{ $seven = $7; }
		
		if ($day1)
		{ 
		    if($days{$day1} == 1){ $daynum = $one; }
		    if($days{$day1} == 2){ $daynum = $two; }
		    if($days{$day1} == 3){ $daynum = $three; }
		    if($days{$day1} == 4){ $daynum = $four; }
		    if($days{$day1} == 5){ $daynum = $five; }
		    if($days{$day1} == 6){ $daynum = $six; }
		    if($days{$day1} == 7){ $daynum = $seven; }
		    
		    $i = 0;
		    
		    while($i < 6)
		    {  
			if ($yearnum)
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$yearnum;  }
			else
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$year;  }
			
			if($url)
			{ $dates{$key} .= " <br> <A HREF=\"".$url."\">".$message."</A>";}
			else  
			{ $dates{$key} .= " <br> ".$message; }
			$i++;
			$daynum = $daynum + 7;
		    }
		}
		if ($day2)
		{  
		    if($days{$day2} == 1){ $daynum = $one; }
		    if($days{$day2} == 2){ $daynum = $two; }
		    if($days{$day2} == 3){ $daynum = $three; }
		    if($days{$day2} == 4){ $daynum = $four; }
		    if($days{$day2} == 5){ $daynum = $five; }
		    if($days{$day2} == 6){ $daynum = $six; }
		    if($days{$day2} == 7){ $daynum = $seven; }
		    
		    $i = 0;
		    
		    while($i < 6)
		    {  
			if ($yearnum)
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$yearnum;  }
			else
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$year;  }
			
			if($url)
			{ $dates{$key} .= " <br> <A HREF=\"".$url."\">".$message."</A>";}
			else  
		    { $dates{$key} .= " <br> ".$message; }
			$i++;
			$daynum = $daynum + 7;
		    }
		}
		if ($day3)
		{  
		    if($days{$day3} == 1){ $daynum = $one; }
		    if($days{$day3} == 2){ $daynum = $two; }
		    if($days{$day3} == 3){ $daynum = $three; }
		    if($days{$day3} == 4){ $daynum = $four; }
		    if($days{$day3} == 5){ $daynum = $five; }
		    if($days{$day3} == 6){ $daynum = $six; }
		    if($days{$day3} == 7){ $daynum = $seven; }
		
		    $i = 0;
		    
		    while($i < 6)
		    {  
			if ($yearnum)
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$yearnum;  }
			else
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$year;  }
			
			if($url)
			{ $dates{$key} .= " <br> <A HREF=\"".$url."\">".$message."</A>";}
			else  
			{ $dates{$key} .= " <br> ".$message; }
			$i++;
			$daynum = $daynum + 7;
		    }
		}
		if ($day4)
		{  
		    if($days{$day4} == 1){ $daynum = $one; }
		    if($days{$day4} == 2){ $daynum = $two; }
		    if($days{$day4} == 3){ $daynum = $three; }
		    if($days{$day4} == 4){ $daynum = $four; }
		    if($days{$day4} == 5){ $daynum = $five; }
		    if($days{$day4} == 6){ $daynum = $six; }
		    if($days{$day4} == 7){ $daynum = $seven; }
		    
		    $i = 0;
		    
		    while($i < 6)
		    {  
			if ($yearnum)
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$yearnum;  }
			else
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$year;  }
			
			if($url)
			{ $dates{$key} .= " <br> <A HREF=\"".$url."\">".$message."</A>";}
			else  
			{ $dates{$key} .= " <br> ".$message; }
			$i++;
			$daynum = $daynum + 7;
		    }
		}
		if ($day5)
		{  
		    if($days{$day5} == 1){ $daynum = $one; }
		    if($days{$day5} == 2){ $daynum = $two; }
		    if($days{$day5} == 3){ $daynum = $three; }
		    if($days{$day5} == 4){ $daynum = $four; }
		    if($days{$day5} == 5){ $daynum = $five; }
		    if($days{$day5} == 6){ $daynum = $six; }
		    if($days{$day5} == 7){ $daynum = $seven; }
		    
		    $i = 0;
		    
		    while($i < 6)
		    {  
			if ($yearnum)
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$yearnum;  }
			else
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$year;  }
			
			if($url)
			{ $dates{$key} .= " <br> <A HREF=\"".$url."\">".$message."</A>";}
			else  
			{ $dates{$key} .= " <br> ".$message; }
			$i++;
			$daynum = $daynum + 7;
		    }
		}
		if ($day6)
		{  
		    if($days{$day6} == 1){ $daynum = $one; }
		    if($days{$day6} == 2){ $daynum = $two; }
		    if($days{$day6} == 3){ $daynum = $three; }
		    if($days{$day6} == 4){ $daynum = $four; }
		    if($days{$day6} == 5){ $daynum = $five; }
		    if($days{$day6} == 6){ $daynum = $six; }
		    if($days{$day6} == 7){ $daynum = $seven; }
		    
		    $i = 0;
		    
		    while($i < 6)
		    {  
			if ($yearnum)
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$yearnum;  }
			else
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$year;  }
			
			if($url)
			{ $dates{$key} .= " <br> <A HREF=\"".$url."\">".$message."</A>";}
			else  
		    { $dates{$key} .= " <br> ".$message; }
			$i++;
		    $daynum = $daynum + 7;
		    }
		}
		if ($day7)
		{  
		    if($days{$day7} == 1){ $daynum = $one; }
		    if($days{$day7} == 2){ $daynum = $two; }
		    if($days{$day7} == 3){ $daynum = $three; }
		    if($days{$day7} == 4){ $daynum = $four; }
		    if($days{$day7} == 5){ $daynum = $five; }
		    if($days{$day7} == 6){ $daynum = $six; }
		    if($days{$day7} == 7){ $daynum = $seven; }
		    
		    $i = 0;
		    
		    while($i < 6)
		    {  
			if ($yearnum)
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$yearnum;  }
			else
			{  $key = $daynum.'-'.$months{$monthName}.'-'.$year;  }
			
			if($url)
			{ $dates{$key} .= " <br> <A HREF=\"".$url."\">".$message."</A>";}
			else  
			{ $dates{$key} .= " <br> ".$message; }
			$i++;
			$daynum = $daynum + 7;
		    }
		}
	    }
	}
    }
}

@cal = `cal $month $year`;
chop($cal[0]);

print "<HTML>\n";
print "<HEAD>\n";
print "<title> 6.838 Online Calendar </title>\n";
print "</HEAD>\n";
print "<BODY>\n";

print "<table width=\"100%\">\n<tr>";

print "<td align=left width=\"10%\">\n";
if ( $USER_INPUT{Month} eq $USER_INPUT{FirstMonth} ) {} else {
	print "<A HREF=";
	print "\"http://graphics.lcs.mit.edu/classes/6.838/S98/calendar/";
	print $monthback{$USER_INPUT{Month}-1};
	print ".html\">";
	print "Previous Month</a>";
}
print "<BR>\n";

print "<td align=center width=\"10%\">\n";
print "<A HREF=\"http://graphics.lcs.mit.edu/classes/6.838/S98/homepage.html\">";
print "Back to 6.838 Homepage</a><BR>\n";

print "<td align=right width=\"10%\">\n";
if ( $USER_INPUT{Month} eq $USER_INPUT{LastMonth} ) {} else {
	print "<A HREF=";
	print "\"http://graphics.lcs.mit.edu/classes/6.838/S98/calendar/";
	print $monthback{$USER_INPUT{Month}+1};
	print ".html\">";
	print "Next Month</a>";
}
print "<BR>\n";

print "</tr></table><br>\n";

print "<table border width=\"100%\" height=\"100%\">\n";
print "<tr> <th colspan=7>$cal[0]</th> </tr>\n";
shift(@cal);
print "<tr><td width=\"10%\" align=center><i>Sunday</i></i></td>\n";
print "<td width=\"10%\" align=center><i>Monday</i></td>\n";
print "<td width=\"10%\" align=center><i>Tuesday</i></td>\n";
print "<td width=\"10%\" align=center><i>Wednesday</i></td>\n";
print "<td width=\"10%\" align=center><i>Thursday</i></td>\n";
print "<td width=\"10%\" align=center><i>Friday</i></td>\n";
print "<td width=\"10%\" align=center><i>Saturday</i></td></tr>\n";

$i = 0;
while($i < 6)
{
   shift(@cal);
   $cal[0] =~ s!(..) ?!<td width=\"10%\"><h3>\1</td>!g;
   $cal[0] =~ s!> ?([0-9]+)!>\1 $dates{\1.'-'.$month.'-'.$year}!g;
   print ("<tr valign=top> ".$cal[0]."</tr>\n");  
   $i++; 
}
print "</table>\n";
print "</BODY>\n";
print "</HTML>\n";


