#!/usr/bin/perl


# linkDocs.pl - modifies autodoc-generated HTML files in-place, creating hyperlinks between classes.
# Michael Pelz-Sherman, Tensor Information Systems, May 1999

$docRoot = shift(@ARGV);

# Read all .html files in the current directory (excluding the top level files).
# Assume each .html file represents a class.
# Build %TREE with class name as key and file path as value.

@dirs = `find . -type d -print`;

foreach $dir (@dirs) {
	chop($dir);
	$cmd = "find $dir -name \"*.html\" -print";
	die $! unless @htmlFiles = `$cmd`;
	push(@allfiles, @htmlFiles);
}

foreach $fileNameTmp (@allfiles) {

	# avoid modifying array elements
	$fileName = $fileNameTmp;

	# files to ignore:
	next if $fileName =~ m/\+/;

	# strip leading "."
	$fileName =~ s/^\.//;

	@pathComponents = split(/\//,$fileName);
	$className = pop(@pathComponents);
	$className =~ s/\.html$//;
	$className =~ s/\s//g;

	# prepend $docRoot from command line to file names
	$TREE{$className} = $docRoot . $fileName;
}

# Replace all occurances of the keys of %TREE (together with .h or *) in every .html file 
# with a hyperlink to its class documentation.

foreach $fileName (@allfiles) {
	print STDERR "Processing file $fileName...\n";
	undef($buf);
	die $! unless open(FILE, "<$fileName");
	while (<FILE>) {
		$buf	.= $_;
	}
	close(FILE);

	foreach $key (keys(%TREE)) {
		$inheritsFromUrl = "<A HREF=$TREE{$key}>$key</A>";
		$buf =~ s/Inherits From:<\/B><BR>\s+$key/Inherits From:<\/B><BR>$inheritsFromUrl/g;

		$pointerURL = "<A HREF=$TREE{$key}>$key *</A>";
		$buf =~ s/\b$key\s*\*/$pointerURL/g;
	}

	die $! unless open(FILE, ">$fileName");
	die $! unless syswrite(FILE, $buf, length($buf));
	close(FILE);
}
