#! /usr/bin/perl -w
# (if your perl5 lives in different path, plese edit the line above.)
#
# sgml21html - convert SGML into one HTML (using sgml2html)
#
# Copyright (C) 1999 Sadaaki Kato <skato@venus.dti.ne.jp>
#                    Taketoshi Sano <kgh12351@nifty.ne.jp>
#
#     All rights reserved.
#     This is free software with ABSOLUTELY NO WARRANTY.
#
# You can redistribute it and/or modify it under the terms of 
# the GNU General Public License version 2.
#
# $Date: 2000/05/07 15:22:35 $
#
# usage: sgml21html file[.sgml]
# Please read the Notes at the end of this script
# 
require 5.004;
use strict;
use IO::File;

# Definition for Commands - Please edit to match your system
my $S2H = "sgml2html"; # used to convert SGML -> HTML initially

my @optS2H = (); # for jLinuxdoc-SGML (linuxdoc-sgml-ja) 1.5.1
# my @optS2H = ("-l", "ja", "-c", "nippon"); # for sgml-tools-ja 1.0.9

my $NKF = "nkf -w"; # used to convert JP code to UTF-8
# my $NKF = "kcc -j"; # if you don't have nkf but have kcc, use this line.

my $DATE = "date"; # used to get date output

#
my $TMPF ="./sgml21html$$.tmp.html"; # temporary file name

# $HED defines the header form. 
# 'SGML_TITLE' will be replaced by the title strings.

my $DEFAULT_HEAD = <<END_OF_HEADER;
<HTML>
<HEAD>
 <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
SGML_TITLE
</TITLE>
</HEAD>
<BODY BGCOLOR=white>
END_OF_HEADER

# $FOT defines the footer form.
# 'TODAY' will be replaced by the $DATE output.

my $DEFAULT_FOOT = <<END_OF_FOOTER;
<HR>
sgml21html conversion date: TODAY
</BODY>
</HTML>
END_OF_FOOTER

sub main() {

	my $sgml = "";
	my $name = "";
	my $ret = 0;
        my $title = "";
	my @htmls = ();
	my $html = "";
	my @stack = ();
	my $line = "";
	my $date = "";
	my $s2hvers = 0;

	if (!defined $ARGV[0]) {
		print "usage: sgml21html SGML_file[.sgml]\n";
		exit 1;
	}

	if ($ARGV[0] eq "--JFindex") {
	  $DEFAULT_HEAD .= "<A HREF=\"INDEX-JF.html\">JF-INDEX (document list of JF Project)</A>";
          shift @ARGV;
	}

	# check sgml-tools or jLinuxdoc-SGML(linuxdoc-sgml-ja)
	$s2hvers = chkvers($S2H);
	if($s2hvers != 0) {
		# for sgml-tools-ja 1.0.9
		@optS2H = ("-l", "ja", "-c", "nippon");
	}

	while (defined $ARGV[0]) {

	# initialize / reset header & footer
		my $HED=$DEFAULT_HEAD;
		my $FOT=$DEFAULT_FOOT;

	# get argument (SGML filename)
		$sgml = shift @ARGV;
		$_ = $sgml;
		s/\.sgml$//;
		s/^.*\///;
		$name = $_;
		print "NAME: " . $name . "\n";

	# use sgml2html on given filename
		if(($ret = useS2H($sgml,$name)) != 0) {
			print "Can not exec sgml2html\n";
			exit 1;
		}

	# get list of html files
		@htmls = gethtmls($name);
		if ( !defined $htmls[0] ) {
			print "Can not read html files\n";
			exit 1;
		}

	# extract title
		if(($title = extitle($sgml)) eq "") {
			print "Can not extract <title>\n";
			exit 1;
		}

	# make and printout the header
        	my $fh = new IO::File "|$NKF > $TMPF";
		$HED =~ s/SGML_TITLE/$title/;
		if ( defined $fh ) {
			print $fh $HED;
		} else {
			print "Can not write temporary file\n";
			exit 1;
		}

	# make text body
		foreach $html (@htmls) {
			@stack = maketxt($html, $name);
			if( defined $stack[0] ) {
				foreach $line (@stack) {
					print $fh $line;
				}
			} else {
				print "Can not make text. aborted\n";
				$fh->close;
				my @del = ($TMPF);
				unlink @del;
				exit 1;
			}
		}

	# add footer
		$date = getdate($DATE);
		$FOT =~ s/TODAY/$date/;
		print $fh $FOT;

	# close temporary file
		$fh->close;

	# remove htmlfiles from sgml2html
		unlink @htmls;

	# mv temporary file to target html file
		$html = $name . ".html";
		link $TMPF, $html;
		my @del = ($TMPF);
		unlink @del;

	# do loop
	}
}

main();

# use sgml2html 
sub useS2H($) {
	my ($sgml,$name) = @_;
	my $check = $sgml . ".sgml";
	if( ( -r $name ) || ( -r $sgml ) || -r "$check" ) {
		my @args = ("$S2H", @optS2H, "$sgml");
		if(system(@args) != 0) {
			print "system @args failed: $?\n";
			return -1;
		}
	} else {
		print "Invalid File specified\n";
		return 1;
	}

	return 0;
}

# extract Title
sub extitle($) {
	my ($sgml) = @_;
	my $title = "";

	my $fh = new IO::File;

	if ( -r "$sgml" ) {
		$fh->open("$sgml");
		while( <$fh> ) {
			if ( /<title>(.*)$/ ) {
		             $title = $1;
		             return $title;
       			}
		}
	} else {
		print "Can not read : " . $sgml . "\n";
		return "";
	}

	return $title;
}

# get the list of html files
sub gethtmls($) {
	my ($name) = @_;
	my @htmls = ();
	my $type = ".html";

	my $i = 0;
	my $html = $name . $type;

	while( -r "$html" ) {
		push @htmls, $html;
		$i++;
		$html = $name . "-" . $i . $type;
	}

	return @htmls;
}

# cutt headers and footers, and modify toc.
sub maketxt($$) {
	my ($html, $name) = @_;
	my $pname = $name;
	my @temp = ();
	my @stack = ();

	my $fh = new IO::File;
	my $cut = 1;
	my $i = 0;
	my $line = "";

	if ( ! -r "$html" ) {
		print "Can not read $html\n";
		return @stack;
	}

# open html
	$fh->open("$html");
	while( <$fh> ) {

	# cut header
		if ( /^<HR>/ ) {
			$cut = 0;
		}

	# push text into stack
		if ( $cut != 1 ) {
			push @temp, $_;
		}
	}
	$fh->close;

# cut footer
	for ($i=0;$i<6;$i++) {
		pop @temp;
	}

# modify toc
	$_ = $pname;
	s/\+/\\\+/g;
	$pname = $_;
	foreach $line (@temp) {
		$_ = $line;
		s/<A HREF=\"$pname-(\d+)\.html\"/<A HREF=\"\#s$1\"/;
		s/<A HREF=\"$pname-\d+\.html(\#.*)\"/<A HREF=\"$1\"/;
		s/<A NAME=\"s(\d+)\">(\d+\.)\s(.*)<\/A/<A NAME=\"s$1\">$2<\/A> <A HREF=\"#toc$1\">$3<\/A/;
		push @stack, $_;
	}

	return @stack;
}

sub getdate($) {
	my ($datecmd) = @_;
	my $date = "";

	my $fh = new IO::File;

	$fh->open("$datecmd |");
	while( <$fh> ) {
		$date = $_;
	}
        $fh->close;

	return $date;
}

sub chkvers($) {
	my ($s2hcmd) = @_;
	my $s2hvers = 0;
	my $chkline = "";

	my $fh = new IO::File;

	$fh->open("$s2hcmd |");
	while( <$fh> ) {
		$chkline = $_;
		if(/^SGML-Tools version 1.0.9$/) {
			$s2hvers = 1;
		}
	}
        $fh->close;

	return $s2hvers;
}

# Note:
#
# $Log: sgml21html,v $
# Revision 1.17  2000/05/07 15:22:35  takes
# guidance-sgml.m4: update some description
# archives/sgml21html: add "--JFindex" option.
#
# Revision 1.16  2000/02/23 01:27:33  morimoto
#
# Wed Feb 23 10:26:22 2000  Jun Morimoto  <morimoto@xantia.citroen.org>
#
# 	* workshop/archives/sgml21html:  [Back] Υ
#         INDEX-JF.html ѹ
#
# Revision 1.15  2000/01/11 15:20:04  takes
# more modification to escape "+" in filename
#
# Revision 1.14  1999/12/05 03:28:02  sano
# modify "sub maketext($$)" to handle label/ref correctly
#
# Revision 1.13  1999/09/07 04:23:45  sano
# add "charset=ISO-2022-JP" as a META tag in default header
#
# Revision 1.12  1999/08/19 07:00:49  sano
# use "$name" variable to distinguish toc related & others
#
# Revision 1.11  1999/06/27 15:22:34  sano
# added one line to correct the <rabel> <ref id> tag handling
# (thanks to fukushima-san, and hayakawa-san)
#
# Revision 1.10  1999/05/24 04:31:38  sano
# update the bottomline Notation
#
# Revision 1.9  1999/05/24 04:27:49  sano
#  add version check rutine "chkvers" to make enable using
#  with ether jLinuxdoc-SGML (linuxdoc-sgml-ja) 1.5.1 or
#  sgml-tools-ja 1.0.9
#
# Revision 1.8  1999/05/24 03:49:27  sano
# add comments for use with jLinuxdoc-SGML 1.5.1,
# and use with kcc.
#
# Revision 1.7  1999/05/24 01:21:02  sano
#  update to be usable with sgml-tools-ja 1.0.9, and
#  fix bug in extitle (add return to avoid the repeating match)
#
# Revision 1.6  1999/04/30 12:54:02  sano
# change <toc> modify code to handle the name including "-"
# 
# Revision 1.5  1999/03/20 18:43:06  sano
# add comments, and header and footer initialization
# 
# v1.4 - remove absolute path for commands,
# v1.4 -  and replace die to return -1 in useS2H
# v1.4 -
# v1.3 - add and modify error recovery in main loop
# v1.3 -
# v1.2 - Fix main loop bug.
# v1.2 -
# v1.1 - Initial Version. announce to JF ML
# v1.1 -
# 
# You need:
#
# - sgml-tools 1.0.9 or jLinuxdoc-SGML (linuxdoc-sgml-ja) 1.5.1
#    if you get trouble with automatic version checking,
#    use the fixed @optS2H line and comment out
#    the call for chkvers($S2H).
#
# - perl 5.004 (or later) as "/usr/bin/perl",
#    if you don't have it at that place, 
#    please change the first line to point the perl 5.004.
#
# - nkf (of kcc with the modification of this script)
#    if you use kcc, use commented out $NKF line.
#
# - date (this is standard command, so you should have it)
#
