########################################################################
# This script goes through the label.pl file created by latex2html and
# creates an html file for each label that immediately redirects the 
# viewer to the relevant location in the main html document. This allows 
# the labels to be readily accessed from pages external to the document.
#
# REVISION HISTORY: 1st version  18 Nov, 2008 
#
# CONTACT: S. Biller, Oxford
########################################################################

$input_file = $ARGV[0];          # Input file name from 1st argument in input list
$output_directory = $ARGV[1];    # Output directory from 2nd argument in input list

# Check for presence of directory
opendir DName, $output_directory or die "ERROR in Make_External_Links: Can't find directory $!";
close DName;

# If directory name doesn't end in "/", add it to the end
my $test_string = $output_directory . '/';
if(index($test_string,'//') eq -1) {$output_directory=$test_string;}

# Open input file or quit and give error if not found
open INPUT, "< $input_file" or die "ERROR in Make_External_Links: Can't find input file $!";

# Loop over input file reading in one line at a time
my $line = <INPUT>;
while($line){
  $i1=index($line,'$key = q/');     # Identify start and end of label name in file
  $i2=index($line,'/;');
  if($i1>-1){                       # If label name exists on this line...
    $string_length=$i2-9;
    $label = substr($line,9,$string_length);     # ...isolate the label name
    }
  $i1 = index($line,'q|');          # Identify start and end of html file with the label
  $i2 = index($line,'.html|;');
  if($i1>-1){                       # If the html file name exists on this line...
    $string_length=$i2+5-$i1-2;
    $target='../' . substr($line,$i1+2,$string_length) . '#' . $label;  # ...isolate html file name
    $output_file=$output_directory . $label . '.html';                  # and tag with the label
    open OUTPUT,">  $output_file";  # Write a redirection file for the label
    print OUTPUT '<HTML>',"\n";
    print OUTPUT '<HEAD>',"\n";
    print OUTPUT '<META HTTP-EQUIV="Refresh" CONTENT="0; URL=' . $target . '">' . "\n";
    print OUTPUT '</HEAD>',"\n";
    print OUTPUT '</HTML>',"\n";
    close OUTPUT
    }
  $line=<INPUT>;                    # Read in the next line
}
