#! /bin/sh 
#
# sgml2latex
# Greg Hankins, 2 November 1995
#
# Based on the original 'format' and 'qtex' scripts by Tom Gordon 
# and Alexander Horz.
# 

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

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

TABS="-8"		      # expand replaces tabs with 8 spaces
DVI="no"                      # no DVI
TEX="yes"		      # just output TEX file
PS="no"			      # default is to create ps file
SAVE="no"
CHAPTER="no"
VERBOSE="yes"
SUFFIXES="log blg aux toc lof lot log dlog bbl"
CHAR="groff"                  # use ASCII (groff) character set
PAGE="letter"                 # default page layout
STYLE=                        # Use standard Linuxdoc-SGML style
DASHI=-ifmttex
LATEX="latex"
GERMAN="no"

trap 'cleanup; exit 1' 1 2 3 9

usage () {
echo "  Usage: sgml2latex [-2e] [-aCdglps] [-style <s>] [-t <n>] <filename> ";
echo "  -2e        use LaTeX2e instead of LaTeX 2.09";
echo "  -a         DIN A4 page layout (default US letter)";
echo "  -C         single chapter";
echo "  -d         DVI output instead of LaTeX";
echo "  -l         use latin1 character set (default ASCII)";
echo "  -g         use german.sty";
echo "  -p         PostScript output instead of LaTeX";
echo "  -s         save all temporary files";
echo "  -style <s> use latex/<s>mapping backend in addition to default";
echo "             latex/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>.tex for LaTeX output,"
echo "  <filename>.dvi for DVI output, or <filename>.ps for"
echo "  PostScript output"
exit 1 
}

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

LaTeX () {   
        $LATEX $1 #> /dev/null
        if [ $? != 0 ]    # some TeX error
        then
                echo sgml2latex: 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/sgml2latex$$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
	      -2e)      LATEX="latex2e"; shift;;
	      -a)	PAGE="a4"; shift;;
	      -C)       CHAPTER="yes"; shift;;
	      -d)	DVI="yes"; TEX="no"; shift;;
	      -g)       GERMAN="yes"; shift;;
              -l)       CHAR="latin1"; shift;;
	      -p)	PS="yes"; TEX="no"; shift;;
	      -s)    	SAVE="yes"; shift;;
              -style)   style=$2; shift; shift;;
	      -t) 	TABS="-"$2; shift; shift;;
	      -v)	VERBOSE="yes"; shift;;
              --)       shift;
			break;;
        esac
done

# if we want only TeX file no DVI or PS file is created
if [ $TEX = "yes" ]
then
	DVI="no"
	PS="no"
fi
if [ $DVI = "yes" ]
then
	PS="no"
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 "sgml2latex: can't find $FILE or $FILE.sgml" >&2
	exit 1
else
	SGMLFILE=$FILE
fi
FILE=`basename $SGMLFILE .sgml`

if [ "$style" != "" ]; then
        STYLE=$LINUXDOCLIB/rep/$LATEX/${style}mapping
fi

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

# for sgmls, must be exported
SGML_PATH=$LINUXDOCLIB/dtd/%N.dtd:$LINUXDOCLIB/dtd/%P.dtd:$LINUXDOCLIB/rep/${STYLE}$LATEX/%N
export SGML_PATH



if [ $TEX = "yes" ]
then
	echo "Making $FILE.tex from $SGMLFILE."
elif [ $DVI = "yes" ]
then
	echo "Making $FILE.dvi from $SGMLFILE."
else
	echo "Making $FILE.ps from $SGMLFILE."
fi

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

# if there are no SGML parse errors, continue...
case $PAGE in
      a4)	cat /tmp/sgml2ps$$tmp | $LINUXDOCBIN/sgmlsasp $STYLE \
		$LINUXDOCLIB/rep/$LATEX/mapping | expand $TABS | \
                sed '/^\\documentstyle/ s/linuxdoc-sgml/linuxdoc-sgml-a4/g' \
		> $FILE.tex;;
      *)	cat /tmp/sgml2ps$$tmp | $LINUXDOCBIN/sgmlsasp $STYLE \
      		$LINUXDOCLIB/rep/$LATEX/mapping | expand $TABS > $FILE.tex;;
esac

if [ $GERMAN = "yes" ]
then
       mv $FILE.tex /tmp/$$.tex
       sed 's/\(documentstyle\[linuxdoc-sgml,\)/\1german,/' < /tmp/$$.tex \
       > $FILE.tex
       rm -f /tmp/$$.tex
fi


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

if [ $TEX = "no" ]
then
	SUFFIXES="$SUFFIXES tex"
	echo "Making $FILE.dvi from $FILE.tex."
	# 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
fi

# cleanup for DVI
if [ $DVI = "no" ]
then 
	SUFFIXES="$SUFFIXES dvi"
fi

# translate to PS
if [ $PS = "yes" ]
then 
	announce "Translating $FILE.dvi to PostScript."
	dvips -q -t $PAGE -o $FILE.ps $FILE.dvi
fi

cleanup

exit 0
