# Makefile for LaTeX files

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

# Version midified by Sylwester Arabas <slayoo@igf.fuw.edu.pl> for support of:
# - natbib
# - bibunits
# - authorindex
# - pdflatex (solely)

# Please check http://www.acoustics.hut.fi/u/mairas/UltimateLatexMakefile
# for new versions.

# Copyright (c) 2005,2006 (in order of appearance):
#	Matti Airas <Matti.Airas@hut.fi>
# 	Rainer Jung
#	Antoine Chambert-Loir
#	Timo Kiravuo

# 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.1 2010/10/24 14:01:29 slayoo Exp $

LATEX	= pdflatex
# BIBTEX	= for i in `find . -maxdepth 1 -name bu\*.aux`; do bibtex `basename $$i .aux`; awk -f fixlines.awk `basename $$i .aux`.bbl > `basename $$i .aux`.bbl.tmp; mv `basename $$i .aux`.bbl.tmp `basename $$i .aux`.bbl; done
BIBTEX = bibtex $$main
MAKEINDEX = makeindex
ATHRIDX = for i in `find . -maxdepth 1 -name bu\[2,3,4\].aux`; do perl -w authorindex -r $$main `basename $$i`; done

# TODO update for bibunits
RERUN = "(There were undefined references|Rerun to get (cross-references|the bars) right)"
# update for natbib
RERUNBIB = "No file.*\.bbl|(Package natbib Warning: |)Citation.*undefined"
MAKEIDX = "^[^%]*\\makeindex"
MAKENML = "^[^%]*\\makenomenclature"
MAKEATHRIDX = "^[^%]*{authorindex\}"
MPRINT = "^[^%]*print"
USETHUMBS = "^[^%]*thumbpdf"

DATE=$(shell date +%Y-%m-%d)

COPY = if test -r $(<:%.tex=%.toc); then cp $(<:%.tex=%.toc) $(<:%.tex=%.toc.bak); fi 
RM = rm -f

# These are OK

SRC	:= $(shell egrep -l '^[^%]*\\begin\{document\}' *.tex)
PDF	= $(SRC:%.tex=%.pdf)

define run-latex
	$(COPY);$(LATEX) $<
	egrep $(MAKEIDX) $< && ($(MAKEINDEX) $(<:%.tex=%);$(COPY);$(LATEX) $<) >/dev/null; true
	egrep $(MAKENML) $< && ($(MAKEINDEX) $(<:%.tex=%.nlo) -s nomencl.ist -o $(<:%.tex=%.nls);$(COPY);$(LATEX) $<) >/dev/null; true
	egrep -c $(RERUNBIB) $(<:%.tex=%.log) && (main=$(<:%.tex=%);$(BIBTEX);$(COPY);$(LATEX) $<) ; true
	egrep -c $(MAKEATHRIDX) $< && (main=$(<:%.tex=%);$(ATHRIDX);$(COPY);$(LATEX) $<) ; true
	egrep $(RERUN) $(<:%.tex=%.log) && ($(COPY);$(LATEX) $<) >/dev/null; true
	egrep -c $(MAKEATHRIDX) $< && (main=$(<:%.tex=%);$(ATHRIDX);$(COPY);$(LATEX) $<) ; true
	egrep $(RERUN) $(<:%.tex=%.log) && ($(COPY);$(LATEX) $<) >/dev/null; 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

define get_dependencies
	deps=`perl -ne '($$_)=/^[^%]*\\\(?:include|input)\{(.*?)\}/;@_=split /,/;foreach $$t (@_) {print "$$t.tex "}' $<`
endef

define getbibs
	bibs=`perl -ne '($$_)=/^[^%]*\\\bibliography\{(.*?)\}/;@_=split /,/;foreach $$b (@_) {print "$$b.bib "}' $< $$deps`
endef

define manconf
	mandeps=`if test -r $(basename $@).cnf ; then cat $(basename $@).cnf |tr -d '\n\r' ; fi`
endef

all 	: $(TRG)

clean	: 
	  -rm -f $(PDF) *.log *.aux *.bbl *.blg *.ilg *.toc *.lof *.lot *.idx *.ind *.nls *.nlo *.ain *.out *~

# This is a rule to generate a file of prerequisites for a given .tex file
%.d	: %.tex
	$(get_dependencies) ; echo $$deps ; \
	$(getbibs) ; echo $$bibs ; \
	$(manconf) ; echo  $$mandeps  ;\
	echo "$*.dvi $@ : $< $$deps $$bibs $$mandeps" > $@ 

include $(SRC:.tex=.d)

$(PDF) : %.pdf : %.tex
	@$(run-latex)

all	: $(PDF) 

