#!/usr/bin/perl -wT

use strict;

my $http_host = $ENV{'HTTP_HOST'};
my $request_uri = $ENV{'REQUEST_URI'};
my $gallery = $request_uri;

$gallery =~ s/\/~pengo\/images\///;
chop ($gallery);

print "Content-type: text/html\n\n";
print <<END_OF_PAGE;
<HTML>
<HEAD>
<TITLE> </TITLE>
<style type="text/css">
        body {
        background : #000000 ;
        color : #EEEE00; }
        A:link, A:visited { text-decoration : none }
        A:visited { color : #FFFF00 }
        A:link { color : #FFFF00 }
        A:hover { color : #808080;
	background :  ;}
        img { border-color: #000000}
</style>
</HEAD>
<body>
<a href = "http://www.yikes.com/~pengo">main</a>.<a href = 
"http://www.yikes.com/~pengo/images/">images</a>.$gallery
<BR><BR>
<center>
<table bgcolor = "#404040" border = "0" cellpadding = "10" cellspacing = "0">
 <tr>
END_OF_PAGE


opendir (DH, "./");
readdir(DH);
readdir(DH);

my $http_cookie = $ENV{'HTTP_COOKIE'};
my $address;
my $thumbnail;
my $i = 0;
my @rray;
my $tring;

while (defined($address = readdir(DH))) {	
    if($address =~ /(\.jpg|\.JPG)/){
	if (!$i--)
        {
            $i = 3;
            print "</tr>\n<tr>\n";
        }
        print "<td align = \"center\" valign = \"bottom\">";
        print "<a href = \"http://$http_host/~pengo/cgi-bin/imgloader.cgi?$request_uri&$address\">";
        print "<img src = \"thumbnails/$address\"></a>";
	if($http_cookie =~ "mst3k=1")
	{
	    open(FH, "comments/" . $address . ".txt");
            read(FH, $tring, 2056);
            close(FH);
            @rray = split (/\n/,$tring);
            print "<br><br>" . @rray . " Comment";
            if(@rray != 1)
	    {
		print "s\n";
            }     
            $tring = "";
        }
        print "</td>\n"
    }
}
print "</tr></table><br><br>";
print '<a href = "../../cgi-bin/setcookie.cgi?' . $request_uri .'"><img border = "0" src = "../random/mst3k_logo.jpg" height = "100"></a><br><br>';

if($http_cookie =~ "mst3k=1")
{
print '<font face = "arial" color = "#00FF00">ENABLED</font>';
}
else
{
print '<font face = "arial" color = "#FF0000">DISABLED</font>';
}

print <<END_OF_PAGE;
</center>
</body>
</HTML>
END_OF_PAGE

closedir(DH);
