#!/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";
	}
}	

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

print INDEX "<html>\n<title>SEP Report: SEP-$rep_num</title>\n\n";
print INDEX "<body bgcolor=white text=black link=blue vlink=red>\n";
print INDEX "<center><h1><i><u>SEP-$rep_num</i></u></h1></center>\n\n";
print INDEX "<center>\n<table border=1 cellpadding=6>\n";
print INDEX "	<tr>\n	<td align=center><b><u>Title\n	<td align=center><b><u>Author(s)\n	<td align=center><b><u>Download\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);
#	$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);   # size of ps.gz file
		open( DESC, "$papnum.desc");
		$paper_title = <DESC>;
		$null = <DESC>;
		$author = <DESC>;
		$abstract_file = $papnum."_abs.html";
		print INDEX "		<td><b>$paper_title\n";
 		print INDEX "		<td><b>$author\n"; 
    print INDEX "   <td><a href='./$pdf_file'>pdf</a><font size=-1> ($pdf_size K)</font>\n";
    print INDEX "   <br><a href='./$ps_file'>ps.gz</a><font size=-1> ($ps_size K)</font>\n";
    print INDEX "   <br><a href='./$abstract_file'>Abstract</a>\n";
		$null = <DESC>;
		open( ABSTRACT, "> $abstract_file");
		print ABSTRACT "<title>$paper_title, by $author</title>\n";
		print ABSTRACT "<body bgcolor=white text=black link=blue vlink=red>\n";
		print ABSTRACT "<center><h1><u>$paper_title,</u> by $author</u></h1></center>\n";

		while( $this_line	= <DESC>) {
			print ABSTRACT "			$this_line";
		}

		print ABSTRACT "\n<p>\n<hr>\n<center>\n<table border=0 cellspacing=15 cellpadding=5>\n<tr>\n";
		print ABSTRACT "<td align=center bgcolor=#ccccff>\n<a href='./index.html'><b><font size=+1>&laquo; BACK</font></b></a><p> to SEP-$rep_num index page\n";
		print ABSTRACT "<td align=center bgcolor=#ccccff>\n<b><font size=+1>DOWNLOAD</font></b><br>\n";
		print ABSTRACT "<a href='./$pdf_file'>pdf</a>($pdf_size KB)\n<br><a href='./$ps_file'>ps.gz</a>($ps_size KB)\n";
		print ABSTRACT "<td align=center bgcolor=#ccccff>\n<a href='http://sepwww.stanford.edu'><img src='http://sepwww.stanford.edu/gifs/sep_title_left_sm.gif' border=0 align='left'></a>\n";
		print ABSTRACT "<a href=http://sepwww.stanford.edu><b><font style=text-decoration:none>STANFORD<br>EXPLORATION<br>PROJECT</font></a>";
		print ABSTRACT "</table>\n";
		close( ABSTRACT);
	}				
}	

print INDEX "</table>\n\n<p>";
print INDEX "\n<center>\n<table border=1 cellspacing=0 cellpadding=10>\n<tr>\n";
print INDEX "<td align=center>\n<font size=+1><b>\n";
print INDEX "<a href='http://sepwww.stanford.edu/research/reports/old_reports.html'>&laquo;-- BACK</a>";
print INDEX "</font> to Old Reports page.\n\n";
print INDEX "<td align=center>\n<a href='http://sepwww.stanford.edu'><img src='http://sepwww.stanford.edu/gifs/sep_title_left_sm.gif' border=0 align='left'></a>\n";
print INDEX "<a href=http://sepwww.stanford.edu><b><font style=text-decoration:none>STANFORD<br>EXPLORATION<br>PROJECT</font></a>";
print INDEX "</table>\n";

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


close( INDEX);

exit( 0);
