#! /bin/sh 
#
# sgml2ps
# Greg Hankins, 27 August 1995
#
# Based on the original 'format' and 'qtex' scripts by Tom Gordon 
# and Alexander Horz.
# 
# This will fail if you give it an option, but no source file. 
#

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

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

# set and export TEXINPUTS
TEXINPUTS=":$LINUXDOCLIB"
export TEXINPUTS

TABS="-8"		      # expand replaces tabs with 8 spaces
CHECK="no"		      # don't just check SGML syntax
DVI="no"
SAVE="no"
CHAPTER="no"
VERBOSE="yes"
SUFFIXES="log blg aux toc lof lot log dlog bbl tex"
CHAR="nroff"                  # use ASCII (nroff) character set

trap 'cleanup; exit 1' 1 2 3 9

usage () {
echo "  Usage: sgml2ps [-Ccdsv] [-t <n>] <filename> ";
echo "	-C	   single chapter";
echo "	-c	   check syntax only";
echo "	-d	   dvi output instead of PostScript";
echo "  -l         use latin1 character set (default ASCII)";
echo "	-s	   save all temporary files";
echo "	-t <n>	   tabstops each <n>th col (default 8)";
echo "	-v	   verbose";
echo "	<filename> SGML source file, .sgml extension is optional";
echo 
echo "  Output will appear in <filename>.ps (or <filename>.dvi if -d)"
exit 1 
}

announce () {   
	if [ $VERBOSE = "yes" ]
	then
		echo  $1 >&2
	fi
}

LaTeX () {   
        latex $1 #> /dev/null
        if [ $? != 0 ]    # some TeX error
        then
                echo sgml2ps: LaTeX error. See $1.log  >&2
                mv $1.log /tmp/x$$.log
                cleanup
                mv /tmp/x$$.log $1.log
                exit 1
        fi
}

cleanup () {
	if [ $SAVE = "no" ] 
	then
		for SFX in $SUFFIXES
		do
			if [ -f $FILE.$SFX ]
			then
				rm $FILE.$SFX
			fi
		done
		rm -f /tmp/sgml2ps$$tmp
	fi
	if [ -f /tmp/$$.tex ]
	then
		mv /tmp/$$.tex $FILE.tex
	fi
}

# 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
	      -c)	CHECK="yes"; shift;;
	      -C)       CHAPTER="yes"; shift;;
	      -d)	DVI="yes"; shift;;
              -l)       CHAR="latin1"; shift;;
	      -s)    	SAVE="yes"; shift;;
	      -t) 	TABS="-"$2; shift; shift;;
	      -v)	VERBOSE="yes"; shift;;
              --)       shift;
			break;;
        esac
done

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

# do we want to just check the file?
if [ $CHECK = "yes" ]
then
	$LINUXDOCBIN/sgmls -s $SGMLFILE > /dev/null
	if [ $? = 1 ] 
	then 
		exit 1
	else
		exit 0
	fi
fi

echo "Making $FILE.dvi from $SGMLFILE."
# format 
$LINUXDOCBIN/sgmls $SGMLFILE > /tmp/sgml2ps$$tmp
if [ $? = 1 ]
then
	echo "SGML parsing error, no formatting done..."
	exit 1
fi

# if there are no SGML parse errors, continue...
case $CHAR in
	latin1) cat /tmp/sgml2ps$$tmp | sed -f $LINUXDOCLIB/latin1.sed | \
		$LINUXDOCBIN/sgmlsasp $LINUXDOCLIB/rep/latex/mapping | \
		expand $TABS > $FILE.tex;;
	*) 	cat /tmp/sgml2ps$$tmp | $LINUXDOCBIN/sgmlsasp \
		$LINUXDOCLIB/rep/latex/mapping | expand $TABS > $FILE.tex;;
esac

if [ $CHAPTER = "yes" ]
then
	announce "Creating report from a chapter."
	mv $FILE.tex /tmp/$$.tex
	awk -f $LINUXDOCLIB/chapt.awk /tmp/$$.tex > $FILE.tex
fi

# must LaTeX 3 times to get references right
announce "LaTeXing..."
/bin/rm -f $FILE.dvi
LaTeX $FILE.tex
announce "LaTeXing again... "
LaTeX $FILE.tex
announce "And again... "
LaTeX $FILE.tex

# translate to PS
if [ $DVI = "no" ]
then 
	SUFFIXES="$SUFFIXES dvi"
	announce "Translating $FILE.dvi to PostScript."
	dvips -q -o $FILE.ps $FILE.dvi
fi

cleanup

exit 0
