#!/usr/bin/perl

$the_dir = `pwd`;
@path = split("/", $the_dir);
$this_dir = $path[$#path];
print "Working on report $this_dir";
if( $this_dir =~ /.*(\d)(\d)/) {
	$digit1 = $1;  $digit2 = $2;
	if( $digit1 == 0) {
		$rep_num = $digit2;
	} else {
		$rep_num = "$digit1$digit2";
	}
	$cmd = "cat ".$digit1.$digit2."_name";
} else {
	chop($this_dir);
	$cmd = "cat ".$this_dir."_name";
}

$title_name = `$cmd`;
($title, $author) = split( "\n", $title_name);

open( INDEX, "> index.html");

print INDEX "<html>\n<title>SEP Thesis: SEP-$rep_num, by $author</title>\n\n";
print INDEX "<body bgcolor=white text=black link=blue vlink=red>\n";
print INDEX "<center><h1><i><u>$title</u></i>, by $author</h1></center>\n\n";
print INDEX "<center>\n<table border=1 cellpadding=6>\n";

$pdf_list = `ls -s | grep pdf`;
@pdf_files = split("\n", $pdf_list);

foreach $pdf_file_size( @pdf_files) {
	print INDEX "	<tr>\n";
	($pdf_size, $pdf_file) = split(" ", $pdf_file_size);
#	$pdf_size = int($size/2);
	if( $pdf_file =~ /(.*)\.pdf/) {
		$papnum = $1;
		print "Processing $pdf_file...$pdf_size KB \n";
		if(-e "$papnum.ps.gz") {
			print "$papnum.ps.gz already exists...\n";
		} else {
			print "Converting $pdf_file to ps.gz...\n";
			system("acroread -toPostScript $pdf_file; gzip $papnum.ps");
		}
		$ps_list = `ls -s $papnum.ps.gz`;
		($ps_size, $ps_file) = split(" ", $ps_list);
		open( DESC, "$1.desc");
		$chap_title = <DESC>;
		print INDEX "		<td align=center><b><font size=+1>$chap_title</font></b><p>\n";
		print INDEX "			<a href=$pdf_file>pdf ($pdf_size KB)</a><br>\n";	
		print INDEX "			<a href=$ps_file>ps.gz ($ps_size KB)</a><br>\n";	
 		print INDEX "		<td>\n"; 
		$null = <DESC>;
		while( $this_line	= <DESC>) {
			print INDEX "			$this_line";
		}
	}				
}	

print INDEX "</table>\n\n<p>\n<b><font size=+1>";
print INDEX "<a href='http://sep2.stanford.edu/doku.php?id=sep:research:theses'>&laquo; -- BACK</a>";
print INDEX "</font> to SEP thesis page.\n\n";

print INDEX "</center>\n\n</body>\n</html>\n";

close( INDEX);

exit( 0);
