#!/usr/bin/perl
$start=$ARGV[0];
$num=$ARGV[1];
if (!$start) {
	$start=1;
}
if (!$num) {
	$num=5;
}
open(DIR,"ls -r 1*txt |");
print "Content-type: text/html\n\n";
print "<BASE HREF=\"http://www.planetquake.com/gunslinger/index.html\">\n";
@months=("Jan","Feb","Mar","Apr","May","Jun",
	 "Jul","Aug","Sep","Oct","Nov","Dec");
#print "Start: $start<BR>\n";
#print "Num: $num<BR>\n";

$top=`cat main.html`;
print $top;

$total=0;
while ($line=<DIR>) {
	chop($line);
	$file[$total]=$line;
	$total++;
}
$end=$start+$num-1;
if ($end>$total) {
	$end=$total;
}

print "<TABLE BORDER=0 WIDTH=99% CELLPADDING=4 CELLSPACING=0>\n";

for ($i=$start-1;$i<$end;$i++) {
	open(IN,$file[$i]);
	$title=<IN>;
	chop($title);
	$date=$file[$i];
	$date=~s/.txt//;
	$time=$date;
	$time=~s/.*\.//;
	$hour=substr($time,0,2);
	$time=substr($time,2,2);
#	substr($time,2,0)=":";
	if ($hour<12) {
		$time.=" AM";
		if ($hour==0) {
			$hour=12;
		}
	} else {
		$time.=" PM";
		$hour-=12;
	}
	if ($hour==0) {
		$hour=12;
	}
	$year=substr($date,0,4);
	$mon=substr($date,4,2);
	$day=substr($date,6,2);
	$monthname=$months[$mon-1];
	$dayofweek=`date -d '$mon/$day/$year' +%A`;
	chop($dayofweek);
	print "<TR BGCOLOR=#880000><TD NOWRAP ALIGN=LEFT><B>$title</B></TD><TD NOWRAP ALIGN=RIGHT><FONT SIZE=-2>$dayofweek, $monthname $day, $year $hour:$time</FONT></TD></TR>\n";
	print "<TR><TD COLSPAN=2>\n";
	while ($in=<IN>) {
		print $in;
	}
	print "<DIV ALIGN=RIGHT><I><A HREF=\"mailto:stone\@rps.net\">Quintin Stone</A></I></DIV>\n";
#	print "<BR>\n";
	print "&nbsp;</TD></TR>\n";
}
print "</TABLE>\n";

print "<TABLE BORDER=0 WIDTH=99% CELLPADDING=2 CELLSPACING=0>\n";
if ($start>1) {
	if ($end<$total) {
		print "<TR BGCOLOR=#000088><TD ALIGN=LEFT>&nbsp;";
		print "<A HREF=\"http://dynamic.gamespy.com/~qstone/gunslinger.pl?",($start-$num),"+$num\"><FONT COLOR=#FFFFFF><B>Next $num news entries</B></FONT></A>";
		print "</TD>\n";
		print "<TD ALIGN=RIGHT>";
		print "<A HREF=\"http://dynamic.gamespy.com/~qstone/gunslinger.pl?",($start+$num),"+$num\"><FONT COLOR=#FFFFFF><B>Previous $num news entries</B></FONT></A>";
		print "&nbsp;</TD></TR>\n";
	} else {
		print "<TR BGCOLOR=#000088><TD ALIGN=CENTER>";
		print "<A HREF=\"http://dynamic.gamespy.com/~qstone/gunslinger.pl?",($start-$num),"+$num\"><FONT COLOR=#FFFFFF><B>Next $num news entries</B></FONT></A>";
		print "</TD></TR>\n";
	}
} else {
	if ($end<$total) {
		print "<TR BGCOLOR=#000088><TD ALIGN=CENTER>";
		print "<A HREF=\"http://dynamic.gamespy.com/~qstone/gunslinger.pl?",($start+$num),"+$num\"><B><FONT COLOR=#FFFFFF>Previous $num entries</FONT></B></A>";
		print "</TD></TR>\n";
	}

}
print "</TABLE>\n";

$bottom=`cat bottom.html`;
print $bottom;
