#!/usr/bin/perl -wT

use strict;

my $http_cookie = $ENV{'HTTP_COOKIE'};
my $script_name = $ENV{'SCRIPT_NAME'};
my $bgcolor = "#404040";
my @colors = ($bgcolor,"#70A0F0","#90C0FF","FF0000");
my @links = ("Schedule","About Me","MST3K Interface","Logic Problems");
my @urls = ("schedule.cgi","about.cgi","cgi-bin/setcookie.cgi?http://www.yikes.com/~pengo/stuff.cgi","logic");
my $file = "random_image.txt";
my $buf;
my @imglist;
my @img;
my @paths;
my $text;
my $i;
my $j;
my $k = 0;
my $twidth = 8;
my $theight = 10;
my $cwidth = 80;
my $cheight = 45;
my $rootcolors = 1.73;
my $random;
my $misties;
my @randbox = (-1,-1);
my @randpic;
my @randmenu;
my @comments;
my @menusize = (1,4);
my @picsize = (3,4);

open(FH, $file) or die "could not open $file";
read(FH, $buf, 2056);
@imglist = split(/\n/,$buf);
@img = split(/&/,$imglist[int(rand(@imglist))]);
@picsize = split(/,/,$img[1]);

$randpic[0] = int(rand($twidth-$picsize[0]+1));
$randpic[1] = int(rand($theight-$picsize[1]+1));

$randmenu[0] = int(rand($twidth-$menusize[0]+1));
$randmenu[1] = int(rand($theight-$menusize[1]+1));


if (($randmenu[0]+$menusize[0]-1 >= $randpic[0] && $randmenu[0] < $randpic[0] + $picsize[0]) &&
    ($randmenu[1]+$menusize[1]-1 >= $randpic[1] && $randmenu[1] < $randpic[1] + $picsize[1]))
{
  $randmenu[0] = ($randpic[0] + $picsize[0] + $randmenu[0]%($twidth - $picsize[0]))%$twidth;
}

if(rand(20) < 1)
{
  $randbox[0] = int(rand($twidth));
  $randbox[1] = int(rand($theight));

  if (($randbox[1] >= $randpic[1] && $randbox[1] < $randpic[1] + $picsize[1]) &&
      ($randbox[0] >= $randpic[0] && $randbox[0] < $randpic[0] + $picsize[0]))
  {
    $randbox[1] = ($randpic[1] + $picsize[1] + $randbox[1]%($theight - $picsize[1]))%$theight; 
  }
}

if($http_cookie =~ "mst3k=1")
{
  $buf = "";
  $file = "templates/mst3k.html";
  open(FH, $file);
  read(FH, $misties, 8192);
  close(FH);
  @paths = split (/\//,$script_name);
  $file = 'comments/' . pop(@paths) . '.txt';
  open(FH, $file);
  read(FH, $buf, 1024);
  @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>
<head>
<title>
Random Stuff
</title>
</head>
<style type="text/css">
        body { font-family : "times";
        background : $bgcolor;
        color : white; }
        A:link, A:visited { text-decoration : none }
        A:visited { color : #FFFFFF }
        A:link { color : #FFFFFF }
        A:hover { color : #0000FF;
	background :  ;}

</style>
<body>
<a href = "http://www.yikes.com/~pengo">main</a>.stuff
<br><br>
<table border = "0" cellpadding = "0" cellspacing = "5">
<tr>
<td rowspan = "11" width = "80">
<font face = "Arial" size = "+3">R<br>A<br>N<br>D<br>O<br>M<br><br>S<br>T<br>U<br>F<br>F<br></font></td>
</tr>
END_OF_TOP

for($i = 0; $i < $theight; $i++)
{
  print "<tr>\n"; 
  for($j = 0; $j < $twidth; $j++)
  {
    $text = "";
    if($i == $randpic[1] && $j == $randpic[0])
    {
      print "<td align = \"center\" rowspan = $picsize[1] colspan = $picsize[0]><img src = \"$img[0]\"></td>";
    }
    if($i >= $randpic[1] && $i < $randpic[1] + $picsize[1] && $j == $randpic[0])
    {
      $j = $randpic[0]+$picsize[0];
    }
    if($i >= $randmenu[1] && $i < $randmenu[1] + $menusize[1] && $j == $randmenu[0])
    {
      $text = "<a href = \"" . $urls[$k] . "\">" . $links[$k++] . "</a>";
    }
    if(($i == $randbox[1] && $j == $randbox[0]) && !($i >= $randmenu[1] && $i < $randmenu[1] + $menusize[1] && $j == $randmenu[0]))
    {
      $random = @colors - 1;
      $text = "<a href =\"panic.cgi\"><b>P a n i c</b></a>";
    }
    else
    {
      $random = rand($rootcolors);
      $random = int($random * $random);
    }

    print "<td align = \"center\" height = \"$cheight\" width = \"$cwidth\" bgcolor = $colors[$random]>$text</td>\n";  
  }
  print "</tr>\n";
}

print <<END_OF_PAGE;
</table>
$misties
</body>
</html>
END_OF_PAGE
