#!/bin/sh

rm -rf $1.xcd

echo " --> running cdbpreproc"
cdbpreproc $1
if ! [ $? = 0 ]; then
   echo "cdbpreproc encountered an error"
   echo "Try running $1 through cbpreproc at the command line."
   exit 1
fi

echo " --> running cadabra"
cadabra --xcadabra < $1.cdb > $1.xcd
if ! [ $? = 0 ]; then
   echo "cadabra encountered an error"
   echo "Try running $1.cdb through cadabra at the command line."
   exit 1
fi

echo " --> running cdbmerge"
$HOME/local/bin/cdbmerge $1
if ! [ $? = 0 ]; then
   echo "cdbmerge encountered an error"
   echo "The most likely cause is a line of Cadabra output exceeded 20000 characters."
   echo "Is that what you expected?."
   exit 1
fi

echo " --> running cdbpstproc"
$HOME/local/bin/cdbpstproc $1 keep-tags
if ! [ $? = 0 ]; then
   echo "cdbpstproc encountered an error"
   echo "Try running $1.tex through cdbpstproc at the command line."
   exit 1
fi

mv $1.notag $1.tex

echo " --> running latex"
pdflatex -halt-on-error -interaction=batchmode $1 > /dev/null
if ! [ $? = 0 ]; then
   echo "LaTeX encountered an error"
   echo "Try running $1.tex through latex at the command line."
   exit 1
fi

rm -rf $1.log $1.xcd $1.eqn $1.tmp
