#!/bin/sh
# $Id: genteammanuals 2895 2009-10-03 22:04:24Z eldering $
#
# Script to generate team manual including actual configuration
# settings like WEBBASEURI.
#
# Part of the DOMjudge Programming Contest Jury System and licenced
# under the GNU GPL. See README and COPYING for details.

set -e

SOURCES="team-manual.tex team-manual-NL.tex"
PREAMBLE=team-manual-preamble.tex

NRUNS=3

OUTPUTDIR=.
[ -n "$1" ] && OUTPUTDIR="$1"

if [ ! -f "$PREAMBLE" ]; then
	echo "File '$PREAMBLE' not found; are you running"
	echo "this script from the team manual directory?"
	exit 1
fi

if [ ! -d "$OUTPUTDIR" -o ! -w "$OUTPUTDIR" ]; then
	echo "Output directory '$OUTPUTDIR' not found or not writable."
	exit 1
fi

# Set LaTeX command and flags if not given:
[ -z "$LATEX" ] && LATEX=pdflatex
[ -z "$LATEXFLAGS" ] && LATEXFLAGS='--interaction=errorstopmode'

TEXCONFIG=`mktemp /tmp/domjudge-texconfig.XXXXXX`

if [ -x "gentexconfig" ]; then
	./gentexconfig > "$TEXCONFIG"
else
	./gentexconfig.in > "$TEXCONFIG"
fi

# Run LaTeX:
for f in $SOURCES ; do
	[ "$QUIET" ] || printf "Running $NRUNS passes of '$LATEX $LATEXFLAGS $f'..."
	run=1
	while [ $run -le $NRUNS ]; do
		( cat "$TEXCONFIG" ; echo \\"input{$f}" ) | \
			$LATEX $LATEXFLAGS -jobname ${f%.tex} -output-directory "$OUTPUTDIR" >/dev/null
		if [ $? -ne 0 ]; then
			printf "\nTeX-ing failed in run $run, see '${f%tex}log' for details.\n"
			exit 1
		else
			[ "$QUIET" ] || printf " $run"
		fi
		run=$(($run+1))
	done
	[ "$QUIET" ] || printf "\n" ;
done

rm -f "$TEXCONFIG"
