#!/usr/bin/perl
# ISDK - Information System Development Kit
# Copyright (C) 1998-1999 Szilard Hajba, Peter Vazsonyi
#
# 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 of
# the License, 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
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

require "getopts.pl";

if (Getopts("hD:")) {
    if ($opt_h) {
	print STDERR "Usage: $0 [-h] [-D dir] file [...]\n";
    } else {
	while ($#ARGV) {
	    $opt_D =~ s#/*$#/# if ($opt_D);
	    $state_pre=0;
	    open IN, "<".shift(@ARGV);

	    while (<IN>) {
		if (/^[ \/]\*(.)(.*)$/) {
		    $com=$1; $zilu=$2;
		    $zilu =~ s/\$(\w+)/${'VAR::'.$1}/g;
		    $zilu =~ s/</\&lt;/g;
		    $zilu =~ s/>/\&gt;/g;
		    if (($com eq ':') && (!$state_pre)) {
			print OUT "<PRE>\n"; $state_pre=1;
		    } elsif (($com  ne ':') && ($state_pre)) {
			print OUT "</PRE>\n"; $state_pre=0;
		    }
		    if ($com eq '$') {
			$zilu =~ /(\w+)\W.*"(.*)"/;
			${"VAR::$1"}=$2;
		    } elsif ($com eq '+') {
			#$zilu =~ /(\w+)/;
			print OUT "</P>\n</BODY>\n</HTML>\n";
			close OUT; open OUT, ">$opt_D"."$zilu.html";
			printf OUT "<HTML>\n<BODY>\n";
			print OUT "<FONT size=+3><TABLE width=\"100%\"><TR><TD>$zilu($VAR::mansection)</TD><TD>$VAR::title</TD><TD>$zilu($VAR::mansection)</TD></TR></TABLE></FONT><BR>\n";
		    } elsif ($com eq '=') {
			print OUT "</P><A name=\"$zilu\"><FONT size=+2>$zilu</FONT><P>\n";
		    } elsif ($com eq '-') {
			print OUT "</P><A name=\"$zilu\"><FONT size=+1>$zilu</FONT><P>\n";
		    } elsif ($com eq '.') {
			print OUT "<BR><BR><B>$zilu</B><BR>\n";
		    } elsif ($com =~ /[ \t:]/) {
			$zilu =~ s/^\t/    /;
			$zilu =~ s/\\[Ll]{([^}:]*):([^}:]*)}/<A href="#$2">$1<\/A>/g;
			$zilu =~ s/\\[Ll][Pp]{([^}:]*):([^}:]*)}/<A href="$2.html">$1<\/A>/g;
			$zilu =~ s/\\[Bb]{([^}]*)}/<B>$1<\/B>/g;
			$zilu =~ s/\\[Ii]{([^}]*)}/<I>$1<\/I>/g;
			print OUT "$zilu\n";
		    }
		} elsif (/^[ \/]\*$/) {
		    print OUT "<BR>\n";
		}
	    }
	    print OUT "</P>\n</BODY>\n</HTML>\n";
	}
	close OUT;
    }
} else {
    exit 1;
}
