#! /bin/sh 
#
# sgml2html
# Greg Hankins, 27 August 1995
#

# Don't make any changes here, it is all done by install!
LINUXDOCBIN=linuxdocbin
LINUXDOCLIB=linuxdoclib

TABS="-8"		      # expand replaces tabs with 8 spaces
CHAR="groff"                  # use ASCII (groff) character set 
DASHI=-ifmthtml
EXT="html"
FAT=
IMG=
STYLE=
SPLIT=
LANGUAGE=

trap "CleanUp" 0 1 2 3 9 15

usage () {
echo "  Usage: sgml2html [-L language] [-2] [-fat] [-img] [-l] [-style <s>] [-t <n>] <filename> ";
echo "  -L         language for labels (next,previous,TOC)";
echo "  -2         split pages at n. and n.m. sections";
echo "  -fat       use DOS FAT .htm extensions instead of .html";
echo "  -img       use image buttons for prev/next links";
echo "  -l         use latin1 character set (default ASCII)";
echo "  -style <s> use html/<s> mapping backend in addition to default";
echo "             html/mapping";
echo "  -t <n>     tabstops each <n>th col (default 8)";
echo "  <filename> SGML source file, .sgml extension is optional";
echo 
echo "  Output will appear in <filename>.html and in <filename>-n.html for"
echo "  each section"
exit 1 
}

CleanUp() {
    rm -f /tmp/sgml2html$$tmp /tmp/sgml2html$$tmp1 /tmp/sgml2html$$tmp2
}

# check argc
if [ $# = 0 ]
then
	usage
fi

# do they need help?
case "$1" in
	"-h" | "--help" | "-help") usage
	exit 1
esac

# getopt
for i in $*
do
        case $i in
	      -L)	LANGUAGE=-L$LINUXDOCLIB/rep/html/$2; shift; shift;;
	      -2)	SPLIT="-2";shift;;
	      -fat)	FAT="-fat";EXT="htm"; shift;;
	      -img)	IMG="-img"; shift;;
	      -t) 	TABS="-"$2; shift; shift;;
	      -style) 	style=$2;STYLE=$LINUXDOCLIB/rep/html/${2}mapping; shift; shift;;
              -l)       CHAR="latin1"; shift;;
              --)       shift;
			break;;
        esac
done

# SGML_PATH for sgmls - must be exported
SGML_PATH=$LINUXDOCLIB/dtd/%N.dtd:$LINUXDOCLIB/dtd/%P.dtd:$LINUXDOCLIB/rep/html/%N
export SGML_PATH

if [ -f $LINUXDOCLIB/dtd/${style}.dcl ]; then
	SGMLDECL=$LINUXDOCLIB/dtd/${style}.dcl
elif [ -f $LINUXDOCLIB/dtd/sgml.dcl ]; then
	SGMLDECL=$LINUXDOCLIB/dtd/sgml.dcl
# else just use sgmls's defaults
fi

# check to see if there is a source file
FILE=$1
if [ -f $FILE.sgml ] 
then
	SGMLFILE=$FILE.sgml
elif [ ! -f $FILE ] 
then
	echo "sgml2html: can't find $FILE or $FILE.sgml" >&2
	exit 1
else
	SGMLFILE=$FILE
fi
HTMLROOT=`basename $SGMLFILE .sgml`

echo "Making $HTMLROOT.$EXT from $SGMLFILE."
# format
$LINUXDOCBIN/sgmls $DASHI $SGMLDECL $SGMLFILE > /tmp/sgml2html$$tmp
case $CHAR in
        latin1) cat $SGMLFILE | sed -f $LINUXDOCLIB/latin1.sed | \
                $LINUXDOCBIN/sgmls $DASHI $SGMLDECL > /tmp/sgml2html$$tmp;;
        *)      $LINUXDOCBIN/sgmls $DASHI $SGMLDECL $SGMLFILE > \
                /tmp/sgml2html$$tmp;;
esac
if [ $? = 1 ] 
then 
	echo "SGML parsing error, no formatting done..."
	exit 1
fi

# if there are no SGML parse errors, continue...
cat /tmp/sgml2html$$tmp | $LINUXDOCBIN/sgmlsasp $STYLE \
$LINUXDOCLIB/rep/html/mapping | expand $TABS > /tmp/sgml2html$$tmp1

# fix references
$LINUXDOCBIN/fixref $SPLIT < /tmp/sgml2html$$tmp1 > /tmp/sgml2html$$tmp2

# then run it through html2html to add HTML formatting
cat /tmp/sgml2html$$tmp2 /tmp/sgml2html$$tmp1 | 
sed -f $LINUXDOCLIB/prehtml.sed | $LINUXDOCBIN/html2html $LANGUAGE $FAT $IMG $SPLIT $HTMLROOT > $HTMLROOT.$EXT

exit 0
