# make_latex
# - A file to be included to enable make(1) to process LaTeX files.
# Suitable for single-part documents with BibTeX references and diagrams.
# David Beasley,  <David.Beasley@cm.cf.ac.uk>, December 1992
# Department of Computing Mathematics, Univeristy of Wales College of Cardiff.
# Last Update: 13/10/93
#
# Note on commands:
# 'make doc1.dvi' - creates dvi file if make thinks that something has changed.
# 'make doc1' - also creates dvi file, unconditionally (useful if you havent
#		specified _all_ the things it depends on)
#
# Refer to man entry for make(1) for explanation of syntax and command options

.KEEP_STATE:

# ---- The following shows the type of entries required in each Makefile ----

# include $(HOME)/lib/make_latex
# # Name(s) of LaTeX document file(s) (without suffixes)
# # eg.
# FILES = doc1 doc2
#
# # List here dependencies of documents on other files (eg diagrams 
# # from .gnu files) eg.
# doc1.dvi : fig1.ps fig2.ps fig3.ps
# doc2.dvi : diag1.ps diag2.ps
#
# # Names of additional files to be deleted by TIDY and CLEAN commands
# TIDYFILES = x
# CLEANFILES = y

# --------------- The following defaults may be user-configured --------------

# Default name(s) of BibTeX source files, eg.
BIBFILES = $(HOME)/Refs/myrefs.bib

# default printer resolution
PRINT_RES = 300

# Switch over resolution between lo and hi res devices
PR_RES_BOUNDARY = 400

# default printer names
PRINTER = lw0		# for output below $(PR_RES_BOUNDARY)
PR_HI_RES = lwpro	# for output >= $(PR_RES_BOUNDARY)

# name of print command
LPR = lpr

# options given to lpr command - delete after printing by default
PR_OPT = -r

# Pattern(s) to match for old versions of files to be deleted by 'make clean'
OLDVERSIONS = *%

# -------------- Lines below here shouldnt need changing -----------------

# default page range to print
PAGE_F = -999999
PAGE_T = 999999

# name of BibTeX command
BIBTEX = bibtex

# name of LaTeX command
LATEX = latex

# names of all DVI files
DVIFILES = $(FILES:%=%.dvi) 

# assume that all dvi files named depend on all bib files named
$(FILES:%=%.bbl) : $(BIBFILES)


# -------------- generic target names ----------------

# create all dvi files
all: $(DVIFILES)

#create all postscript files
allps: $(FILES:%=%.ps)

# print all files
allpr: $(FILES:%=%.pr)

# delete all files which can easily be regenerated
tidy:
	-rm -f $(DVIFILES) $(TIDYFILES)
	-rm -f $(FILES:%=%.ps) $(FILES:%=%.log) $(FILES:%=%.blg)

# delete all files which can be regenerated
clean:	tidy
	-rm -f $(FILES:%=%.aux) $(FILES:%=%.bbl) $(CLEANFILES)
	@# delete editor old-version files
	-rm -f $(OLDVERSIONS)

# -------------- pattern matching rules --------------

# ensure that the bbl file gets regenerated if the bib file is changed
%.bbl : $(BIBFILES)
	@# if there is no aux file, skip this, it will get done later
	-@if [ -r $*.aux ] ;\
	then $(BIBTEX) $* ; \
	     fi

# create a dvi file from a tex file
% %.dvi: %.tex
	$(LATEX) $*
	-@egrep -c 'Citation .* undefined.' $*.log && ($(BIBTEX) $*;$(LATEX) $*)
	-@grep 'Rerun to get cross-references right' $*.log && $(LATEX) $*

# create postscript file of a dvi file
%.ps: %.dvi
	@if [ $(PAGE_F) -lt -999998 ] ;\
	then 	echo dvips -D $(PRINT_RES) -l $(PAGE_T) -o $@ $* ;\
		dvips -D $(PRINT_RES) -l $(PAGE_T) -o $@ $* ;\
	else	echo dvips -D $(PRINT_RES) -p $(PAGE_F) -l $(PAGE_T) -o $@ $* ;\
		dvips -D $(PRINT_RES) -p $(PAGE_F) -l $(PAGE_T) -o $@ $* ;\
	fi

# create postscript file of a gnu plot
%.ps: %.gnu
	gnuplot $<

# create a latex picture file from an xfig file
%.pic: %.fig
	fig2dev -L latex $< $@

# create a postscript file from an xfig file
%.ps: %.fig
	fig2dev -L ps $< $@

# send a postcript file to be laser printed
%.pr: %.ps
	@if [ $(PRINT_RES) -lt $(PR_RES_BOUNDARY) ] ;\
	then echo $(LPR) $(PR_OPT) -P$(PRINTER) $< ;\
		  $(LPR) $(PR_OPT) -P$(PRINTER) $< ;\
	else echo $(LPR) $(PR_OPT) -P$(PR_HI_RES) $< ;\
		  $(LPR) $(PR_OPT) -P$(PR_HI_RES) $< ;\
	fi

