## -*- mode: Makefile -*-
##
## Copyright (c) 2000, 2001 University of Utah and the Flux Group.
## All rights reserved.
## 
## This file is part of the Knit component composition software.
## 
## Permission to use, copy, modify, distribute, and sell this software and
## its documentation is hereby granted without fee, provided that the above
## copyright notice and this permission/disclaimer notice is retained in all
## copies or modified versions, and that both notices appear in supporting
## documentation.  THE COPYRIGHT HOLDERS PROVIDE THIS SOFTWARE "AS IS" AND
## WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION,
## THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
## PURPOSE.  THE COPYRIGHT HOLDERS DISCLAIM ANY LIABILITY OF ANY KIND FOR
## ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
## 
## Users are requested, but not required, to send to csl-dist@cs.utah.edu any
## improvements that they make and grant Univ. of Utah redistribution rights.
##

###############################################################################

# Tell LaTeX to look in the source directories for input files.
TEXINPUTS = .:$(SRCDIR):$(SRCDIR)/tex::
export TEXINPUTS
# Tell LaTeX to look in the source directories for a `texmf.cnf'.
TEXMFCNF = $(SRCDIR)/tex::
export TEXMFCNF

vpath %.fig $(SRCDIR)/fig
vpath %.tex $(SRCDIR) $(SRCDIR)/tex

# The set of document ``root files''.
ROOT_TEXFILES = manual.tex manual-pdf.tex manual-www.tex

# TEXFILES: the set of non-root LaTeX files.
TEXFILES := $(wildcard *.tex) \
	    $(wildcard $(SRCDIR)/*.tex) \
	    $(wildcard $(SRCDIR)/tex/*.tex) \
	    $(wildcard $(SRCDIR)/tex/*.cfg)
TEXFILES := $(filter-out $(addprefix %/,$(ROOT_TEXFILES)),$(TEXFILES))

FIGFILES := $(wildcard $(SRCDIR)/fig/*.fig)
EPSFILES := $(patsubst $(SRCDIR)/fig/%.fig,%.eps,$(FIGFILES))

.SUFFIXES: .fig .eps

.PHONY: all
all: manual.ps

.PHONY: install
install: all

# Important!  Without `.DELETE_ON_ERROR', a bad run of `latex' will produce a
# bad but apparently up-to-date DVI file.
.DELETE_ON_ERROR:

# Do not delete `.dvi' files just because they are intermediaries.
.SECONDARY: $(addsuffix .dvi,$(basename $(notdir $(ROOT_TEXFILES))))

# Include any needed extra rules for building document-specific files.
-include $(SRCDIR)/Extras.mk

# XXX --- We don't currently handle BibTeX, indices, ....

###############################################################################

###
### Rules for the DVI/PS version of the document.
###

%.ps: %.dvi
	$(DVIPS) $(DVIPS_FLAGS) -o $@ $<

%.dvi: %.tex $(TEXFILES) $(EPSFILES) $(EXTRAFILES)
	$(LATEX) $<
	if fgrep -q -i -e 'rerun to get' $(@:.dvi=.log) ; then \
		$(LATEX) $< ; \
	fi; true
	if fgrep -q -i -e 'rerun to get' $(@:.dvi=.log) ; then \
		$(LATEX) $< ; \
	fi; true

%.2up.ps: %.ps
	-$(PSNUP) -n2 -rG $< > $@

###############################################################################

###
### Rules for the PDF version of the document.
###

###############################################################################

###
### Rules for the WWW version of the document.
###

manual-www.html: manual-www.dvi
	$(TEX4HT) $(basename $@)
	$(T4HT) $(basename $@)

# XXX --- The `fgrep' trick doesn't really work here.  TeX4ht doesn't always
# say ``rerun'' when a second run is required to get its files in order.
#
manual-www.dvi: manual-www.tex $(TEXFILES) $(EPSFILES) $(EXTRAFILES)
	$(LATEX) $<
	$(LATEX) $<
	$(LATEX) $<

###############################################################################

###
### Rules for figures.
###

ifneq "$(TEXFILES)" ""
ifneq "$(EPSFILES)" ""
# Build the figures first!
$(TEXFILES): $(EPSFILES)
endif
endif

%.eps: %.fig
	$(FIG2DEV) -L ps $? $@

###############################################################################

.PHONY: dist
dist: manual.ps distclean

.PHONY: clean
clean: distclean
	$(RM) \
		*.css *.gif *.html *.ps

.PHONY: distclean
distclean:
	$(RM) \
		*.aux *.dvi *.eps *.idx *.ilg *.ind *.log *.toc *.unit

###############################################################################

## End of file.

