#!/usr/bin/perl -w
# generate links from .ant files

$html = `pwd`;
$html =~ s/\n//g;
@words = split(/\//, $html);
$dir =  $words[@words - 1];
$html = $dir . ".html";
print "html $html\n";
opendir(DIR, ".");
@files = grep(/\.djvu\.ant$/, readdir(DIR));
open (OUTDATA, ">$html") or die "Can't open $html \n";
closedir(DIR);
$first_iter = 1;
#$home = "http://canberra.cs.umass.edu/~culotta/nips/";
$home = "http://books.nips.cc/";

foreach $file (@files) {
    $title = "";
    $author = "";
    $link = "";
    open (INDATA, $file) or die "Could not open $file \n";
    while ($line = <INDATA>) {
	if ($line =~ m/\(title/) {
	    $quote1 = index($line, "\"",0);
	    $quote2 = index($line, "\"",$quote1 + 1);
	    $title = substr($line, $quote1+1, ($quote2 - $quote1)-1);
	}
	elsif ($line =~ /\(author/) {
	    $quote1 = index($line, "\"",0);
	    $quote2 = index($line, "\"",$quote1 + 1);
	    $author = substr($line, $quote1+1, ($quote2 - $quote1)-1);
	}	
	elsif ($line =~ /\(year/) {
	    $quote1 = index($line, "\"",0);
	    $quote2 = index($line, "\"",$quote1 + 1);
	    $year = substr($line, $quote1+1, ($quote2 - $quote1)-1);
	    $year = $year - 1;
	}	
    }
    if ($first_iter == 1) {
	print OUTDATA "<html><head><title>NIPS $year</title></head><body><table>
 <tr>
 <td>
 <a href=\"" . $home . "\"><img border=0 src=\"". $home . "logo.jpg" . "\"></img></a></td>
 <td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
</td>
<td> <h2> NIPS*$year <br>Online Papers</h2>
</td>
</tr>
</table> <br>
<br><br>
<table width=600><tr><td>";	    
 	$first_iter = 0;
    }
    @words = split (/\./, $file);
    $base_name = $words[0];
    $ps_link = "files/$base_name" . ".ps.gz";
    $pdf_link ="files/$base_name" . ".pdf";
    $djvu_link = "files/$base_name" . ".djvu";
    $bib_link = "files/$base_name" . ".bib";
    $ps_local =  $base_name . ".ps.gz";
    $pdf_local =  $base_name . ".pdf";
    $djvu_local =  $base_name . ".djvu";
    $bib_local =  $base_name . ".bib";
    -e $ps_local and $link = $link . &make_link ($ps_link, "[ps.gz]");
    -e $pdf_local and $link = $link . &make_link ($pdf_link, "[pdf]");
    -e $djvu_local and $link = $link . &make_link ($djvu_link, "[djvu]");
    -e $bib_local and $link = $link . &make_link ($bib_link, "[bibtex]");
    print OUTDATA "<b>$title</b> <br>$author $link<br><br>";
    close INDATA;
}
$first_iter = 1;
print OUTDATA "</td></tr></table></html>";
close OUTDATA;

sub make_link {
    my $file = $_[0];
    my $title = $_[1];
    my $link = "<a href=\"$file\">$title</a>";
    return ($link);
} 

