#
# 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).ltx.  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).ltx until no more warnings about
#     cross-references are generated
#

MAIN = main

TEXFILES = \

OTHERDEPS = \

TABSUF = lof lot toc ind idx

all: $(MAIN).dvi

$(MAIN).PS: $(MAIN).dvi
	dvips -o $(MAIN).PS $(MAIN)

$(MAIN).lj: $(MAIN).dvi
	dvilj -s2 $(MAIN).dvi

$(MAIN).dvi: $(MAIN).aux.bib
	echo done

tabcheck:
	@error=0; \
	for i in $(TABSUF); do \
	   if test -f $(MAIN).$$i; then \
	      diff $(MAIN).$$i.old $(MAIN).$$i >/dev/null 2>&1 || error=1; \
	   fi; \
	done; \
	if test $$error -eq 1; then false; fi

$(MAIN).aux.no-unres: $(MAIN).ltx $(TEXFILES) $(OTHERDEPS)
	-@for i in $(TABSUF); do \
		cp $(MAIN).$$i $(MAIN).$$i.old >/dev/null 2>&1; \
	done
	if test -f $(MAIN).idx; then makeindex $(MAIN).idx; fi
	latex $(MAIN).ltx
	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).ltx; fi
	touch $(MAIN).aux.no-unres

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