#! /usr/bin/env perl
#
# Copyright (c) 2001 Tama Communications Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# usage: makehtml.pl [-v] skelton-file ...
#
# skelton-file:
# +-------------------------------
# |@title <title for html>
# |@link <link name for anchor>
# |@license
# |<license for this file>
# |@body
# |...
############################################################
# Definitions
############################################################
#
# String
#
$l_year = '2005, 2006';
$l_company = 'Tama Communications Corporation';
$l_copyright = 'Copyright (c)';
$l_statement = "$l_copyright $l_year $l_company";
$l_biglogo = 'bokin.png';
$l_smalllogo = 'sbokin.png';
$l_GFDL = 'Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2';
$host = "http://www.tamacom.com";
#
# Color
#
$c_bgcolor	= '#ffffcc';
$c_text		= '#191970';
$c_link		= '#1f00ff';
$c_alink	= '#ff0000';
$c_vlink	= '#7B68EE';
$c_menucolor	= '#ffff99';
$c_titlecolor	= 'blue';
$c_titlebgcolor = 'yellow';
$c_logocolor	= 'red';
$c_logobgcolor	= '#336666';
$c_linecolor    = 'grey';
# Size
$w_menuwidth	= '100';
$hr		= "<hr width=100% size=3 align=left noshade color=$c_linecolor>\n";
#
# Parts
#
sub start_html {
	"<!doctype html public \"-//W3C//DTD HTML 3.2 Final//EN\">\n" .
	"<html>\n" .
	"<head>\n" .
	"<title>$_[0]</title>\n" .
	"<meta name='keywords' content='bokin,bookkeeping'>\n" .
	"<meta name='description' content='BOKIN is an accounting system based on double-entry bookkeeping.'>\n" .
	"</head>\n" .
	"<body bgcolor=$c_bgcolor text=$c_text link=$c_link alink=$c_alink vlink=$c_vlink>\n";
}
sub end_html {
	"</body></html>\n";
}
sub home_logobox {
	my($title, $fontsize, $logo) = @_;
	"<table width='100%' cellpadding=0 cellspacing=0 border=1>\n" .
	"<tr>\n" .
		"<td bgcolor=$c_logobgcolor width='30%'>\n" .
			"<img src=$logo alt='[logo of BOKIN]' align=right vspace=5 hspace=15>\n" .
		"</td>\n" .
		"<td bgcolor=$c_titlebgcolor align=center valign=middle>\n" .
			"<font color=$c_logocolor size=$fontsize>$title</font>\n" .
		"</td>\n" .
	"</tr>\n" .
	"</table>\n";
}
sub logobox {
	my($title, $fontsize, $logo) = @_;
	"<table width='100%' cellpadding=0 cellspacing=0 border=1>\n" .
	"<tr>\n" .
		"<td bgcolor=$c_titlebgcolor align=left valign=middle width='80%'>\n" .
			"<font color=$c_logocolor size=$fontsize>$title</font>\n" .
		"</td>\n" .
		"<td bgcolor=$c_logobgcolor>\n" .
			"<img src=$logo alt='[logo of BOKIN]' align=right vspace=2 hspace=15>\n" .
		"</td>\n" .
	"</tr>\n" .
	"</table>\n";
}
sub title {
	"<h2><font color=$c_titlecolor>$_[0]</font></h2>\n";
}
sub home_title {
	home_logobox("<h2>$_[0]</h2>", 4, $l_biglogo) .
	"<p align=right><i><font size=-1>\n" .
	"$l_copyright $l_year $l_company" .
	"</font></i></p>\n";
}
sub copyright {
	my($msg) = $l_statement;
	$msg .= '. All rights reserved.';
	return logobox($msg, -1, $l_smalllogo);
}
sub get_version {
	my($infile) = 'version.texi';
	my($val);

	open(INFILE, "$infile") || die("cannot open '$infile'\n");
	while (<INFILE>) {
		if (/^\@set VERSION (.*)$/) {
			$val = $1;
		}
	}
	close(INFILE);
	if (!$val) {
		die("Version number not found in '$infile'\n");
	}
	$val;
}
# End of definitions
############################################################
while ($ARGV[0] =~ /^-/) {
	$opt = shift;
	if ($opt eq '-v') {
		$verbose = 1;
	}
}
$version = get_version();
$total = @ARGV;
#
# Making %outfile, %title, %link, %body for each @ARGV.
#
for ($i = 0; $i < $total; $i++) {
	my($infile, $outfile, $skip);

	$infile = $outfile = $ARGV[$i];
	$outfile =~ s/\.[^.]+/.html/;
	$outfile{$infile} = $outfile;
	open(INFILE, "$infile") || die("cannot open '$infile'\n");
	$license = 0;
	while (<INFILE>) {
		if ($license) {
			if (/^\@/) {
				$license = 0;
			} else {
				next;
			}
		}
		if (/^\@body/) {
			last;
		} elsif (/^\@title (.*)$/) {
			$title{$infile} = $1;
		} elsif (/^\@link (.*)$/) {
			$link{$infile} = $1;
		} elsif (/^\@license/) {
			$license = 1;
		} else {
			die("illegal skelton file format.($infile)\n");
		}
	}
	if (!defined($title{$infile})) {
		die("\@title not found.\n");
	}
	if (!defined($link{$infile})) {
		die("\@link not found.\n");
	}
	my(@body) = <INFILE>;
	foreach (@body) {
		s/\@VERSION\@/$version/g;
	}
	$body{$infile} = \@body;
	close(INFILE);
}
#
# Generating html files.
#
for ($i = 0; $i < $total; $i++) {
	my($infile) = $ARGV[$i];

	print STDERR "Generating $outfile{$infile} ...\n" if ($verbose);
	open(OUTFILE, ">$outfile{$infile}") || die("cannot open '$outfile{$infile}'\n");

	# (1) Print prologue.
	print OUTFILE start_html($title{$infile});
	print OUTFILE "<!-- $outfile{$infile} generated automatically by makehtml.pl from $infile. -->\n";
#	print OUTFILE "<small>\n";
#	print OUTFILE "<a href='http://tamacom.com/'>Tama Communications Corporation</a>\n";
#	print OUTFILE "<a href='http://savannah.nongnu.org/'>Savannah</a> -\n";
#	print OUTFILE "<a href='http://savannah.nongnu.org/projects/bokin/'>BOKIN</a>\n";
	print OUTFILE "=>  <a href='$host/index.html'>Home</a> > <a href='$host/software.html'>Free Software</a> > <a href='$host/bokin.html'>BOKIN</a> > $link{$infile}<br>\n";
#	print OUTFILE "</small>\n";
	print OUTFILE "<table width='100%' cellpadding=10 cellspacing=0 border=0>";
	print OUTFILE "<tr>\n";

	# (2) Print menu.
	print OUTFILE "<td valign=top align=center bgcolor=$c_menucolor width=$w_menuwidth>\n";
	print OUTFILE "<b>\n";
	foreach $f (@ARGV) {
		print OUTFILE "<p>";
		print OUTFILE "<hr><p>\n" if ($f eq 'model.in');
		print OUTFILE "<a href='$outfile{$f}'>" if ($f ne $infile);
		print OUTFILE $link{$f};
		print OUTFILE "</a>" if ($f ne $infile);
		print OUTFILE "\n";
	}
	print OUTFILE "</b>\n";
	print OUTFILE "</td>\n";


	# (3) Print body.
	print OUTFILE "<td valign=top>\n";
	if ($infile eq 'bokin.in') {
		print OUTFILE home_title($title{$infile});
	} else {
		print OUTFILE $hr;
		print OUTFILE title($title{$infile});
	}
	print OUTFILE $hr;
	print OUTFILE @{$body{$infile}};
	print OUTFILE "</td>\n";

	print OUTFILE "</tr>\n";
	print OUTFILE "<tr>\n";

	print OUTFILE "<td valign=center align=center bgcolor=$c_menucolor width=$w_menuwidth>\n";
	print OUTFILE "<a href='#top'>[Top of page]</a>\n";
	print OUTFILE "</td>\n";
	print OUTFILE "<td>\n";
	print OUTFILE $hr;
	print OUTFILE copyright();
	print OUTFILE "</td>\n";

	# (4) Print epilogue.
	print OUTFILE "</tr>\n";
	print OUTFILE "</table>\n";
	print OUTFILE end_html;

	close(OUTFILE);
}
