#!/usr/bin/perl -wT

use strict;

my $time		= localtime;
my $remote_id		= $ENV{REMOTE_HOST} || $ENV{REMOTE_ADDR};
my $admin_email 	= $ENV{SERVER_ADMIN};
my $http_cookie 	= $ENV{'HTTP_COOKIE'};
my $script_name 	= $ENV{'SCRIPT_NAME'};
my @paths;
my $file;
my $buf;
my $i;
my $misties = "";
my @comments;
my $random;
my @quotes;
my $quote;

$file = "random_quote.txt";
open(FH, $file) or die;
read(FH, $buf, 1024);
close(FH);
@quotes = split(/\n/,$buf);
$quote = $quotes[int(rand(@quotes))];

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';
  $misties =~ s/FILENAME/$file/;
  if(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/;
    }
  }
  else
  {
    for($i = 0;$i < 3;$i++)
    {
      $misties =~ s/TEXT//;
    }
  }
}

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

print <<END_OF_PAGE;

<html>
<head>
<title>Don't Panic</title>
</head>
<table border = "0">
 <tr>
  <td valign = "bottom"><H1>Don't Panic!</H1>
  <ul>$quote</ul>
  <ul>If you think this is a server error, please contact the 
  <a href= "mailto:spiderman">webmaster</a></ul>
  <H2>Error 628</H2></td>
  <td rowspan = "2"><img src = "images/kaneda.gif"></td>
 </tr>
 <tr>
  <td valign = "top">
   <ul>
    <A HREF="/">www.yikes.com</A>
    <BR>
    <small>$time</small>
    <BR>
   </ul>
  </td>
 </tr>
</table>
$misties
</body>
</html>

END_OF_PAGE
