#!/usr/bin/perl -wT

use strict;

my $file = "people.txt";
my $buf;
my @people;
my @attrib;
my @name;
my @vice;
my @status;
my @link;
my $paid = 0;
my $i;
my $j;
my $http_cookie = $ENV{'HTTP_COOKIE'};
my $script_name = $ENV{'SCRIPT_NAME'};
my $misties = "";
my @comments;
my @paths;
my $random;
my $random_image;
my $random_link;
my $random_picture;
my $address;
my @pictures;
my $dir;

open (FH, $file) or die "cannot open $file";
read (FH, $buf, 8192);
@people = sort{$b cmp $a}(split(/#/,$buf));
for($i = 0; $i < @people; $i++)
{
	@attrib = split (/,/,$people[$i]);
	$name[$i] = $attrib[0];
	$name[$i] =~ s/\n//;
	$vice[$i] = $attrib[1];
	$status[$i] = $attrib[2];
	$link[$i] = $attrib[3];
#	if ($status[$i] =~ "paid")
#		$paid = $paid + 1;
	chomp($link[$i]);
}
  @paths = ("../images/rafting_2005","../images/rafting_2003","../images/rafting_2004");

  $dir = $paths[int(rand(@paths))];
  opendir (DH, $dir);
  while(defined($address = readdir(DH)))
  {
    if($address =~ /(\.jpg|\.JPG)/)
    {
      push(@pictures,$address);
    }
  }
  $random_picture = $pictures[int(rand(@pictures))];
  $random_image = "<img border = \"1\" src = \"$dir/thumbnails/$random_picture\">\n";
  $random_link = "http://www.yikes.com/~pengo/cgi-bin/imgloaderon.cgi?/~pengo$dir/&$random_picture";
  $random_link =~ s/\.\.//;
  closedir (DH);

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 {
        background : #FFFFFF ;
        color : black; }
        A:link, A:visited { text-decoration : none }
        A:visited { color : RED }
        A:link { color : RED }
        A:hover { color : ORANGE;}
</style>
<center>
<a href = http://www.yikes.com/~pengo/images/rafting_2003/>2003 </a>
<a href = http://www.yikes.com/~pengo/images/rafting_2004/>2004 </a>
<a href = http://www.yikes.com/~pengo/images/rafting_2005/>2005</a>
<br><br>
Random Rafting Image<br>
<a href=$random_link>
$random_image
</a>
<br><br>
Honestly, I told it sort everything. I dunno why it won't sort me
</center>
<br>
<TABLE border=1 cellspacing=0 cellpadding=5 align=center width=50%>
<TR>
 <TD WIDTH = 5% ALIGN = CENTER>#</TD>
 <TD WIDTH = 15% ALIGN = CENTER>NAME</TD>
 <TD WIDTH = 10% ALIGN = CENTER>VICE</TD>
 <TD WIDTH = 5% ALIGN = CENTER>STATUS</TD>
</TR>
end_of_top

for($i = 0; $i < @people; $i++)
{
	print "<TR>\n";
	print " <TD>".($i+1)."</TD>\n";
	print " <TD>\n";
	if($link[$i])
	{
		print"  <a href=\"$link[$i]\">\n";
	}
	print "   <b>$name[$i]</b>\n";
	if($link[$i])
	{
		print"  </a>\n";
	}
	print " </TD>\n";	
	print " <TD>$vice[$i]<font color=WHITE>v</font></TD>\n";
	print " <TD>$status[$i]</TD>\n";
	print "</TR>\n";
}
print <<end_of_bottom;
</table>
<h2>Things to bring</h2>
<li> Sunscreen - Duh </li>
<li> Big Hat - Sunscreen on the face on the river is problematic </li>
<ul><i><b><font size = -1>REI sells what must be a vaseline-based SPF50 
sunscreen that does indeed stay on the face in the river, even after 
dunks.  I use it when scuba diving.  Recommended By Doug[tm]
</font></b></i></ul>
<li>Cheap Sun Glasses - Nice glasses tend to be mangled or stolen by the 
river.  Rest assured, the ugly pair that you planned on losing will stay 
with you for years to come.  Polarized is also nice, it'll cut out all 
the reflections off the river.</li>
<li>Light Long Sleeve Shirt - Not really needed, but I like to wear one 
sometimes on the river.</li>
<li>Teva's or Old Tennis Shoes - You need something to wear while 
rafting to protect your feet from rocks and either doesn't mind river 
water or you don't care if they get ruined.  Flip flops or anything that 
won't stay on your feet won't work!</li>
<li>Tolietries - How old are you really? you already know that.</li>
<li>Sleeping Bag - No one has ever forgotten this.</li>
<ul><i><b><font size = -1>no, i did. :)  but thanks for not embarassing 
me.</b></font> -Doug </i></ul>
<br><br>
end_of_bottom

print "$misties</body></html>";
close (FH);
