#!/usr/bin/perl

use CGI qw(param);
$graphicsroot="/gfx/calendar";
$loki="engaged";
$serverroot=$ENV{DOCUMENT_ROOT};
$serverroot = substr($serverroot,0,length($serverroot)-1) if ((substr($serverroot,-1,1) eq "/") && (substr($graphicsroot,0,1) eq "/"));

$align = param("align");
if (defined($align)) {
  $align=lc($align);
  undef $align if (($align ne "right") && ($align ne "left") && ($align ne "center"));
}

$color = param("color");
$color=param("colour") if ( (!(defined($color))) && (param("colour")) );
if (defined($color)) {
  undef $color unless (-d "${serverroot}$graphicsroot/$color");
}

$color="red" unless(defined($color));

$date=(localtime)[3];
$month=((localtime)[4])+1;
$day=(localtime)[6];
$day=7 if ($day == 0);

$fulldate=(qw(0 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st))[(localtime)[3]];
$fullmonth=(qw(January February March April May June July August September October November December))[(localtime)[4]];
$fullday=(qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday))[(localtime)[6]];

loki();

print "Content-type: text/html\n\n";

print "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"";
print " align=\"$align\"" if (defined($align));
print ">\n";
print <<EOF;
<tr>
<td colspan="3" bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="-" width="124" height="2" border="0"></td>
<td><img src="$graphicsroot/pad.gif" alt="-" width="2" height="2" border="0"></td>
</tr>
<tr>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#ffffff"><img src="$graphicsroot/pad.gif" alt=" " width="120" height="8" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
</tr>
<tr>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#ffffff" align="center"><img src="$graphicsroot/$color/months/$month.gif" alt="$fullmonth" width="102" height="11" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
</tr>
<tr>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#ffffff"><img src="$graphicsroot/pad.gif" alt=" " width="120" height="8" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
</tr>
<tr>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#ffffff" align="center"><img src="$graphicsroot/$color/dates/$date.gif" alt="$fulldate" width="103" height="60" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
</tr>
<tr>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#ffffff"><img src="$graphicsroot/pad.gif" alt=" " width="120" height="8" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
</tr>
<tr>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#ffffff" align="center"><img src="$graphicsroot/$color/days/$day.gif" alt="$fullday" width="106" height="11" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
</tr>
<tr>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#ffffff"><img src="$graphicsroot/pad.gif" alt=" " width="120" height="8" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
<td bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="|" width="2" height="2" border="0"></td>
</tr>
<tr>
<td colspan="4" bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="-" width="126" height="2" border="0"></td>
</tr>
<tr>
<td colspan="1"><img src="$graphicsroot/pad.gif" alt="-" width="2" height="2" border="0"></td>
<td colspan="3" bgcolor="#000000"><img src="$graphicsroot/pad.gif" alt="-" width="124" height="2" border="0"></td>
</tr>
</table>
EOF

sub loki {
  if ($loki eq "engaged") {
    # green
    if (($date==17)&&($month==3)) {
      $color="green" if (-d "${serverroot}$graphicsroot/green");
    }
    
    # red
    if (($date==14)&&($month==2)) {
      $color="red" if (-d "${serverroot}$graphicsroot/red");
    }
    if (($date==24)&&($month==12)) {
      $color="red" if (-d "${serverroot}$graphicsroot/red");
    }
    if (($date==25)&&($month==12)) {
      $color="red" if (-d "${serverroot}$graphicsroot/red");
    }
    if (($date==26)&&($month==12)) {
      $color="red" if (-d "${serverroot}$graphicsroot/red");
    }
    
    # black
    if (($month == 11) && ($date == 11) && (($hour>=10)&&($hour<=12)) ) {
      $color="black" if (-d "${serverroot}$graphicsroot/black");
    }
    if (($day == 7) && ($month == 11) && (($date>=8)&&($date<=14))  && (($hour>=10)&&($hour<=12)) ) {
      $color="black" if (-d "${serverroot}$graphicsroot/black");
    }
    
    #datechange
    $year=1900+(localtime)[5];
    $hour=(localtime)[2];
    if (($date==1) && ($month==4) && ($hour < 12)) {
      if (($year % 2) == 0) {
        $date=32;
        $month=3;
      } else {
        $date=rand(28)+1;
        $month=rand(11)+1;
        $day=rand(6)+1;
      }
    }
  }
  
}