#------------------------------------------------------------------------
#$Author: saulius $
#$Date: 2019-03-04 20:41:30 +0000 (Mon, 04 Mar 2019) $
#$Revision: 5598 $
#$URL: file:///home/saulius/svn-repositories/paskaitos/%C5%A1ablonai/ataskaitoms/LaTeX/Makefile $
#------------------------------------------------------------------------
#
# The behaviour of this Makefile is following:
#
# 1. 'make cleanAll' cleans all files that are produced by this
# Makefile.
#
# 2. in the clean directory, just typing 'make' builds the .dvi file
# with *correct* bibliographic references; it runs latex 3 times to
# fix the references and bibtex to produce the citation list for the
# article as appropriate.
#
# 3. Now, if you modify the source .tex file and say 'make', the latex
# will be run only once, which is much faster but can build some new
# references incorrectly. To run the full "3-latex procedure" again,
# either type 'make cleanAll; make' or touch the .bib file(s) and type
# make (or use make's -W option)
#
#------------------------------------------------------------------------------

MAKECONF_FILES = $(filter-out %.example, $(filter-out %~, $(wildcard Makeconf*)))

ifneq ("${MAKECONF_FILES}","")
include ${MAKECONF_FILES}
endif

#------------------------------------------------------------------------------
## LATEX = pdflatex
LATEX = latex

PATH := tools:${PATH}

export PATH

FIGURE_DIR = figures

.PRECIOUS: %.aux %.bbl %.eps.gz  %.eps.bb

TEXINPUTS  = ./texinputs:./text-parts:${FIGURE_DIR}::./bibliography
BIBINPUTS  = bibliography
BSTINPUTS  = texinputs::

export TEXINPUTS BIBINPUTS BSTINPUTS

LTX_FILES = $(wildcard *.ltx)
TEX_FILES = $(wildcard *.tex)

PDF_TARGETS = $(wildcard $(LTX_FILES:%.ltx=%.pdf)*) \
	$(wildcard $(TEX_FILES:%.tex=%.pdf)*) \
	$(wildcard $(TEX_FILES:%.tex=%.ps.pdf)*)

PS_FILES   = $(LTX_FILES:%.ltx=%.ps)  $(TEX_FILES:%.tex=%.ps)
DVI_FILES  = $(LTX_FILES:%.ltx=%.dvi) $(TEX_FILES:%.tex=%.dvi)
RTF_FILES  = $(LTX_FILES:%.ltx=%.rtf) $(TEX_FILES:%.tex=%.rtf)
HTML_FILES = $(LTX_FILES:%.ltx=%.www) $(TEX_FILES:%.tex=%.www)
PDF_FILES  = $(LTX_FILES:%.ltx=%.pdf) $(TEX_FILES:%.tex=%.pdf) \
	$(LTX_FILES:%.ltx=%.ps.pdf) $(TEX_FILES:%.tex=%.ps.pdf)

LOG_FILES  = $(LTX_FILES:%.ltx=%.log) $(TEX_FILES:%.tex=%.log)
AUX_FILES  = $(LTX_FILES:%.ltx=%.aux) $(TEX_FILES:%.tex=%.aux)
TOC_FILES  = $(LTX_FILES:%.ltx=%.toc) $(TEX_FILES:%.tex=%.toc)
OUT_FILES  = $(LTX_FILES:%.ltx=%.out) $(TEX_FILES:%.tex=%.out)

FIGURES_PNG = $(wildcard ${FIGURE_DIR}/*.png)
EPSGZ_FILES = ${FIGURES_PNG:${FIGURE_DIR}/%.png=%.eps.gz} \
              ${FIGURES_PNG:${FIGURE_DIR}/%.png=%.eps.bb}

DEPEND_FILES = ${LTX_FILES:%.ltx=.%.d} ${TEX_FILES:%.tex=.%.d}

#------------------------------------------------------------------------------

all: $(PS_FILES) $(PDF_TARGETS)

include ${DEPEND_FILES}

ps:   $(PS_FILES)
dvi:  $(DVI_FILES)
rtf:  $(RTF_FILES)
www:  $(HTML_FILES)
html: $(HTML_FILES)
pdf:  $(PDF_FILES)

#------------------------------------------------------------------------------

MAKELOCAL_FILES = ${filter-out %~, ${wildcard Makelocal*}}

ifneq ("${MAKELOCAL_FILES}","")
include ${MAKELOCAL_FILES}
endif

#------------------------------------------------------------------------------

BIBLIOGRAPHY = \
    $(sort ${shell perl -ne 'print `find . -name $$1.bib`."\n" if /^\\bibliography\{(.+)\}/' \
         ${LTX_FILES} ${TEX_FILES}})

# Forget the built-in TeX run rule:
%.dvi: %.tex

ifeq ($LATEX,"pdflatex")

# Rules using 'pdflatex':

%.pdf: %.tex ${FIGURES_PNG} %.bbl
	pdflatex $*

else

# Rules using PostScript flow:

%.dvi: %.tex ${EPSGZ_FILES}
	latex $*

%.ps: %.dvi
	dvips $*.dvi -D600 -o $@

%.ps.pdf: %.ps
	ps2pdf \
		-dAutoFilterColorImages=false \
		-dColorImageFilter=/FlateEncode \
	$< $@

endif

# Managing bibliography:

%.bbl: ${BIBLIOGRAPHY}
	latex  $*.tex
	bibtex $*
	latex  $*.tex

%.rtf: %.tex ${EPSGZ_FILES}
	latex2rtf -b $*.bbl -a $*.aux -o $@ $<

.%.d: %.tex
	mktexdepend $< > $@

%.eps: %.dia
	dia --filter=eps --export=$@ $<

%.eps.gz: %.png
	pngtopnm -background white -mix  $< | pnmtops -noturn | gzip > $@
	zcat $@ | grep '%%BoundingBox' > $*.eps.bb

%.eps.gz: ${FIGURE_DIR}/%.png
	pngtopnm $< | pnmtops -noturn --imagewidth 3.2 | gzip > $@

%.eps.bb: %.eps.gz
	zcat $< | grep '%%BoundingBox' > $@

#---------------------------------------------------------------------------
# The 'correct' but slower set of rules. This set of rules will run
# latex 3 times, whenever the source .tex file is modified. This is
# more 'correct' set of rules (the bibliographic references in fact
# might have been changed when the .tex source has been modified), but
# is probably too slow for everyday work.
# 

## %.bbl: %.aux ${BIBLIOGRAPHY}
## 	   bibtex $*
## 	   latex  $*
## 
## %.aux: %.tex
## 	   latex $<

#---------------------------------------------------------------------------

%.www: %.tex
	([ -d $*.www ] && mv $*.www $*; true)
	latex2html $<
	mv $* $*.www

#------------------------------------------------------------------------------

.PHONY: clean cleanAll distclean
.PHONY: cleanps cleanpdf cleandvi cleanwww cleanhtml cleanAll
.PHONY: cleanbibtex

cleanps:
	rm -f $(PS_FILES)

cleanpdf:
	rm -f $(PDF_FILES)

cleandvi:
	rm -f $(DVI_FILES)

cleanwww cleanhtml:
	rm -rf $(HTML_FILES)

cleanbibtex:
	rm -f  $(LTX_FILES:%.tex=%.bbl)
	rm -f  $(LTX_FILES:%.tex=%.blg)
	rm -f  $(TEX_FILES:%.tex=%.bbl)
	rm -f  $(TEX_FILES:%.tex=%.blg)

clean:
	rm -f $(LOG_FILES) $(AUX_FILES) $(TOC_FILES) include/*.aux
	rm -f $(OUT_FILES)
	rm -f $(LTX_FILES:%.tex=%.idx)
	rm -f $(TEX_FILES:%.tex=%.idx)

cleanAll distclean: clean cleandvi cleanps cleanpdf cleanhtml cleanbibtex
	rm -f ${DEPEND_FILES}
	rm -f ${EPSGZ_FILES}
