#---*- Makefile -*-------------------------------------------------------
#$Author: saulius $
#$Date: 2019-10-16 10:31:34 +0300 (Wed, 16 Oct 2019) $
#$Revision: 226 $
#$URL: svn+ssh://saulius-grazulis.lt/home/saulius/svn-repositories/makefiles/xetex-and-latex/Makelocal-xetex-and-latex $
#------------------------------------------------------------------------
#*
# 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)
#**

FIGURE_DIR = figures

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

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

export TEXINPUTS BIBINPUTS BSTINPUTS

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

PS_TARGETS  = $(wildcard ${LTX_FILES:%.ltx=%.ps*}) $(wildcard ${TEX_FILES:%.tex=%.ps*})

RTF_TARGETS = $(wildcard ${LTX_FILES:%.ltx=%.rtf*}) $(wildcard ${TEX_FILES:%.tex=%.rtf*})

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)

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)

RTF_TARGETS = $(wildcard ${RTF_FILES:%=%*})

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

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

.PHONY: all
.PHONY: ps dvi pdf rtf www html

all: pdf ${PS_TARGETS) ${RTF_TARGETS}

include ${DEPEND_FILES}

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

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

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

%.pdf: %.tex
	xelatex $*

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

## %.pdf: %.ps
## 	ps2pdf \
## 	$< $@

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

%.dvi: %.tex %.bbl
	latex $<

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

%.rtf: %.tex
	perl -pe 's/\\input\{/\\input\{text-parts\//' $< > tmp-$$$$.tex; \
	latex2rtf -b $*.bbl -a $*.aux -o $@ tmp-$$$$.tex; \
	rm tmp-$$$$.tex

.%.d: %.tex
	date +"# %F %T %Z" > $@
	mktexdepend $< | sed 's/\.dvi:/.pdf:/' >> $@

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

%.eps: %.svg
	inkscape --export-eps=$@ $<

%.eps.gz: %.png
	pngtopnm $< | pnmtops -noturn -scale=0.12 | gzip > $@

%.eps.gz: %.jpg
	jpegtopnm $< | pnmtops -noturn -scale=0.12 | gzip > $@

## ${FIGURE_DIR}/%.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 $*
## 	   xelatex  $*
## 
## %.aux: %.tex
## 	   xelatex $<

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

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

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

.PHONY: clean cleanAll distclean
.PHONY: cleanps cleanpdf cleandvi cleanwww cleanhtml
.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}
