#---*- Makefile -*-------------------------------------------------------
#$Author: saulius $
#$Date: 2020-06-04 12:55:15 +0300 (Kt, 04 Bir 2020) $
#$Revision: 243 $
#$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)
#**

TEXINPUTS  = ./texinputs:./text-parts::

export TEXINPUTS

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

PDF_FILES  = $(LTX_FILES:%.ltx=%.pdf) $(TEX_FILES:%.tex=%.pdf)
PS_FILES   = ${TEX_FILES:%.tex=%.ps}
RTF_FILES  = $(LTX_FILES:%.ltx=%.rtf) $(TEX_FILES:%.tex=%.rtf)
HTML_FILES = $(LTX_FILES:%.ltx=%.www) $(TEX_FILES:%.tex=%.www)

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)

PS_TARGETS  = $(wildcard ${PS_FILES:%=%*})
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}

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

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

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

%.pdf: %.tex
	xelatex $*

%.ps: %.pdf
	pdf2ps $< $@

%.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:/' >> $@

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

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

.PHONY: cleanpdf-xelatex cleanwww-xelatex cleanhtml-xelatex
.PHONY: cleanps-xelatex clean-xelatex distclean-xelatex cleanAll-xelatex

cleanps-xelatex:
	rm -f ${PS_FILES}

cleanpdf-xelatex:
	rm -f $(PDF_FILES)

cleanwww-xelatex cleanhtml-xelatex:
	rm -rf $(HTML_FILES)

clean-xelatex:
	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)
	rm -f $(TEX_FILES:%.tex=%.nav)

cleanAll-xelatex distclean-xelatex: \
	clean-xelatex cleanps-xelatex cleanpdf-xelatex cleanhtml-xelatex
	rm -f ${DEPEND_FILES}

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

.PHONY: clean cleanAll distclean

clean: clean-xelatex

distclean: distclean-xelatex

cleanAll: cleanAll-xelatex
