## -*- mode: Makefile -*-
##
## Copyright (c) 2002 University of Utah and the Flux Group.
## All rights reserved.
## This file is part of the JanosVM documentation.
## 
## Permission to use, copy, modify, distribute, and sell this
## documentation for any purpose is hereby granted without fee,
## provided that the above copyright notice(s) appear in all copies.
##

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

# 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

check:

# 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 indices, ....

janosvm.bib: $(SRCDIR)/tex/janosvm.bib
	$(CP) $(SRCDIR)/tex/janosvm.bib .

bib: manual.aux janosvm.bib
	bibtex manual
	$(CP) -f manual.bbl $(SRCDIR)/tex/
	@echo `grep bibitem manual.bbl | wc -l` references

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

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

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

manual.bbl: $(TEXFILES) $(SRCDIR)/tex/janosvm.bib
	$(CP) -f $(SRCDIR)/tex/manual.bbl .

# %.dvi: %.aux manual.bbl

%.dvi: %.tex $(TEXFILES) $(EPSFILES) $(EXTRAFILES) %.bbl
	$(RM) *.aux
	$(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 *.bbl *.blg \
		*.bib

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

## End of file.

