#!/usr/bin/perl -w
#
# Args:
# addhyperlinkstobbl.pl 
#          [bbl file]
#          [bibtex file]
#          [file path to local files with trailing slash]
#          [http path to online local files with http:// and trailing slash]
#
# Replace bbl file with one with hyperlinks in it.  The tag used for
# hyperlinks is the latex2html \htmladdnormallink{}{}.
#
# The hyperlinks added are:
#
# * a local link in case the bibtex attribute "availability" contains "online"
#
# * a remote link in case the bibtex attribute "url" exists
#
# * a remote link in case the bibtex attribute "availability" contains
#   "internet (<url>)"

$bblfile = shift @ARGV;
$bibtexfile = shift @ARGV;

$localpath= shift @ARGV;
$localhttppath= shift @ARGV;


# read bibtex database and get the "local","url", and "internet" tags
%bibtexurl=();
%bibtexinternet=();
%bibtexlocal=();

open BIB,"$bibtexfile";
$f = "";
while (<BIB>) { $f .= $_; }
close BIB;

#split by bibtex headers
@bibs = split /(@[a-zA-Z][a-zA-Z]*\s*{\s*[^,]*)/, $f;
$prevkey = "";
for (@bibs) {
	if (/@[a-zA-Z][a-zA-Z]*\s*{\s*([^,]*)/) {
		$prevkey = $1;
	} else {
		if (/availability\s*=([^,}]*)/i) {
			$avattr = $1;
			if ($avattr =~ /online/i) {
				#find file (ugh!)
				if (-e "$localpath$prevkey.ps") {
					$filename = "$prevkey.ps";
				} elsif (-e "$localpath$prevkey.pdf") {
					$filename = "$prevkey.pdf";
				} elsif (-e "$localpath$prevkey.html") {
					$filename = "$prevkey.html";
				} elsif (-e "$localpath$prevkey.htm") {
					$filename = "$prevkey.htm";
				} elsif (-e "$localpath$prevkey.ps.gz") {
					$filename = "$prevkey.ps.gz";
				} else {
					#try without the ":" characters
					$filename = $prevkey;
					$filename =~ s/://g;
					if (-e "$localpath$filename.ps") {
						$filename = "$filename.ps";
					} elsif (-e "$localpath$filename.pdf") {
						$filename = "$filename.pdf";
					} elsif (-e "$localpath$filename.html") {
						$filename = "$filename.html";
					} elsif (-e "$localpath$filename.htm") {
						$filename = "$filename.htm";
					} elsif (-e "$localpath$filename.ps.gz") {
						$filename = "$filename.ps.gz";
					} else {
						$filename = "unknown.html";
					}
				}
				$bibtexlocal{$prevkey} = $filename;
				print "LOCAL: $localhttppath$filename\n";
			}
			if ($avattr =~ /internet\s*\(([^)]*)\)/) {
				$bibtexinternet{$prevkey} = $1;
				print "INTERNET: $1\n";
			}
		}
		#note: ";" is used by springer to separate urls
		if (/url\s*=([^,}]*)/i) {
			$url = $1;
			$url =~ /["{]([^;"}]*)/;
			$bibtexurl{$prevkey} = $1;
			print "URL: $1\n";
		}
		$prevkey = "";
	}
	
}

open BBL,"$bblfile";
$f = "";
while (<BBL>) { $f .= $_; }
close BBL;
if ($f =~ /htmladdnormallink/) {
	die "bbl file $bblfile already processed!";
}
@bibitems = split /(\\bibitem\[[^\]]*\]{[^}]*})/, $f;
$prevkey = "";
$fnew = "";
for (@bibitems) {
	if (/\\bibitem\[[^\]]*\]{([^}]*)}/) {
		$prevkey = $1;
	} else {
		if ($prevkey) {
			$local = $bibtexlocal{$prevkey};
			$url = $bibtexurl{$prevkey};
			$internet = $bibtexinternet{$prevkey};
			#note: we append to the left
			if ($url or $internet or $local) {
				$_ = "\\newblock $_";
			}
			if ($url) {
				$url =~ s/http:\/\///;
				$_ = "\n\\htmladdnormallink{[URL: $url]}{http://$url}$_";
			}
			if ($internet) {
				$internet =~ s/http:\/\///;
				$_ = "\n\\htmladdnormallink{[PAPER URL: $internet]}{http://$internet}$_";
			}
			if ($local) {
				$_ = "\n\\htmladdnormallink{[LOCAL COPY]}{$localhttppath$local}$_";
			}
		}
	}
	$fnew .= $_;
}
open BBL, ">$bblfile";
print BBL $fnew;
close BBL;

