#
# Generic Makefile for multi-part latex document with bibliographic
# data.
#
# List names of component files in the TEXFILES variable.
# This makefile keeps rerunning latex until all cross-references
# and bibliographic references are resolved. 
#
# The main file is assumed to be called main.tex.  The bibliography
# is assumed to be in main.bib.
#
# The rules and dependencies here acheive the following logic:
#   Build main.bbl as soon as possible after main.aux exists
#   Keep running latex on main.tex until no more warnings about
#     cross-references are generated
#

TEXFILES = \

OTHERDEPS = \

TABSUF = lof lot toc

all: main.dvi

main.PS: main.dvi
	dvips -o main.PS main

main.dvi: main.aux.bib
	echo done

tabcheck:
	@for i in $(TABSUF); do \
		if test -f main.$$i; then \
			diff main.$$i.old main.$$i >/dev/null 2>&1; \
		fi; \
	done

main.aux.no-unres: $(TEXFILES) $(OTHERDEPS)
	-@for i in $(TABSUF); do \
		cp main.$$i main.$$i.old >/dev/null 2>&1; \
	done
	latex main.tex
	if grep "No file main.bbl" main.log >/dev/null 2>&1; then \
		make main.bbl; fi
	if grep "cross-references right" main.log >/dev/null 2>&1; then \
		make main.aux.no-unres; fi
	if make tabcheck >/dev/null 2>&1; then true; else latex main.tex; fi
	touch main.aux.no-unres

clean:
	-rm -f *.aux *.dvi *.log *.toc *.bbl *.blg *.lof *.lot
	-rm -f *~ *.bak *.aux.*
	-rm -f *.PS TAGS
	-for i in $(TABSUF); do \
		rm -f *.$$i.old; \
	done
