# Makefile for LaTeX files

# Original Makefile from http://www.math.psu.edu/elkin/math/497a/Makefile

# Copyright (c) 2005 Matti Airas <Matti.Airas@hut.fi>

# Hacked in various ways by Martin Bays; all bugs and uglinesses likely his 
# fault

# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions: 

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software. 

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

# $Id: Makefile,v 1.7 2005/02/17 20:16:59 martin Exp martin $

LATEX	= latex
PDFLATEX= pdflatex
BIBTEX	= bibtex
MAKEINDEX = makeindex
XDVI	= xdvi -gamma 4
ifdef PAGES
DVIPS	= dvips -pp $(PAGES)
DVIPDF  = dvipdft -s $(PAGES)
else
DVIPS	= dvips
DVIPDF  = dvipdft
endif
L2H	= latex2html
#GH	= gv
GH	= run-mailcap
IGNORETEX = ^_.*
DATE	= date

RERUN = "(There were undefined references|Rerun to get (cross-references|the bars) right)"
RERUNBIB = "No file.*\.bbl|Citation.*undefined"
USEBIB = '^[^%]*\\bibliography'
MAKEIDX = '^[^%]*\\makeindex'
USEVERSION = '\\input{version.tex}'
USEBUILDDATE = '\\input{build-date.tex}'
MPRINT = '^[^%]*print'
USETHUMBS = '^[^%]*thumbpdf'

SRC	:= $(shell egrep -l '^[^%]*\\begin\{document\}' *.tex | egrep -v $(IGNORETEX))
SEDSRC	:= $(wildcard *.tex.in)
SRC	+= $(SEDSRC:%.tex.in=%.tex)
SRC	:= $(sort $(SRC))

# FIXME: this is broken
#EPSPICS := $(shell perl -ne '@foo=/^[^%]*\\(includegraphics|psfig)(\[.*?\])?\{(.*?)\}/g;if (defined($$foo[2])) { if ($$foo[2] =~ /.eps$$/) { print "$$foo[2] "; } else { print "$$foo[2].eps "; }}' *.tex)

TAG := $(shell ( [ -e _darcs ] && changes --tags='.*' 2>/dev/null | sed -n 's/^ *tagged \(.*\)$$/\1/p' | head -n1 ) || ( [ -e .git ] && git rev-parse HEAD | head -c 8 ) )

DEP	:= $(shell egrep -L '^[^%]*\\begin\{document\}' *.tex | egrep -v $(IGNORETEX))
SEDDEP	:= $(wildcard *.tex.in)
SEDDEP	:= $(SEDDEP:%.tex.in=%.tex)
DEP	+= $(SEDDEP)
DEP	:= $(sort $(DEP))

TRG	= $(SRC:%.tex=%.dvi)
PSF	= $(SRC:%.tex=%.ps)
PDF	= $(SRC:%.tex=%.pdf)
BBL	= $(SRC:%.tex=%.bbl)
BIB	= $(SRC:%.tex=%.bib)

COPY = if test -r $(<:%.tex=%.toc); then cp $(<:%.tex=%.toc) $(<:%.tex=%.toc.bak); fi 
RM = rm -f
OUTDATED = echo "EPS-file is out-of-date!" && false


all 	: $(TRG)


define run-latex
	  $(COPY);$(LATEX) $<
	  egrep -q $(USEBUILDDATE) $< && $(DATE) >| build-date.tex ; true
	  egrep -q $(USEVERSION) $< && ( if [ -n '$(TAG)' ]; then echo 'version $(TAG)'; else echo ''; fi >| version.tex ) ; true
	  egrep -q $(MAKEIDX) $< && ($(MAKEINDEX) $(<:%.tex=%);$(COPY);$(LATEX) $<) ; true
	  egrep -q $(RERUN) $(<:%.tex=%.log) && ($(COPY);$(LATEX) $<) ; true
	  egrep -q $(RERUN) $(<:%.tex=%.log) && ($(COPY);$(LATEX) $<) ; true
	  if cmp -s $(<:%.tex=%.toc) $(<:%.tex=%.toc.bak); then true ;else $(LATEX) $< ; fi
	  $(RM) $(<:%.tex=%.toc.bak)
	  # Display relevant warnings
	  egrep -i "(Reference|Citation).*undefined" $(<:%.tex=%.log) ; true
endef

# TODO: cleverer method!
%.tex : %.1.sed %.2.sed %.tex.in
	sed -f $*.1.sed $*.tex.in > $@.t
	sed -f $*.2.sed $@.t > $@

# Parse the input .tex to extract dependencies

BIBFILES = $(shell perl -ne '($$_)=/^[^%]*\\bibliography\{(.*?)\}/;@_=split /,/;foreach $$b (@_) {print "$$b.bib "}' $(SRC))
BIBPATHS = $(shell pwd) $(shell echo "$(BIBINPUTS)" | sed 's/:/ /g')

.bibdeps: $(SRC)
	for bib in $(BIBFILES); do for path in $(BIBPATHS); do if [ -e "$$path/$$bib" ]; then echo "$$path/$$bib"; break; fi; done; done > .bibdeps

# Handling recursive includes properly would be too difficult, so we just
# depend on anything included in the current dir and *everything* in any local
# include directories set by TEXINPUTS

INCFILES = $(shell sed -n 's/^\\\(input\|include\){\([^}]*\)}/\2.tex/p'  < $(SRC)| sed 's/\.tex\.tex/.tex/')
INCFILES += $(shell sed -n 's/^\\usepackage{\([^}]*\)}/\1.sty/p' < $(SRC))
INCPATHS = $(shell echo "$(TEXINPUTS)" | sed 's/:/ /g')
.incdeps: $(SRC)
	for inc in $(INCFILES); do if [ -e "./$$inc" ]; then echo "`pwd`/$$inc"; fi; done > .incdeps
	for incpath in $(INCPATHS); do if ! [ "$$incpath" = "." ]; then ls -1 "$$incpath"/*.{tex,sty} >> .incdeps 2>/dev/null; fi; done

.deps: .bibdeps .incdeps
	cat .bibdeps .incdeps > .deps

deps: .deps

arXivdeps: .incdeps
	cat .incdeps > .arXivdeps


# XXX: This clearly isn't the right way to do the DEPS thing... but I'm not sure
# what is. 'make deps' must be called explicitly.

DEPS = $(shell cat .deps 2>/dev/null)

$(TRG)	: %.dvi : %.tex $(DEP) $(EPSPICS) $(DEPS) $(patsubst %.bib, %.bbl, $(BIBFILES))
	  @$(run-latex) || rm %.dvi

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

#$(PDF)  : %.pdf : %.dvi
#	  @$(DVIPDF) -o $@ $<

$(PDF)  : LATEX=$(PDFLATEX)
$(PDF)  : %.pdf : %.tex $(DEP) $(EPSPICS) $(DEPS) $(patsubst %.bib, %.bbl, $(BIBFILES))
	  @$(run-latex)

$(BBL)  : %.bbl : %.bib %.tex
	$(LATEX) $(<:%.bib=%.tex)
	@$(BIBTEX) $(<:%.bib=%)

show	: $(TRG)
	  @for i in $(TRG) ; do $(XDVI) $$i ; done

showps	: $(PSF)
	  @for i in $(PSF) ; do $(GH) $$i ; done

showpdf	: $(PDF)
	  @for i in $(PDF) ; do $(GH) $$i ; done

test	: showpdf

test-p% :
	echo $*

ps	: $(PSF) 

pdf	: $(PDF) 

# TODO: This probably needs fixing
html	: @$(DEP) $(EPSPICS)
	  @$(L2H) $(SRC)

clean	:
	  -rm -f $(TRG) $(PSF) $(PDF) $(TRG:%.dvi=%.aux) $(TRG:%.dvi=%.bbl) $(TRG:%.dvi=%.blg) $(TRG:%.dvi=%.log) $(TRG:%.dvi=%.out)

NAME := $(shell pwd | sed 's/.*\///')
VERSION := "$(shell if [ -n '$(TAG)' ]; then echo '$(TAG)'; else date +%y%m%d; fi)"
DATESTAMP := $(shell date +%y%m%d)
FULLNAME := "$(NAME)-$(DATESTAMP)-$(VERSION)"
dist	: $(TRG) $(PSF) $(PDF)
	[ -r .deps ] || (echo "run make .deps first, and prune .deps appropriately" && false)
	(\
	rm -rf dist-tmp	;\
	mkdirhier dist-tmp/$(FULLNAME);\
	cp $(SRC) $(SEDSRC) *.sed Makefile dist-tmp/$(FULLNAME);\
	for dep in `cat .deps`; do cp $$dep dist-tmp/$(FULLNAME); done	;\
	cd dist-tmp	;\
	tar -zcvf "$(FULLNAME).tgz" "$(FULLNAME)"	;\
	mv "$(FULLNAME).tgz" ..	;\
	cd ..	;\
	rm -rf dist-tmp	;\
	[ -d dist ] && cp -p "$(FULLNAME).tgz" $(TRG) $(PSF) $(PDF) dist/;\
	true;\
	)

arXivsrc	:
	[ -r .arXivdeps ] || (echo "run make .arXivdeps first, and prune .arXivdeps appropriately" && false)
	(\
	rm -rf arXiv-tmp	;\
	mkdir arXiv-tmp	;\
	cd arXiv-tmp	;\
	mkdir "$(FULLNAME)"	;\
	cd "$(FULLNAME)"	;\
	cp ../../$(SRC) .	;\
	for dep in `cat ../../.arXivdeps`; do cp $$dep .; done	;\
	tar -zcvf "$(FULLNAME)"-arXiv_src.tgz *	;\
	mv "$(FULLNAME)"-arXiv_src.tgz ../..	;\
	cd ../..	;\
	rm -rf arXiv-tmp	;\
	true;\
	)

#cp ../../$(TRG:%.dvi=%.bbl) .	;\
#(cd ../.. && cp --parents $(INCFILES) "arXiv-tmp/$(FULLNAME)") ;\

arXivsrc1	:
	[ -r .arXivdeps ] || (echo "run make .arXivdeps first, and prune .arXivdeps appropriately" && false)
	(\
	rm -rf arXiv-tmp	;\
	mkdir arXiv-tmp	;\
	cd arXiv-tmp	;\
	mkdir "$(FULLNAME)"	;\
	cd "$(FULLNAME)"	;\
	cp ../../$(SRC) .	;\
	for dep in `cat ../../.arXivdeps`; do cp $$dep .; done	;\
	)

arXivsrc2	:
	(\
	cd arXiv-tmp/$(FULLNAME)	;\
	tar -zcvf "$(FULLNAME)"-arXiv_src.tgz *	;\
	mv "$(FULLNAME)"-arXiv_src.tgz ../..	;\
	cd ../..	;\
	rm -rf arXiv-tmp	;\
	true;\
	)


.PHONY	: all show clean ps pdf showps dist


######################################################################
# Define rules for EPS source files.
%.eps: %.sxd
	$(OUTDATED)
%.eps: %.sda
	$(OUTDATED)
%.eps: %.png
	$(OUTDATED)
%.eps: %.sxc
	$(OUTDATED)
%.eps: %.xcf
	$(OUTDATED)
%.eps: %.zargo
	$(OUTDATED)
%.eps: %.m
	@egrep -q $(MPRINT) $< && ($(OUTDATED))
