# Based on original from Eric Bischoff <eric@caldera.de>

# Modified by Tammy Fox <tfox@redhat.com> for Red Hat Documentation

# Convert to TeX
$SGML_JADE -t tex -o ${SGML_FILE_NAME}.tex $SGML_ARGUMENTS
if [ $? -ne 0 ]
then exit 1
fi

# Convert from TeX to DVI
jadetex ${SGML_FILE_NAME}.tex >${SGML_FILE_NAME}.out
if [ $? -ne 0 ]
then
  cat ${SGML_FILE_NAME}.out
  rm ${SGML_FILE_NAME}.out
  rm ${SGML_FILE_NAME}.tex
  exit 2
fi
rm ${SGML_FILE_NAME}.out

# If there are unresolved references, re-run jadetex, twice 
if egrep '^LaTeX Warning: There were undefined references.$' ${SGML_FILE_NAME}.log >/dev/null 2>&1
then
    jadetex ${SGML_FILE_NAME}.tex >/dev/null
    jadetex ${SGML_FILE_NAME}.tex >/dev/null
fi
#Keep log file - tfox
#rm ${SGML_FILE_NAME}.log
rm ${SGML_FILE_NAME}.aux
rm ${SGML_FILE_NAME}.tex

# Convert from DVI to PostScript
echo -n Converting to PS...
dvips -T 8.5in,11in -q ${SGML_FILE_NAME}.dvi -o ${SGML_FILE_NAME}.ps
if [ $? -ne 0 ]
then
  rm ${SGML_FILE_NAME}.dvi
  exit 3
fi
rm ${SGML_FILE_NAME}.dvi
echo Done!

# Convert from PS to PDF
echo -n Converting to PDF...
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=letter -sOutputFile=${SGML_FILE_NAME}.pdf ${SGML_FILE_NAME}.ps
rm ${SGML_FILE_NAME}.ps
echo Done!

exit 0
