#! /usr/bin/perl

$location = "/home/dtj/public_html/";
$httploc  = "uberh4x0r.org/~dtj";
$theTitle = "UNTITLED";

use FileHandle;
use CGI;
$cgi = new CGI;

($datafile) = @ARGV;

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

#print "<U><B>".$datafile."</U></B><P>\n";
$theFile=$location.$datafile;
if (-d $theFile) {
	$theFile=$theFile."/index";
}

$preambleShown = 0;
$filesInLine   = 0;
$tableStarted  = 0;

if (($datafile eq "") || (! -e $theFile)) {
	print "<html><body>&nbsp;<p>&nbsp;<p>&nbsp;<p><center><H1>No Data Found!</H1>\n";
}

open(INDEX_FILE,"<$theFile") || die "no index file";
while (<INDEX_FILE>) {
	if (/^#/) { next; }

		#-----------------------#
		#	The Title	#
		#-----------------------#

	if (/^\%T/) {	# new table or starter
		($lhs,$rhs) = split(/ /,$_,2);
		$theTitle = $rhs;
		next;
	}

		#-----------------------#
		#	 Literal	#
		#-----------------------#

	if (/^"/) {
		print $_;
		next;
	}

		#-----------------------#
		#	New Table	#
		#-----------------------#

	if (/^\%P/) {	# new table or starter
		if ($tableStarted != 0) {
			if ($filesInLine > 0) {
				for ($i = $filesInLine; $i < 3; $i++) {
					print "<td class='pic'>"; # bogus items
				}
			}
			EndTable();		# close the table
		}
		# open the table
		($lhs,$rhs) = split(/ /,$_,2);
		StartTable($rhs);
		next;
	}

	if (/^\%R/) {	# New Row 
		NewRow();
		next;
	}

	chomp;

	if ($tableStarted == 0) {		# no table yet, so open blank
		StartTable();
	}
	if ($filesInLine == 0) {		# need to start line
		print "<TR><TD COLSPAN=4 class='pic'><FONT SIZE=1>&nbsp;</FONT><BR></TR>\n";

		print "<tr><TD class='pic'>&nbsp;</td>\n";
	}

		#-----------------------#
		#	The Image	#
		#-----------------------#

	print "<td class='pic'><center>";
	print "<A  HREF='http://".$httploc."/".$datafile."/".$_."_med.jpg'>";
	print "<img src='http://".$httploc."/".$datafile."/".$_."_sm.jpg' border=0></A>\n";
	print "<br>\n";
	print "<A  HREF='http://".$httploc."/".$datafile."/".$_."_big.jpg'>";
	print "<FONT size=1><b>$_</b></FONT></center>\n";
	print "</A>\n";

	$filesInLine++;
	if ($filesInLine >= 3) {
		NewRow();
	}
}

sub NewRow {
	if ($filesInLine > 0) {
		for ($i = $filesInLine; $i < 3; $i++) {
			print "<td class='pic'>";	# bogus items
		}
	}
	print "</tr>\n";
	$filesInLine = 0;
}

WritePostamble();

sub WritePreamble {
	print "<html>";
	print "<HEAD>\n";
	print "<TITLE>".$theTitle."</TITLE>\n";
	print "</HEAD>\n";
	print "<style type=\"text/css\">\n";
	print "body { background-color:#FFFFFF; font-family: Helvetica, Geneva, Arial, SunSans-Regular, sans-serif }\n";
	print "TR.top { background: #FFFFFF }\n";
	print "TD.pic { background: #E0E0E0; font-family: Helvetica, Geneva, Arial, SunSans-Regular, sans-serif }\n";
print "A:link {text-decoration: none}\n";
print "A:visited {text-decoration: none}\n";
print "A:active {text-decoration: none}\n";
	print "</style>\n";
	print "<BODY>\n";
	print "<CENTER>\n";
	print "<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0 WIDTH='90%'>\n";
	print "<TR class='top'><TD COLSPAN=4><CENTER><HR><B><FONT size=+4>".$theTitle."</FONT></B><HR></CENTER>\n";
	print "<TR class='top'><TD COLSPAN=4>&nbsp;</TD></TR>\n";
	print "</TABLE>\n";
}


sub WritePostamble {
	#
	#	Clean up whats left.
	#
	EndTable();
	print "</BODY>";
	print "</HTML>";
}

sub StartTable {
        $title = $_[0];
	if ($title eq "") { $title = "UNKNOWN"; }
	if ($preambleShown == 0) {
		WritePreamble();
		$preambleShown = 1;
	}

	print "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>\n";
	print "<TR class='top'>\n";
	print "<TD BGCOLOR='#000000' VALIGN=TOP ALIGN=LEFT><IMG SRC='http://www.uberh4x0r.org/~dtj/Assets/upperLeft.gif' WIDTH=13 HEIGHT=16>";
	print "<TD COLSPAN=3 BGCOLOR='#000000'><B><FONT size=6 color='#FFFFFF'>$title</FONT></B></TR>";
	$tableStarted = 1;
	$filesInLine  = 0;
}

sub EndTable {
	if ($filesInLine > 0) {
		for ($i = $filesInLine; $i < 3; $i++) {
			print "<TD class='pic'>";	# bogus items
		}
	}
	if ($tableStarted != 0) {
		print "<TR><TD COLSPAN=4><FONT SIZE=1>&nbsp;</FONT><BR></TR>";
		print "</TABLE>";	# close the table
		$tableStarted = 0;
	}
}
