#!/usr/bin/perl

use warnings;

# perl script adds links to urls to be interpreted by the hyperref package 
# for BibTeX items for which an automatic translation from the eprint
# field to the url is possible (e.g. hep-ex) 
#
# the key technique for the string replacement is the use of backreferences
#
# synopsis: addhref filename.bbl

$outfile = $ARGV[0];
@fileparts = split(/\./, $outfile);
#print "@fileparts\n";

$infile = $fileparts[0] . "_nohref." . $fileparts[1];
#print "$infile\n";

system("mv $outfile $infile");

my($infile)="$infile";
my($outfile)="$outfile";

open(INFILE,$infile);
open(OUTFILE,">$outfile");

while(<INFILE>) {

# hep-??

    if (m/(hep-[a-z][a-z]\/[\d]+)/) {
	print "Hyper reference to be added for $1!\n";
	s/($1)/\\href{http:\/\/arxiv.org\/abs\/$1}{$1}/;
    }

    print OUTFILE "$_";
}

close(INFILE);
close(OUTFILE);

system("rm $infile");
