#!/usr/bin/perl -wT

use strict;

my $file = "schedule.txt";
my $buf;
my @days = (" ","Mon", "Tue", "Wed", "Thu", "Fri");
my $localtime = localtime;
my @fake_time;
my $fake_day;
my @classes;
my @attrib;
my @start_time;
my @end_time;
my @day;
my @class_name;
my $time;
my $rows;
my $text = "";
my $color;
my $other_color;
my $font_color;
my $i;
my $j;
my $k;
my $http_cookie = $ENV{'HTTP_COOKIE'};
my $script_name = $ENV{'SCRIPT_NAME'};
my $misties;
my @comments;
my @paths;
my $random;

$localtime =~ s/  / /;
@fake_time = split(/ /,$localtime);
$fake_day = $fake_time[0];
@fake_time = split(/:/,$fake_time[3]);
$fake_time[2] = $fake_time[0] + int($fake_time[1]*4/60)/4 -3;

open (FH, $file) or die "cannot open $file";
read (FH, $buf, 4056);
@classes = split (/\n/,$buf);
for($i = 0; $i < @classes; $i++)
{
	@attrib = split (/&&/,$classes[$i]);
	$start_time[$i] = $attrib[0];
	$end_time[$i] = $attrib[1];
	$day[$i] = $attrib[2];
	$class_name[$i] = $attrib[3];
}
if($http_cookie =~ "mst3k=1")
{
  $file = "templates/mst3k.html";
  open(FH, $file);
  read(FH, $misties, 8192);
  close(FH);
  @paths = split(/\//,$script_name);
  $file = 'comments/' . pop(@paths) . '.txt';
  $buf = "";
  open(FH, $file);
  read(FH, $buf, 1024);
  close(FH);
  @comments = split(/\n/,$buf);
  for($i = 0;$i < 3;$i++)
  {
    $random = int(rand(@comments));    
    $random = splice(@comments,$random,1);
    $misties =~ s/TEXT/$random/;
  }
  $misties =~ s/FILENAME/$file/;
}
print "Content-type: text/html\n\n";

print <<end_of_top;

<HTML>
<BODY>
<style type="text/css">
        body { font-family : "arial";
        background : #005050 ;
        color : white; }
        A:link, A:visited { text-decoration : none }
        A:visited { color : #AAAAAA }
        A:link { color : #AAAAAA }
        A:hover { color : #80E0F0;
	background :  ;}
</style>
<TABLE border = \"0\" cellspacing = \"0\" cellpadding = \"5\" align = \"center\">

end_of_top

for($j = 0; $j < 6; $j++)
{
	print "<TH>$days[$j]</TH>";
}
for($j = 0; $j < 64; $j++)
{
	print "<TR>\n";
	for($i = 0; $i < 6; $i++)
	{
		$time = (8+$j/4)-.5;
		if($fake_time[2] == $time && $days[$i] eq $fake_day)		
		{
			$other_color = "AA0000";
		}
		elsif(($i*4+$j+10)%8 >= 4)
		{
			$other_color = "002020";
		}
		else
		{
			$other_color = "000000";
		}
		for($k = 0; $k < @start_time; $k++)
		{
			if($i == $day[$k] && $start_time[$k] <= $time && $end_time[$k] > $time)
			{			
			
				if($time == $start_time[$k])
				{
					if($start_time[$k] <= $fake_time[2] && $fake_time[2] < $end_time[$k] && $days[$i] eq $fake_day)
					{
						$color = "AA0000";
						$font_color = "000000";
					}
					else
					{
						$font_color = "AAAAAA";
						$color = int($time*2)*10000 +int($time*4.5)*100 + int($time*1.5+$i*12)*1;
					}
					$rows = ($end_time[$k] - $start_time[$k])*4;
					$text = "<TD width = \"100\" align = \"center\" rowspan = \"$rows\" bgcolor = \"#$color\"><font color = $font_color>$class_name[$k]</font></td>\n";
				}
				else
				{
					$text = "\n";
				}
			}
		}
		if($i)
		{
			if($text eq "")
			{ 
				$text = "<TD height = \"10\" bgcolor = \"#$other_color\" width = \"100\">\n";
			}
			print $text;
			$text = "";
		}
		elsif(!($j%4))
		{
			$color = "000000";
			print "<TD height = \"40\" rowspan = \"4\" width = \"100\" align = \"right\" valign = \"middle\">";
			print $time%12+1;
			if($time < 11)
			{
				print " am ";
			}
			else
			{
				print " pm ";
			}
			print "</td>\n";
		}
	}
	print"</TR>\n";
}
print "</TABLE>$misties</body></html>";
close (FH);
