#---*- Makefile -*-------------------------------------------------------
#$Author: saulius $
#$Date: 2020-08-12 11:01:16 +0300 (Wed, 12 Aug 2020) $
#$Revision: 250 $
#$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:.generated::

# Where the generated files are stored. Must be consistent for
# (Xe)LaTeX, BibTeX and/or Biber. MUST end in a slash, or MAY be
# empty:

GENERATED_DIR ?= ./

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=${GENERATED_DIR}%.log) $(TEX_FILES:%.tex=${GENERATED_DIR}%.log)
AUX_FILES  = $(LTX_FILES:%.ltx=${GENERATED_DIR}%.aux) $(TEX_FILES:%.tex=${GENERATED_DIR}%.aux)
TOC_FILES  = $(LTX_FILES:%.ltx=${GENERATED_DIR}%.toc) $(TEX_FILES:%.tex=${GENERATED_DIR}%.toc)
OUT_FILES  = $(LTX_FILES:%.ltx=${GENERATED_DIR}%.out) $(TEX_FILES:%.tex=${GENERATED_DIR}%.out)

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

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

LATEX ?= xelatex

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

.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
	${LATEX} $*
	if [ -n "${GENERATED_DIR}" -a "${GENERATED_DIR}" != "./" ]; \
	then \
		mv ${GENERATED_DIR}$*.pdf .; \
	fi

%.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:/' \
	| perl -pe 's@^(.*?\.bbl:)@${GENERATED_DIR}$$1@' \
	| perl -pe 's@:(\s*)(.*?\.bbl)$$@:$$1${GENERATED_DIR}$$2@' \
	>> $@

%.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=${GENERATED_DIR}%.idx)
	rm -f $(TEX_FILES:%.tex=${GENERATED_DIR}%.idx)
	rm -f $(TEX_FILES:%.tex=${GENERATED_DIR}%.nav)
	rm -f $(TEX_FILES:%.tex=${GENERATED_DIR}%.snm)
	rm -f $(TEX_FILES:%.tex=${GENERATED_DIR}%.pdf)

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
