#!/bin/bash
# compile
#
# This command compiles the document and will stop at any point due to error.
# Put the document name here. 'master' is useful for auctex and makes
# the script generic.
DOCUMENT_BASENAME="master"
# First remove any old tex-generated files as these can get messed up.
# See the clean script for what all gets deleted.
./clean
# Now compile all graphics files (.asy, .fig, etc.) in the graphics directory
# with their corresponding binaries (asymptote, fig2pdf, etc) assuming the
# graphics directory exists. Then run pdflatex (three times).  Exit on error.
if [ -e graphics ]; then
    cd graphics
    if asy -f pdf *.asy ; then
	:
    else
	exit 1
    fi
    if TEXINPUTS=$PWD//: fig2pdf --add=bm,amsfonts,bbm,localsymbols --nogv *.fig ; then
	:
    else
	exit 1
    fi
    cd ..
fi
if pdflatex $DOCUMENT_BASENAME.tex ; then
    if [ -e $DOCUMENT_BASENAME.idx ]; then
	makeindex $DOCUMENT_BASENAME
    fi
    if pdflatex $DOCUMENT_BASENAME.tex ; then
	pdflatex $DOCUMENT_BASENAME.tex
    fi
fi
# Make a copy of the master document with new name based on the
# current directory.  This retains the last successfully compiled
# document and gives it a meaningful name for circulation.
cp $DOCUMENT_BASENAME.pdf $(basename $(pwd)).pdf
