#!/bin/bash
#
# Automate running LaTeX

program=`basename $0`
tex=latex
format=dvi
rerun='Rerun to get cross-references right'
maxruns=4
quiet=false

function texclean
{ rm -f *.idx *.ind *.ilg *.aux *.log *.lof *.out *.toc *.blg
}

# make runtex --clean work without GNU getopt
case "$1" in
	--clean)
	texclean
	exit 0
	;;
esac

appbase=`dirname $0`
case "$appbase" in
    /*)
	;;
    .)  appbase=`pwd`
        ;;
    ./*|../*)
	appbase="`pwd`/$appbase"
	clean=false
	while [ $clean = false ]; do
	  b=`echo $appbase | sed -e 's@/\./@/@g' -e 's@/[a-z]*/\.\./@/@g'`
	    if [ "x$b" = "x$appbase" ]; then
	      clean=true
	    else
	      appbase="$b"
	    fi
	done
	;;
    *)  echo "ERROR: runtex must be called with relative or absolute path"
        exit 1
	;;
esac

echo "runtex application base = $appbase"

function addinput
{ if [ -z "$TEXINPUTS" ]; then
    export TEXINPUTS="$1::"
  else
    export TEXINPUTS=$TEXINPUTS:$1::
  fi
}

function addbstinput
{ if [ -z "$BSTINPUTS" ]; then
    export BSTINPUTS="$1::"
  else
    export BSTINPUTS=$BSTINPUTS:$1::
  fi
}

function addbibinput
{ if [ -z "$BIBINPUTS" ]; then
    export BIBINPUTS="$1::"
  else
    export BIBINPUTS=$BIBINPUTS:$1::
  fi
}

function usage
{ echo "Usage:"
  echo ""
  echo "    $program [options] file"
  echo ""
  echo "Options:"
  echo ""
  echo "    --pdf        Use pdflatex and make .pdf images"
  echo "    --dvi        Use latex"
  echo "    --help       Print this message"
  echo "    --maxruns=#  Specify maximum # runs"
  echo "    --clean      Just remove TeX temporary files"
}



if [ -d $appbase ]; then
  addinput $appbase
  addbstinput $appbase
  addbibinput $appbase
fi

argp=true
while [ $argp = true ] ; do
    case "$1" in
        --pdf)
	    tex=pdflatex
	    format=pdf
	    shift ;;
        --dvi)
	    tex=latex
	    format=dvi
	    shift ;;
	--quiet)
	    quiet=true;
	    shift ;;
	--maxruns=*)
	    maxruns="`echo $1 | sed 's/--maxruns=//'`"
	    shift ;;
	--inputs=*)
	    addinput "`echo $1 | sed 's/--inputs=//'`"
	    shift ;;
	--help)
	    usage
	    exit 0 ;;
	--clean)
	    texclean
	    exit 0
	    ;;
        --*)
	    usage
	    exit 1 ;;
	*)
	    argp=false;
	    ;;
    esac
done

if [ -z "$1" ]; then
  usage
  exit 1
fi

file="$1"

# ensure .tex suffix

if [ ${file%.tex} = $file ]; then
    file=$file.tex
fi

doc=${file%.tex}

if [ -r Makefile ] && grep -q '^tex:' Makefile; then
    make tex
fi

cont=yes
done=0
while [ $cont != "no" ]; do
    cont=maybe

					# fix index problems
    if [ -r $doc.idx ]; then
	cp $doc.idx $doc.idx.$$
        if [ -x $appbase/correctindex ]; then
	    $appbase/correctindex $doc.idx
	fi
	if [ -r $appbase/makeindex.ist ]; then
	    makeindex -s $appbase/makeindex.ist $doc
	else
	    makeindex $doc
	fi
    fi
    if test -r $doc.aux && grep -qw bibdata $doc.aux; then
	if [ -f $doc.bbl ]; then cp $doc.bbl $doc.bbl$$; fi
	echo "%%% Running Bibtex"
        bibtex $doc
	if [ -r $doc.bbl -a -r $doc.bbl.$$ ]; then
	   if cmp -s $doc.bbl $doc.bbl.$$; then
	      true
	   else
	      cont=yes
	      echo "*** Bibtex output changed.  Rerunning $tex ***"
	   fi
	fi
	rm -f $doc.bbl$$
    fi
    if [ `basename $tex` = pdflatex -a -r $appbase/Makefile.pdf ]; then
        make -f $appbase/Makefile.pdf
    fi
    rm -f $doc.log
    if [ $quiet == true ]; then
        $tex $doc -file-line-error -halt-on-error >/dev/null
    else
	$tex $doc
    fi
    if [ $? != 0 ]; then
        rm -f $doc.idx.$$
        exit $?;
    fi
    if grep -q "$rerun" $doc.log; then
        echo "*** Cross-references changed.  Rerunning $tex ***"
	cont=yes
    else
        if [ -r $doc.idx -a -r $doc.idx.$$ ]; then
	   if cmp -s $doc.idx $doc.idx.$$; then
	      true
	   else
	      ls -l $doc.idx $doc.idx.$$
	      cont=yes
	      echo "*** Index changed.  Rerunning $tex ***"
	   fi
        fi
    fi
    rm -f $doc.idx.$$
    done=$(($done+1))
    if [ $done = $maxruns ]; then cont=no; fi
    if [ $cont = maybe ]; then cont=no; fi
done


if grep -q Warning $doc.log; then
   echo ""
   echo "*****************************"
   echo "The following warnings remain"
   echo "*****************************"
   grep Warning $doc.log
fi