#!/usr/athena/bin/perl

# Mike Jacknis, mjacknis@mit.edu

print "
This perl script will compile the big brainop text site:
   big.html
This file consists of a hyperlinked table of contents
followed by all the files.

I am expecting an INDEX file that lists the files to be included.
I am also expecting:
  warning.cat    --- File contents copied at the very beginning.
  header.cat     --- File contents copied at the beginning.
  contfooter.cat --- File copied after the table of contents,
                     but before the body.
  footer.cat     --- File copied after the body.

Also, if the individual files include a comment of the form:
  <! ENTRY table of contents entry>
then I will make an entry in the table of contents at that point.

";

# First we read in a whole bunch of informational text files:

open(INDEX,"INDEX") || die "I couldn't open INDEX";
open(HEAD,"header.cat") || die "I couldn't open header.cat";
open(CONTFOOT,"contfooter.cat") || die "I couldn't open contfooter.cat";
open(FOOT,"footer.cat") || die "I couldn't open footer.cat";
open(WARNING,"warning.cat") || die "I couldn't open warning.cat";

# We copy the contents of these files to array variables:
@head=<HEAD>;
@contfoot=<CONTFOOT>;
@foot=<FOOT>;
@warning=<WARNING>;

# We close most of the files:
close(HEAD);
close(CONTFOOT);
close(FOOT);
close(WARNING);

# We open the output file:
open (OUT,">big.html") || die "Couldn't open big.html for output";

while(<INDEX>) {		# MAIN LOOP... through all the filenames.
    chop;			# Get rid of ending carriage return.
    s/#.*//;			# get rid of comments
    if (/.+/) {			# Check that there is a name there.
	open (IN, $_)		# Open the input file.
	    || die "I couldn't open $_"; 
	print "opened $_ \n";	# This goes out to the USER.
	$file=$_;		# Save the filename.
	$file =~ s/^.*\///;	# Get rid of directory.
	$copy="";		# Clear the flag saying to copy the text.
	push(@body,"<a name=\"$file\">\n"); # Make an anchor.
	$number=1;		# reset the counter of anchors.
	while(<IN>) {		# Loop through the file.
	    if (/^<! *ENTRY +(.*) *>/i) { # Is this a 
				# table of contents entry?
		push(@contents,"<LI><a href=\"\#$file$number\">$1</a>\n"); 
				# Make a link in the table of contents...
	    	push(@body,"<a name=\"$file$number\">\n");
				# ...make the anchor where the link will go.
		$number++;}	# Increment the anchor counter.
	    elsif (/<BODY>(.*)/i) { # We found the start of the <BODY>
		$copy="t";	# Set the flag to begin copying text.
		push(@body,$1);} # Copy any text that may be after <BODY>
	    elsif (/(.*)<\/BODY>/i) { # We found the end... </BODY>
		$copy="";	# Clear the flag to copy text.
		push(@body,$1); } # Send off any last remaining text.
	    elsif ($copy eq "t") { # If we are copying text...
		if (m@<a +href *= *\"http://@i) {
		    # If this is an OUTSIDE link
		    push(@body,$_); } # just copy it normally
		else {
		    s@<a +href *= *\".*/(.*\.html)@<a href=\"$1@i; # get rid
				#        ^ HERE is the bug
				# This period needs to be changed
				# to mean "not a /"
		# get rid of directories
		    s@<a +href *= *\"(.*\.html)@<a href=\"\#$1@i; 
				# add in the # sign. 
		    push(@body,$_); }}}	# Send off the result.
	close(IN);}}		# Close off the .html file, try another.

close(INDEX);			# We are finished.

# NOW TO PUT THE WHOLE OUTPUT FILE TOGETHER:

print OUT @warning;		# Warns the user.
print OUT @head;		# The header
print OUT @contents;		# The table of contents.
print OUT @contfoot;		# The end of the table of contents.
print OUT @body;		# The full text of all the files...
print OUT @foot;		# The ending.

close (OUT);			# Close up the file, we're done.




