#! /usr/bin/perl -w
#
# $Id: fixhtml,v 1.8 2003/09/25 03:30:21 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]);

# Bring in other files.
print "Renaming index.html to Table-of-Contents.html.\n";
rename("index.html", "Table-of-Contents.html") or die;
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();

# Link to a well-known files that are used by the rest of the MH book.
print "Linking tour.html to Tour-Through-MH-E.html.\n";
symlink "Tour-Through-MH-E.html", "tour.html";
print "Linking getmhe.html to Getting-MH-E.html.\n";
symlink "Getting-MH-E.html", "getmhe.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;
	    s/index.html/Table-of-Contents.html/;
	}
	continue {
	    print;
	}
	close(HTML) or die;
	close(HTMLOUT) or die;

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