#
# SCCS:		%W% %G% %U%
#
# Name:		html_mklinks.pl
#
# Description:	A perl script to construct a table of links in an HTML file.
#		
#		Each input file is processed, replacing the text between lines
#		containing STARTLINKS and ENDLINKS. The new text consists of HTML
#		rows containing hyperlinks to selected files which are related to
#		the file being processed (and so may be of interest to the reader). 
#		So if the file names are changed or new files added, the hyperlinks
#		in all the files can be reconstructed just by re-running this script.
#
#		Its assumed that the user will prefix HTML file names with information
#		defining how they wish the HTML rows to be related and ordered.
#		The prefix consists of a sequence of digits separated by underscores.
#		The number of underscores in the prefix defines the level of the page.
#		(No underscores should appear except in the prefix to the page name.)
#		Candidate files are related to an input file if their prefix, when the 
#		final digits are stripped off, matches the chosen file's prefix with its 
#		final digits also stripped off and truncated to the length of the 
#		candidate file's prefix if necessary.
#		(Level one files are always related to every other as a consequence.)
#
#		Candidate files are processed in normal collating sequence order. 
#		Between the final underscore and the rest of the file name, its a good idea
#		to insert a few zeros. This ensures the file name appears before any 
#		lower level related file names, for example:
#	
#		10_00projectintro.html		Level 1
#		10_10_00project1.html		Level 2
#		10_20_00project2.html		Level 2
#
# Author:	DPJ Cater
#
# History:
#-----------------------------------------------------------------------------
#  Date		Modifications
#  04/03/2000	Initial version for Linux
#  21/04/2000	Tested on DOS
#-----------------------------------------------------------------------------

select STDOUT; $| = 1;
select STDERR; $| = 1;

#
# Process any options.
#
use Getopt::Std;
getopt('io');

if(defined($opt_i) && ($opt_i ne "")) {
    $i_dir = $opt_i;
} else {
    $i_dir = ".";
}
print "Input directory $i_dir\n";

chdir($i_dir) or die "Can't change to directory $i_dir";

if(defined($opt_o) && ($opt_o ne "")) {
    $newdir = $opt_o;
} else {
    $newdir = "/tmp";
}
print "Output directory $newdir\n";

while ($file = shift) {
    @file_sects = ();
    @file_sects = split("_", $file);
    $level = $#file_sects;
    print "file $file; level $level\n";
    
    if(!open(IFILE, "<$file")) {
        die "open(IFILE, $file) failed; $!\n";
    }
    if(!open(OFILE, ">$newdir/$file")) {
        die "open(OFILE, $newdir/$file) failed; $!\n";
    }
    $dolinks = 0;
    while(<IFILE>) {
#
#       Found ENDLINKS - print this and everything following it unchanged.
#
        if(/ENDLINKS/) {
            $dolinks = 0;
        }
        if(!$dolinks) {
            print OFILE;
        }
#
#       Found STARTLINKS and printed it - set flag to say we're formatting links and
#       ignore following lines up to ENDLINKS.
#
        if(/STARTLINKS/) {
            $dolinks = 1;
            opendir(DIRH, ".") or die "Can't opendir(.)";
            @html_files = sort grep { /^[0-9].*\.html$/ } readdir(DIRH);
            closedir(DIRH);  
LINKFILE:   foreach (@html_files) {
                chomp;
                $linkfile = $_;
                @linkfile_sects = ();
                @linkfile_sects = split("_", $linkfile);
                $linklevel = $#linkfile_sects;
                for($i = 0; $i < $linklevel-1; $i++) {
#
#                  Reject links more than one level below the input file.
#
                    if($i > $level - 1) {
                        next LINKFILE;
                    }
#
#                  Reject links for different sections.
#
                    if($linkfile_sects[$i] ne $file_sects[$i]) {
                        next LINKFILE;
                    }
                }
#
#               Try to open the link file and find its title.
#
                if(!open(LFILE, "<$linkfile")) {
                    die "open(LFILE, $linkfile) failed; $!\n";
                }
#
#               By default, use its file name.
#
                $linktitle = $linkfile;
                while(<LFILE>) {
                    if(/<[Tt]itle>/) {
                        chomp;
                        $linktitle = $_;
                        $linktitle =~ s/<.*?>//g;
                        $linktitle =~ s/\015//g;
                    }
                }
                close(LFILE);
#
#               Now output all the HTML to replace that between STARTLINKS and ENDLINKS.
#               Notice output is in DOS format with carriage return characters!
#               This is just because I want to be able to handle the files in Windows.
#
            $fontstr = " bgcolor=\"#00FFFF\"";
            if($linkfile eq $file) { 
                $fontstr = " bgcolor=\"#00CCCC\"";
            }
            print OFILE "<tr", $fontstr, "><td><h6><a href=\"$linkfile\"><font face=\"Arial\">\r\n";
            print OFILE "$linktitle\r\n";
            print OFILE "</font></a></h6></td></tr>\r\n";
            }
        }
    }
    close(OFILE);
    close(IFILE);
}

exit 0;
