#! /usr/bin/perl

($current, $ini) = (@ARGV);

$ini = "genfile.ini" unless $ini;
$suffix = "html";

require ($ini);

$color = "#DDDDCC" unless $color;
$toctitle = "Table of Contents" unless $toctitle;
$toccols = 1 unless $toccols;
$stoptoc = 0 unless $stoptoc;

if ($current ne $combined) {
	# 1. HTML Header, Menu, Banner
	&genheader($current);
	($index eq $current) && !$stoptoc && &gentoc($current);

	# 2. Include the Body
	print "<a name=\"endnav\"></a>\n";
	open (FILE, "<$current.inc");
	while (<FILE>) {
		print;
	}
	close (FILE);

	# 3. HTML Footer, Menu
	&genfooter($current);
} else { # generate combined file
	&genheader($current);
	&gentoc($current);
	print "<a name=\"endnav\"></a>\n";
	for $item (@items) {
		($file, $label, $title) = split (/[|]/, $item);
		open (FILE, "<$file.inc");
		print "<a name=\"toc-$file\"></a>\n";
		while (<FILE>) {
			print;
		}
		close (FILE);
	}
	&genfooter();
}

##########################################################################

sub gentoc { # creates a table of contents
	local ($current) = (@_); # if missing, then make internal TOC
	local ($tocbegin) = "<ul style=\"margin-top: 0; font-size: 80%;\" >";
	local ($toc) = "<p><b>$toctitle</b></p>\n$tocbegin\n";
	local ($item);
	local ($file, $label, $title);
	local ($url); # link from item
	for $item (@items) {
		($file, $label, $title) = split (/[|]/, $item);
		if ($current ne $combined) { # external toc with links to files
			($file eq $current) && next;
			$url = "$file.$suffix";
		} else { # internal toc with links to names
			$url = "#toc-$file";
		}
		if ($labeltoc) {
			$toc .= "	<li><a href=\"$url\"><b>$label</b>: $title</a></li>\n";
		} else {
			$toc .= "	<li><a href=\"$url\">$title</a></li>\n";
		}
	}
	$toc .= "</ul>\n";
	print $toc;
}

sub genmenu { # sets $menu and $maintitle (called by header and footer)
return unless $current;
($current eq $combined) && return;
local ($current, $top) = @_; # are we at the top?
local ($options) = $top ? "name='top'" : "";
$menu = "<div align='center'>
<table summary='Navigation Menu' border='0' cellpadding='2' cellspacing='0'><tr valign='top'>
<td>
<a $options accesskey='k' href='#endnav'><img src='2x2.gif' border='0' height='1' width='1' alt='skip navigation' /></a>
</td>\n";
local ($nitems) = 0;
local ($delim) = "<td valign='middle'>|</td>\n";
for $item (@items) {
	($menu .= $delim) if $nitems++;
	($file, $label, $title) = split (/[|]/, $item);
	if ($current eq $file) {
		$menu .= "<td align='center' bgcolor=\"$color\" style='font-size: 80%'><b title=\"$title\">$label</b></td>\n";
		$maintitle = $title;
		$thisfile = $file;
	} else {
		$menu .= "<td align='center'><a href=\"$file.$suffix\" title=\"$title\"><font size='1'>$label</font></a></td>\n";
	}
}
($menu .= "$delim<td><font size='1'><a href=\"#top\">Top</a></font></td>\n") unless $top;
$menu .= "</tr></table></div>\n";
}

sub genheader
{
local ($current) = @_;
local ($logfile) = $current ? ".$current.$suffix" : ".$index.$suffix";
local ($htmlfile) = $current ? "$current.$suffix" : "$index.$suffix";
local ($mystartdate) = &getstartdate($current);
local ($leftlogocell, $rightlogocell);
if ($leftlogo) {
	$leftlogocell = "<td align='left' rowspan='2'>$leftlogo</td>";
}
if ($rightlogo) {
	$rightlogocell = "<td align='right' rowspan='2'>$rightlogo</td>";
}
&genmenu($current, 1);

print '<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';

print "<head>
<meta name=\"robots\" content=\"index,follow\" />
<meta name=\"generator\" content=\"genfile $current $ini\" />
<meta name=\"start-date\" content=\"$mystartdate\" />\n";
if ($hitcounter) {
	print "<meta name=\"accesses\" content=\"<!--#exec cmd=\"$hitcounter $logfile\" -->\" />\n";
}
local ($BOOKTITLE) = "$booktitle : " if $booktitle;

$filekeywords = $keywords{$current};
$filekeywords = $keywords unless $filekeywords;
$filekeywords =~ s/^\s*//;
$filekeywords =~ s/\s*$//;

$filedescription = $description{$current};
$filedescription = $description unless $filedescription;

print "<meta name=\"description\" content=\"$filedescription\" />
<meta name=\"keywords\" content=\"$filekeywords\" />
<!--#config timefmt=\"%Y-%m-%d\"-->
<!--#config sizefmt=\"bytes\"-->
$robot
	<title>
		$BOOKTITLE$maintitle
	</title>
	$style

</head>
<body bgcolor=\"#FFFFFF\">
";
if ($booktitle) {
	print "<table summary='Title' bgcolor='$color' width='100%' border='0' cellpadding='5' cellspacing='0'>
	<tr>
		$leftlogocell
		<td align='center'>
			<div style='font-size: 125%'><b>$booktitle</b></div>
			<div style='font-size: 90%'>$booksubtitle</div>
		</td>
		$rightlogocell
	</tr>
	<tr>
	<td align='center' style='font-size: 80%'>\n";
	&genauthors();
	print "<br/>\n$authornote\n" if $authornote;
	print "	</td>
	</tr>
</table>\n";
}
print "$menu\n";
if ($showtitle) {
	print "<h1>$maintitle</h1>\n";
	print "<p>$filedescription</p>" if ($filedescription && $current eq "index");
}
if ($showstats) {
	print "<p style='font-size: 80%'>\n";
	print "Last updated:\n";
	print "<b><!--#flastmod file=\"$htmlfile\"--></b>\n";
	# print "<br>\n";
	print "&nbsp;\n";
	print "Accesses since $mystartdate: \n";
	print "<b><!--#fsize file=\"$logfile\" --></b>\n";
	print "</p>\n";
}
if ($content{$thisfile}) {
	print "<p>$content{$thisfile}\n";
}
}

sub genauthors
{
	local ($authno) = 0;
	local ($author);
	return unless @authors;
	if ($attribution) {
		print "$attribution\n";
	} else {
		print "by\n";
	}
	for $author (@authors) {
		local ($authorname, $authorurl, $authoremail) = split(/[|]/, $author);
		print " and \n" if $authno;
		print "<font size='3'>\n";
		$authorurl && print "<a href=\"$authorurl\">";
		print "<b>$authorname</b>";
		$authorurl && print "</a>";
		print "\n";
		print "</font>\n";
		$authno++;
	}
}

sub genfooter
{
local ($current) = @_;
&genmenu($current, 0);
print "<br/>
<table summary='Page Footer' bgcolor='$color' width='100%' border='0' cellpadding='5' cellspacing='0'>
	<tr><td align='center' style='font-size: 80%'>
		$footer
	</td></tr>
</table>
$menu
</body>
</html>
";
}

sub getstartdate # file
{
	local ($target) = (@_);
	$target = "index" unless $target;
	local ($item);
	local ($file, $label, $title, $date);
	for $item (@items) {
		($file, $label, $title, $date) = split (/[|]/, $item);
		if ($file eq $target) {
			if ($date) {
				return ($date);
			} else {
				return ($startdate);
			}
		}
	}
	return ($startdate);
}
