#!/util/bin/perl
# Convert the techrep bib file to a README html file.

$outFilename = "/web/tech-reports/README.html";
$bibFilename = "/web/tech-reports/bibs.refer";
$siteTitle = "Department of Computer Science and Engineering, University at Buffalo";

#set parameters
$delim = "::";      #delimiter between fields


# read the bib file and pack it into an array
open (bibfile, $bibFilename);
$line = 1;
$author = "";
while ($line) {
   $line = <bibfile>;     # read a line
   if ($line ne "\n") {
      chop $line;
   }
 
   # author
   if ($line =~ /\%A/) {  # concat authors
      if (length($author)) {
         $author = $author."; ".substr($line, 3);
      } else {
         $author = substr($line, 3);
      }
   }

   # title
   if ($line =~ /\%T/) {
      $title = substr($line, 3);
   }

   # TR number
   if ($line =~ /\%R/) {
      $trNumber = substr($line, 3);
   }

   # URL
   if ($line =~ /\%U/) {
      $URL = substr($line, 3);
   }

   # Date
   if ($line =~ /\%D/) {
      $date = substr($line, 3);
   }

   # pack it up and save it as a string
   if ($line eq "\n") {
      push (@records, "$trNumber$delim$author$delim$title$delim$date$delim$URL");
      $author = ""; $title = ""; $trNumber = ""; $date = ""; $URL = "";
   }
}
close (bibFile);

# print header to the html file
open (outfile, ">".$outFilename);
print outfile "<html>\n<head>\n";
print outfile "<title>", $siteTitle,"</title>\n";
print outfile "</head>\n<body bgcolor=\"#ffffff\" text=\"#000000\">\n";
print outfile "<div align=\"center\"><h2>Technical Reports at ", $siteTitle, "</h2></div>\n";

# sort the records by trnumber
sub trsort {
  my($p, $q);

  if (substr($a,0,2) ne '20') {
    $p = '19' . $a;
  } else {
    $p = $a;
  }
  if (substr($b,0,2) ne '20') {
    $q = '19' . $b;
  } else {
    $q = $b;
  }
    
  return $p cmp $q;
}

@records = reverse sort trsort @records;

# print them html formatted.
print outfile "<DL>\n";
foreach $record (@records) {
    ($trNumber, $author, $title, $date, $URL) = split (/$delim/, $record);
    if (length($URL)) {
        print outfile "<DT><a href=",$URL,">";
        print outfile $trNumber, "</a>\n";
    } else {
        print outfile "<DT>", $trNumber, " (Not available on-line)\n";
    }
    print outfile "<DD>", $author, ".\n<i>", $title, "</i>,\n", $date, ".\n";
}
print outfile "</DL>\n";

print outfile "</body>\n</html>\n";
close (outfile);

