#!/usr/local/bin/perl
#
# Special features:
# 1) The item 
#	<!-- newfile filename other_words_for_title -->
#    if it occurs on a line all by itself, causes 
#    this perlscript to chop files here.  The filename
#    will be as specified.  The other_words 
#    will be used as the title.  If neither the
#    filename nor the other_words are present, then
#    this script makes up filenames using numbers, e.g.,
#    gri1.html, gri2.html.  If the filename is ".",
#    then this same naming scheme is used, but the titles
#    are used.
# 2) If the item <!-- occurs on a line all by itself, then
#    this perlscript skips until a line containing --> all
#    by itself is encountered.  This is a kludge because
#    xmosaic refuses to skip comments if a <PRE> item is
#    contained within them.
#
$debug = 0;

$num_files = 1;
die "Need 2 arg (eg chop_into_subfiles gri.hml \"The Gri graphing language\")" if ($#ARGV != 1);

$basefile = "gri";
#$file = "$basefile$num_files.html";
$file = "index.html";
$basetitle = $ARGV[1];
$title = $basetitle;
$marginnote[0] = "Gri";
$#ARGV = 0;			# Dumb way to ignore
print STDOUT "$file " if $debug;
if (open (OUT, ">", "$file")) { #|| die "Cannot open $file";
$file_list[$num_files++] = $file;

$name_of_top_node = "index.html#Top";


print OUT "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
print OUT "<html>\n";
print OUT "<head>\n";
print OUT "<title>$title</title>\n";
print OUT "<META NAME=\"author\" CONTENT=\"Dan E. Kelley\">\n";
print OUT "<META NAME=\"keywords\" CONTENT=\"gri gre science scientific graphics graph\">\n";
print OUT "<META NAME=\"description\" CONTENT=\"Gri is an extensible plotting language for producing scientific graphs, such as x-y plots, contour plots, and image plots.  It was written by a scientist and therefore has the sorts of things a scientist needs.  It is much like LaTeX, in that it rewards users who climb the learning curve, in a way that GUI-based programs do not.  Gri is a free version of the more powerful commercial program Gre (still in development).\">\n";
print OUT "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">";
print OUT "</head>\n";



#$body_start = "<body bgcolor=\"#ffffff\" link=\"#993333\" vlink=\"#333399 alink=\"#cc3333\">";
#$body_start = "<body bgcolor=\"#ffffff\" link=\"#993333\" vlink=\"#333399\">";

# Colors below from Mozilla page as of 2001-mar-05.
$body_start = "<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#0000EE\" vlink=\"#551A8B\" alink=\"FF0000\">";

print OUT "$body_start\n";

while(<>) {
    # Skip comment blocks (kludge; see above)
    if (/^\s*<!--\s*$/) {
	while(<>) {
  	    last if /^\s*-->\s*$/;
        }
	$_ = <>;
    }
    if (/^<a name\s*=\s*"([^"]*)/i){ 
	$key = $1;
	$key =~ s/ //g;
        $ref{$key} = "$file#$key";
    }
    if (/^\s*<!-- newfile\s+([^ ]*)\s+"(.*)"\s+"(.*)"\s*-->\s*$/) {
        $file = (length($1) > 0 && $1 ne ".")
	    ? $1 : "$basefile$num_files.html";
	if (length($2) > 0) {
	    $title = $2;
	} else {
	    $title = $basetitle;
	}
        if (length($3) > 0) {
	    $marginnote[$num_files] = $3;
	}
        $titleList{$file} = $title;
        print OUT "</table>
<img src=\"./resources/bottom_banner.gif\" alt=\"navigation map\" usemap=\"#navigate_bottom\" border=\"0\">
";
	close OUT;
	open (OUT, ">$file") || die "Cannot open $file";
	$file_list[$num_files++] = $file;
        print OUT "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
	print OUT "<html>\n";
	print OUT "<head>\n";
        print OUT "<title>$title</title>\n";
	print OUT "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">";
        print OUT "</head>\n";
	print OUT "$body_start\n";
        print "Creating $file\n" if $debug;
    }
    print OUT;
} 
close OUT;

for ($i = 1; $i < $num_files; $i++) {
    $file = $file_list[$i];
    $ofile = "$file.pass2";
    open (IN, "$file") || die "Cannot open $file";
    open (PASS2, ">$ofile") || die "Cannot open $ofile";
    print "$file " if $debug;
    $node_parent = "";
    $need_to_find_parent = 1;
    while(<IN>) {
        if (/\s*href\s*=\s*"#([^"]*)/i) {
            $key = $1;		# suck out key
	    $key =~ s/ //g;	# despace key
	    s,href\s*=\s*"#[^"]*,href="$ref{$key},;	# replace key with what is keyed to
            s,="#,=",;
        } elsif (/<a\s*name\s*=\s*"([^"]*)/i) {
            # <a name="foo bar">
	    $key = $1;
	    $key =~ s/ //g;
	    s,"[^"]*","$key",;
       }
       if ($need_to_find_parent && /a href="([^"]*)">parent/) {
           $need_to_find_parent = 0;
           $node_parent = $1;
       }
       # Do not print those 'Navigation' things
       if (!/^<b>Navigation<\/b>:/ 
	   && !/<a href=".*">next<\/a>,/
	   && !/<a href=".*">previous<\/a>,/
	   && !/<a href=".*">parent<\/a>/) {
	   print PASS2 if (!/href=""/);
       }
    }
    print "." if $debug;
    close IN;
    close PASS2;
    open (PASS3, ">$file.pass3") || die "Cannot open $file.pass3";
    open (PASS2, "$file.pass2")  || die "Cannot open $file.pass2";
    $need_table_cmd = 1;
    while(<PASS2>) {
        if (/^<\s*a\s*name="(.*)"\s*>/ && $need_table_cmd) {
            $need_table_cmd = 0;
            print PASS3;
	    $this_chapter = $marginnote[$i];
	    if ($this_chapter eq "") {
		$this_chapter = "Gri";
	    }
	    #print STDERR "[$file]\n";
	    print PASS3 "
<img src=\"./resources/top_banner.gif\" alt=\"navigation map\" usemap=\"#navigate_top\" border=\"0\">
<table summary=\"top banner\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td width=\"150\" valign=\"top\">
<font size=-1>
<br>
Chapters:
<br>
&nbsp;&nbsp;<a href=\"Introduction.html\">1: Introduction</a><br>
&nbsp;&nbsp;<a href=\"SimpleExample.html\">2: Simple example</a><br>
&nbsp;&nbsp;<a href=\"InvokingGri.html\">3: Invocation</a><br>
&nbsp;&nbsp;<a href=\"GettingMoreControl.html\">4: Finer Control</a><br>
&nbsp;&nbsp;<a href=\"X-Y.html\">5: X-Y Plots</a><br>
&nbsp;&nbsp;<a href=\"ContourPlots.html\">6: Contour Plots</a><br>
&nbsp;&nbsp;<a href=\"Images.html\">7: Image Plots</a><br>
&nbsp;&nbsp;<a href=\"Examples.html\">8: Examples</a><br>
&nbsp;&nbsp;<a href=\"Commands.html\">9: Gri Commands</a><br>
&nbsp;&nbsp;<a href=\"Programming.html\">10: Programming</a><br>
&nbsp;&nbsp;<a href=\"Environment.html\">11: Environment</a><br>
&nbsp;&nbsp;<a href=\"Emacs.html\">12: Emacs Mode</a><br>
&nbsp;&nbsp;<a href=\"History.html\">13: History</a><br>
&nbsp;&nbsp;<a href=\"Installation.html\">14: Installation</a><br>
&nbsp;&nbsp;<a href=\"Bugs.html\">15: Gri Bugs</a><br>
&nbsp;&nbsp;<a href=\"TestSuite.html\">16: Test Suite</a><br>
&nbsp;&nbsp;<a href=\"GriInThePress.html\">17: Gri in Press</a><br>
&nbsp;&nbsp;<a href=\"Acknowledgments.html\">18: Acknowledgments</a><br>
&nbsp;&nbsp;<a href=\"License.html\">19: License</a><br>
<br>
Indices:<br>
&nbsp;&nbsp;<a href=\"ConceptIndex.html\"><i>Concepts</i></a><br>
&nbsp;&nbsp;<a href=\"CommandIndex.html\"><i>Commands</i></a><br>
&nbsp;&nbsp;<a href=\"BuiltinIndex.html\"><i>Variables</i></a><br>
</font>
<td width=\"500\" valign=\"top\">
";
	    print "file $file\n" if $debug;
            if ($file ne "index.html") {
                print PASS3 "<map name=\"navigate_top\">\n";
		print PASS3 "<area alt=\"$name_of_top_node\" shape=\"rect\" coords=\"5,2,218,24\" href=\"$name_of_top_node\">\n";
		if ($node_parent ne "") {
		    print PASS3 "<area alt=\"$node_parent\" shape=\"rect\" coords=\"516,2,532,24\" href=\"$node_parent\">\n";
		}
		print PASS3 "<area alt=\"$titleList{$file_list[$i-1]}\" shape=\"rect\" coords=\"557,2,573,24\" href=\"",
		    $file_list[$i-1], "\">\n";
		print PASS3 "<area alt=\"$titleList{$file_list[$i+1]}\" shape=\"rect\" coords=\"581,2,599,24\" href=\"",
		$file_list[$i+1], "\">\n" if $file_list[$i+1] ne "";
		print PASS3 "</map>\n";
		print PASS3 "<map name=\"navigate_bottom\">\n";
		print PASS3 "<area alt=\"$name_of_top_node\" shape=\"rect\" coords=\"5,2,218,24\" href=\"",
	            $name_of_top_node, "\">\n";
	        print PASS3 "<area alt=\"$titleList{$file_list[$i+1]}\" shape=\"rect\" coords=\"581,2,599,24\" href=\"",
		    $file_list[$i+1], "\">\n</map>\n" if $file_list[$i+1] ne "";
            }
        } else {
            print PASS3;
        }
    }
    close(PASS2);
    print PASS3 "
</body>
</html>
";
    close(PASS3);
    rename("$file.pass3", "$file");
    print ".\n" if $debug;
}
}
