#!/usr/bin/perl

# Given an HTML file and a list of names, add hyperlinks to all
# occurances of each name in the list.
#
# David Edwards <edwards@dee-engineering.com> Wed Jan 24 21:33:15 EST 2001
# Ver 1.1 - Sat Jun  9 21:56:16 EDT 2001

$SRC_EXT='.c';
$HTML_EXT='.html';
$EMAIL='edwards@dee-engineering.com';

#
# Check for proper number of arguments on command line.
#

if ( scalar @ARGV < 2) {

    $prog_name = $0;
    $prog_name =~ s/^.*\///;

# Do :set ts=8 before modifying the below help
print << "END";

Usage: $prog_name name_list source_file(s)...

  name_list 
    Path spec of a file with one name per line.  The source 
    files are searched for all names in name_list and made 
    into hyper links.
 
  source_file(s)...
    List of one or more HTML files to serach through.

Exmample:

  $prog_name dependency.lst *.htm?

    This example will search all HTML for each name in
    "dependency.lst" change it from "name" to:

        <a href="name.c.html">name<\\a>

Each occurance of $EMAIL is also changed 
to a mailto tag.

NOTE: If an entry in name_list has a period in it, then
"$SRC_EXT" will not be appended to the link name.

END

    exit 1;
}

#
# Read in the name list
#
open(IN,"$ARGV[0]") || die "Unable to open $ARGV[0]: $!";

while (<IN>) {
    chomp($_);
    push(@names,$_);
}

close(IN) || warn "Problem closing $ARGV[0]: $!\n";

shift(@ARGV);       # Discard uneeded name_list

#
# Search through all the input files
#

foreach $fname (@ARGV) {
    print "Processing $fname\n";

    unless (open(IN,"$fname")) {
        warn "Unable to open $fname: $!";
        next;
    }
    open(OUT,">$fname.tmp") || die "Unable to create $fname.tmp: $!";

    while (<IN>) {
        foreach $name (@names) {
          if ($name =~ /\./) {
              s/(\b)$name(\b)/$1<a href="$name$HTML_EXT">$name<\/a>$2/g;
          } else {
              s/(\b)$name(\b)/$1<a href="$name$SRC_EXT$HTML_EXT">$name<\/a>$2/g;
          };
        }
        s/\Q$EMAIL\E/<a href="mailto:$EMAIL">$EMAIL<\/a>/g;
        print OUT;
    }
    close(IN);
    close(OUT);
}