#! /usr/bin/perl -w
#
# $Id: fixhtml,v 1.5 2001/12/29 18:31:55 wohler Exp $
#
# NAME
#   fixhtml - prepare mh-e's HTML documentation
#
# SYNOPSIS
#   fixhtml
#
# DESCRIPTION
#   This program fixes up the output of texi2html specifically for mh-e.
#
# OPTIONS
#
# RETURNS
#
# BUGS
#
# AUTHOR
#   Copyright 1999,2001 Bill Wohler <wohler@newt.com>, Newt Software

use strict;

#
# Initializations (internal variables that need to be set to something).
#
select((select(STDOUT), $| = 1)[0]);

# Loop through HTML files, looking for text that indicates the needed file
# and link a well-known file to it which is used by the rest of the MH
# book.
print "Creating links...\n";
my ($tour, $last, $getmhe, $conidx, $varidx, $comidx) = (0);

while (<*.html>) {
    my $current_file = $_;
    open HTML, $current_file;
    while (<HTML>) {
	if (/<TITLE>mh-e( -|:) Tour Through mh-e<\/TITLE>/) {
	    print "  Linking tour.html to $current_file.\n";
	    symlink $current_file, "tour.html";
	    $tour = 1;
	}
	elsif (/<TITLE>mh-e( -|:) Concept Index<\/TITLE>/) {
	    print "  Linking last.html to $current_file.\n";
	    symlink $current_file, "last.html";
	    $last = 1;

	    print "  Linking con_idx.html to $current_file.\n";
	    symlink $current_file, "con_idx.html";
	    $conidx = 1;
	}
	elsif (/<TITLE>mh-e( -|:) Getting mh-e<\/TITLE>/) {
	    print "  Linking getmhe.html to $current_file.\n";
	    symlink $current_file, "getmhe.html";
	    $getmhe = 1;
	}
	elsif (/<TITLE>mh-e( -|:) Command Index<\/TITLE>/) {
	    print "  Linking com_idx.html to $current_file.\n";
	    symlink $current_file, "com_idx.html";
	    $comidx = 1;
	}
	elsif (/<TITLE>mh-e( -|:) Variable Index<\/TITLE>/) {
	    print "  Linking var_idx.html to $current_file.\n";
	    symlink $current_file, "var_idx.html";
	    $varidx = 1;
	}
    }
    close HTML;
}
print "Failed to create tour.html link.\n" if ($tour == 0);
print "Failed to create last.html link.\n" if ($last == 0);
print "Failed to create getmhe.html link.\n" if ($getmhe == 0);
print "Failed to create con_idx.html link.\n" if ($conidx == 0);
print "Failed to create var_idx.html link.\n" if ($varidx == 0);
print "Failed to create com_idx.html link.\n" if ($comidx == 0);

print "Creating links...done\n";

# Bring in other files.
print "Copying index.html.\n";
system "cp ../index.html .";
print "Copying indexes.html.\n";
system "cp ../indexes.html .";

# Fix up various HTML things.
print "Playing around with Texinfo output.\n";
fix_texinfo_html();

# Set the mode to read-only to be consistent with other files and because
# Jerry has a script that checks the mode of the index.html file.
chmod 0444, <*.html>;


sub fix_texinfo_html {
    while (<*.html>) {
	my $current_file = $_;
	open(HTML, $current_file) or die;
	open(HTMLOUT, ">.$current_file") or die;
	select(HTMLOUT);

	while (<HTML>) {
	    s/<BODY>/<BODY BGCOLOR="#FFFFFF">/i;
	}
	continue {
	    print;
	}
	close(HTML) or die;
	close(HTMLOUT) or die;

	select(STDOUT) or die;
	rename(".$current_file", "$current_file") or die;
    }
}
