#! /bin/sh
######################################################################
#
# Authors:        Alexander Horz and Tom Gordon
# Date:           11-28-89
# Last Modified:  22 May 92
#
######################################################################

# Set to path of linuxdoc-sgml directory
LINUXDOC=/usr/lib/linuxdoc-sgml-1.2
export LINUXDOC

# Set to command to use for LaTeX
LATEX="latex"

# Set to command to use for dvips. 
DVI2PS="dvips"

# You shouldn't have to edit anything below this line.

TEXINPUTS=":$LINUXDOC"
export TEXINPUTS


######################################################################
## all switches are "no" by default
######################################################################

DVI="no"
MULTIPLE_PASSES="yes"
SAVE="no"
BIBTEX="no"
CHAPTER="no"
VERBOSE="yes"

######################################################################
## 
######################################################################

FILE=" "
SUFFIXES="log blg aux toc lof lot log dlog bbl"
PROGNAME=$0

######################################################################
# synopsis: announce <string>
#
# 	     echo string on std_err if verbose mode is set on
######################################################################

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

######################################################################
# synopsis: cleanup
#
# remove all the files $FILE.$SFX, where $SFX is in $SUFFIXES
######################################################################

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

######################################################################
# synopsis: usage
#
# print a short help message
######################################################################

usage () {
echo "	qtex 	[-d]		* dvi instead of PostScript";
echo "		[-s]		* save all temporary files";
echo "		[-x]		* cross references";
echo "		[-g]		* german";
echo "		[-b]		* bibliography";
echo "		[-v]		* verbose";
echo "		[-c]		* single chapter";
echo "		<filename>	* .tex extension MUST be ommitted (BUG)";
exit 1 
}

##################eno###################################################
# synopsis: LaTeX  $FILE
#
# run LaTeX on $FILE 
######################################################################


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

######################################################################
## 		command line argument processing ...
######################################################################

case "$1" in
	"help" | "HELP" | "Help" | "-help" ) usage
esac

trap 'cleanup; exit 1' 1 2 3 9

set -- `getopt dxgbsvc $*`

if [ $? != 0 ]
then
	usage
fi

for i in $*
do
        case $i in
	      -c)       CHAPTER="yes"; shift;;
	      -d)	DVI="yes"; shift;;
	      -x)  	MULTIPLE_PASSES="yes"; shift;;
	      -b)  	BIBTEX="yes"; shift;;
	      -s)    	SAVE="yes"; shift;;
	      -v)	VERBOSE="yes"; shift;;
              --)       shift;
			break;;
        esac
done

if [ "$1" = "" ] 
then
	cat > $$.tex  # write standard input to file
	FILE=$$
	SUFFIXES=$SUFFIXES" tex"
else
	FILE=$1
fi


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

######################################################################

announce "producing $FILE.dvi ..."

LaTeX $FILE

if [ $BIBTEX = "yes" ]
then
	MULTIPLE_PASSES="yes"
	announce "BiBTeXing $FILE ... "
	bibtex $FILE > /dev/null
	
	announce "LaTeXing again ... "
	LaTeX $FILE  
fi

if [ $MULTIPLE_PASSES = "yes" ]
then
	announce "LaTeXing again ... "
        LaTeX $FILE
	announce "And again ... "
        LaTeX $FILE
fi

if [ $DVI = "no" ]
then 
	SUFFIXES="$SUFFIXES dvi"
	announce "translating $FILE.dvi to PostScript ... "
	$DVI2PS -o $FILE.ps $FILE.dvi  2> /dev/null
else
	cat $FILE.dvi 
	rm -f $FILE.dvi
fi

cleanup

# end of qtex script 
