#!/opt/PUBperl/bin/perl

#
# Makes chapter-level links to Java source code and either applet HTML
# file or output HTML file.
#
# Should run in directory to contain index.html
# Writes to stdout.
#

$PROG = $0;
if ($PROG =~ /(.*)\/([^\/]*)$/) {
        $DIRNAME = $1;
        $BASENAME = $2;
} else {
        $DIRNAME = ".";
        $BASENAME = $PROG;
}
$DIRNAME = $ENV{"PWD"} if ($DIRNAME eq ".");

&printHeader;

open(JAVASRC, 'find . -name "*.java" | sort |') || die "find: bad";
while(<JAVASRC>)
{
	chop;
	local($filename) = $_;
	$filename =~ s/^\.\///;
	local($htmlfile) = ($filename);
	$htmlfile =~ s/\.java/.html/;
	local($basename) = ($filename);
	$basename =~ s/^.*\///;
	print "<H4> $filename </H4>\n";
	print "<UL>\n";
	print "\t<li><A HREF=\"$filename\">";
	print "Source code for $basename.</A>\n";
	$basename =~ s/.java$//;
	$isApplet=`grep -c "Applet" $filename`;
	chop($isApplet);
	if (-f $htmlfile)
	{
		print "\t<li><A HREF=\"$htmlfile\">";
		if ($isApplet)
		{
			print "Run the $basename applet.";
		}
		else
		{
			print "See $basename program output results.";
		}
		print "</A>\n";
	}
	else
	{
		if ($isApplet)
		{
			print "\t<li><A HREF=\"$htmlfile\">";
			print "Run the $basename applet.";
			print "</A>\n";
			&createAppletHtmlFile($basename, $htmlfile);
		}
		else
		{
			print "\t<li> $basename is not a runnable program with output.\n";
		}
	}
	print "</UL>\n";
	$ranOnce++;
}
close(JAVASRC);

if (!$ranOnce)
{
	&noSource;
}

&printFooter;


exit 0;


sub printHeader
{
	$dir=`pwd`;
	chop($dir);
	$dir =~ s/^.*\///;
	$dir =~ s/Chapter/Chapter /;
	print qq{
<HTML>
<HEAD>
	<TITLE>
	Java 2 Programmer's Interactive Workbook: $dir
	</TITLE>
</HEAD>
<BODY BGCOLOR=#ffffff >
<H3> Java 2 Interactive Programmer's Workbook </H3>
<H2> Source code for $dir </H2>

Here is the source code to the exercises and examples for $dir.
When possible, we have linked to the corresponding applet as well.
You will need to make sure that the 
<A HREF="http://java.sun.com/products/plugin"> Java 2 plug-in </A>
is installed on your browser.
<p>
<b> Suggestion: </b> Bring up the source code in a separate browser window
so you can see the source code and the applet at the same time.  To bring up
a page in a new window, right-click on the link and select "View link in new
window."  If you have a three-button mouse, you can middle-click to do
the same thing.
<hr>

};


}

sub printFooter
{
	print "
<HR>
<FONT SIZE=1>
Authors: Kevin Chu and Eric Brower
<br>
Copyright 2000 Prentice Hall PTR
</FONT>
</BODY>
</HTML>
";

}

sub noSource
{
	print qq{
<H3> No source code for this chapter </h3>
Sorry, there is not any code for this chapter.
An code examples were either lost by the authors
or were not complete programs.
};

}

sub createAppletHtmlFile
{
	local($classname, $htmlfile) = @_;
	$applet = "$DIRNAME/Applet.html";
	open(HTML, ">$htmlfile") || die "Can't create $htmlfile.";
	open(APPLET, $applet) || die "Can't open $applet.";
	while (<APPLET>)
	{
		s/APPLETNAME/$basename/;
		print HTML;
	}
	close(APPLET);
	close(HTML);
}
