#!/bin/sh

# This script was taken from the LILO distribution (Version 19):
#
# : LILO program code, documentation and auxiliary programs are
# : Copyright 1992-1996 Werner Almesberger.
# : All rights reserved.
# :
# : Redistribution and use in source and binary forms of parts of or the
# : whole original or derived work are permitted provided that the
# : original work is properly attributed to the author. The name of the
# : author may not be used to endorse or promote products derived from
# : this software without specific prior written permission. This work
# : is provided "as is" and without any express or implied warranties.
#
# Modified by Geert Uytterhoeven <geert@linux-m68k.org> to run BibTeX when
# necessary.

bibtexcnt=0

while [ $bibtexcnt != 4 ]; do
    pid=$$
    export pid
    rm -f /tmp/rlok$pid
    (
	if latex $1; then
	    touch /tmp/rlok$pid
	fi
    ) | tee /tmp/rlso$pid
    if [ ! -f /tmp/rlok$pid ]; then
	rm -f /tmp/rlso$pid
	exit 1
    fi
    if grep '^LaTeX Warning: Citation' /tmp/rlso$pid >/dev/null; then
	echo "Running BibTeX"
	bibtex `echo $1 | sed -e 's/.tex$//'`
	bibtexcnt=`expr $bibtexcnt + 1`
    else
	if grep '^LaTeX Warning: Label(s) may' /tmp/rlso$pid >/dev/null; then :
	else
	    rm -f /tmp/rlso$pid
	    exit 0
	fi
    fi
    echo "Re-running LaTeX"
done
