#!/bin/sh

# usage: build [-c] [-f] [-u N] [-ps] [-pdf] [target]

# With no flags, <target> will be handed to make as `make <target>'
# With the -f flag, the first command run is `latex <target>'
# In either case, the log file is scanned to decide what more needs to be done.
# You can limit the total number of latex runs with the -u N argument.

knownTargets="dylp dylpfigs"
dfltTarget=dylp

makecmd='make -f makefile.dylpdoc'

# Make sure we have a directory for generated .eps files for figures.

if ! test -d Figures ; then
  mkdir Figures
fi

# Tell TeX where to find its inputs. TEXMFHOME is predefined in TeX Live to
# allow a user to point to a local source tree for style files, fonts, etc.
# This tree must have the same structure as the distribution tree.
# TEXINPUTS is for general input files. See the TeX Live documentation. If your
# TeX distribution uses different mechanisms, well, good luck.

# abs_srcdir=/home/Coin/Split/DyLP/DyLP/doc
# TEXMFHOME=$abs_srcdir/TexMF
# TEXINPUTS=$abs_srcdir/:

TEXMFHOME=@abs_srcdir@/TexMF
TEXINPUTS=@abs_srcdir@/:

export TEXMFHOME TEXINPUTS

printenv | grep TEX

force=0
clean=0
ltxlimit=5
needLaTeX=0
needBibTeX=0
buildPDF=0
buildPS=0

while test $# -ge 1 ; do
  case $1 in
    *-c*)
      clean=1 ;;
    *-f*)
      force=1 ;;
    *-pdf)
      buildPDF=1 ;;
    *-ps)
      buildPS=1 ;;
    *-u)
      shift
      ltxlimit=$1 ;;
    *-h*)
      echo "usage: $0 [-c|--clean] [-f|--force] [-u N] [-ps] [-pdf] [target]"
      echo "  known targets: $knownTargets"
      echo "  default target: $dfltTarget"
      echo "  $0 --clean with no target will clean and exit"
      echo "  $0 --clean with a target will clean and then build the target"
      echo "  For all options, you can use \`-' or \`--'."
      exit ;;
    *)
      tgt=$1 ;;
  esac

  shift
done

# Clean out the previous build?

if test $clean = 1 ; then
  $makecmd clean
  if test ${tgt:-0} = 0 ; then exit ; fi
fi

# Known target?

if test ${tgt:-0} = 0 ; then
  tgt=$dfltTarget
fi
found=0
for knowntgt in $knownTargets ; do
  if test "$knowntgt" = "$tgt" ; then
    echo "Building $tgt"
    found=1
    break
  fi
done
if test ${found:-0} = 0 ; then
  echo "$0: Unknown target \"$tgt\"; try \"$0 --help\""
  exit
fi

# Check if we need to do anything.

if test ${needLaTeX:-0} = 0 ; then
  $makecmd -q $tgt
  if test $? -eq 0 ; then
    echo "Make says no need to run LaTeX."
  else
    needLaTeX=1
    echo "Make says run LaTeX."
  fi
fi

if test ${needBibTeX:-0} = 0 ; then
  $makecmd -q $tgt.bbl
  if test $? -eq 0 ; then
    echo "Make says no need to run BibTeX."
  else
    needBibTeX=1
    echo "Make says run BibTeX."
  fi
fi

if test ${force:-0} = 0 ; then
  if test ${needLaTeX:-0} = 1 ; then
    echo "Calling make for $tgt"
    $makecmd $tgt
    ltxlimit=`expr $ltxlimit - 1`
  fi
else
  echo "Forcing LaTeX for $tgt"
  latex $tgt
  ltxlimit=`expr $ltxlimit - 1`
fi

if test $ltxlimit -eq 0 ; then exit ; fi

# Now check if we need to run BibTex. There are two conditions to check:
# 1) The log file contains one or both of the following lines:
#	No file dylp.bbl.
#	LaTeX Warning: Citation `----' on page N undefined ...
# 2) tgt.bbl already exists, and make -q tgt.bbl indicates work to be done
# This section runs LaTex only once --- we'll need to run a second time, but
# that'll be swept up in the next block for labels.

egrep 'No file [^.]*\.bbl|Citation .* undefined' $tgt.log
if test $? -eq 0 ; then
  needBibTeX=2
fi
if test $needBibTeX -gt 0 ; then
  echo "Running BibTex to generate/update $tgt.bbl."
  bibtex $tgt
  echo "Running LaTeX for initial scan of $tgt.bbl"
  latex $tgt
  ltxlimit=`expr $ltxlimit - 1`
  if test $ltxlimit -eq 0 ; then exit ; fi
fi

# Do we need to run again to get labels right?

egrep 'Rerun' $tgt.log
if test $? -eq 0 ; then
  echo "Running LaTex for cross-references"
  latex $tgt
  ltxlimit=`expr $ltxlimit - 1`
  if test $ltxlimit -eq 0 ; then exit ; fi
fi

# And did the user ask for more than a DVI file? We can build PostScript or
# PDF.

if test ${buildPS:-0} = 1 ; then
  echo "Running dvips for PostScript output"
  dvips -D1200 -o dylp.ps dylp.dvi
fi

if test ${buildPDF:-0} = 1 ; then
  echo "Running pdflatex for PDF output"
  make -f makefile.dylpdoc $tgt.pdf
fi

