# This Makefile updates the source and documentation for the
# babel-greek LateX package
#
# © 2019 Günter Milde
#
# SRCDIR (and the other variables) can be easily modified as parameters
# while calling `make', e.g.
#
#   make install SRCDIR=~/texmf/tex/latex/

# Global variables
# =================

# for installation into a users local texmf tree
DESTDIR = ~/texmf/tex/latex/greek/babel-greek
DOCDIR = ~/texmf/doc/latex/greek/babel-greek

CHDIR = cd
COPY = cp --update
SH = sh

# programs
PYLIT = pylit        # text <-> source converter for literal programming
RST2HTML = rst2html5 # text -> HTML converter, see docutils.sf.net
RUBBER = rubber --force --pdf # latex-wrapper running until refs are resolved
LATEX = pdflatex -interaction batchmode # suppress logging to terminal


.PHONY: all

all: packages doc test

# LaTeX source files
# ==================

.PHONY: packages

packages: greek.ldf athnum.sty grmath.sty

greek.ldf: babel-greek.dtx
	tex babel-greek.ins

athnum.sty: athnum.dtx
	tex babel-greek.ins

grmath.sty: athnum.dtx
	tex babel-greek.ins

# Documentation
# =============

.PHONY: doc html pdf test

doc: html pdf

html: babel-greek-doc.html

babel-greek-doc.html: babel-greek-doc.rst
	$(RST2HTML) babel-greek-doc.rst babel-greek-doc.html


pdf: babel-greek.pdf athnum.pdf grmath.pdf usage.pdf

test: test-greek.pdf test-greek-tu.pdf test-greek-8bitcompat.pdf \
      test-greek-ini.pdf \
      test-lgr-fixes.pdf test-case-changing.pdf\
      test-greeknum.pdf test-greeknum-tu.pdf \
      test-athnum.pdf test-athnum-tu.pdf

babel-greek.pdf: babel-greek.dtx
	$(RUBBER) -m hyperref babel-greek.dtx
	makeindex -s gglo.ist -o babel-greek.gls babel-greek.glo
	$(RUBBER) -m hyperref babel-greek.dtx

athnum.pdf: athnum.dtx
	$(RUBBER) athnum.dtx

grmath.pdf: grmath.dtx
	$(RUBBER) grmath.dtx

usage.pdf: usage.tex greek.ldf
	$(RUBBER) usage.tex

test-greek.pdf: test-greek.tex greek.ldf
	$(RUBBER) -m hyperref test-greek.tex

test-greek-tu.pdf: test-greek.tex greek.ldf
	$(RUBBER) -m xelatex --jobname=test-greek-tu test-greek.tex

# requires rubber >= 1.6, otherwise `-m lualatex` is ignored.
test-greek-8bitcompat.pdf: test-greek-8bitcompat.tex test-greek.tex greek.ldf
	$(RUBBER) -m hyperref -m lualatex test-greek-8bitcompat.tex

test-greek-ini.pdf: test-greek-ini.tex test-greek.tex greek.ldf
	$(RUBBER) -m hyperref -m xelatex test-greek-ini.tex

test-case-changing.pdf: test-case-changing.tex greek.ldf
	$(RUBBER) -m hyperref test-case-changing.tex

test-athnum.pdf: test-athnum.tex greek.ldf athnum.sty
	$(RUBBER) test-athnum.tex

test-athnum-tu.pdf: test-athnum.tex greek.ldf athnum.sty
	$(RUBBER) -m xelatex --jobname=test-athnum-tu test-athnum.tex

test-greeknum.pdf: test-greeknum.tex greek.ldf
	$(RUBBER) test-greeknum.tex

test-greeknum-tu.pdf: test-greeknum.tex greek.ldf
	$(RUBBER) -m xelatex --jobname=test-greeknum-tu test-greeknum.tex

test-lgr-fixes.pdf: test-lgr-fixes.tex greek.ldf
	$(RUBBER) -m hyperref -m index test-lgr-fixes.tex


# Installation/Update
# ===================

update: packages test
	$(COPY) *.ldf $(DESTDIR)
	$(COPY) *.sty $(DESTDIR)

update-package: packages doc test
	$(COPY) README.md $(DOCDIR)
	$(COPY) babel-greek-doc.rst $(DOCDIR)
	$(COPY) babel-greek-doc.html $(DOCDIR)
	# selected PDFs
	$(COPY) [!t]*.pdf $(DOCDIR)
	$(COPY) test-greek.pdf $(DOCDIR)
	$(COPY) test-athnum.pdf $(DOCDIR)
	# also copy all sources
	$(COPY) *.dtx $(DOCDIR)
	$(COPY) *.ins $(DOCDIR)
	$(COPY) *.tex $(DOCDIR)
	$(COPY) Makefile $(DOCDIR)


# Cleanup
# =======

.PHONY: clean

clean:
	rm -f *~ *.aux *.bbl *.bcf *.blg *.run.xml *.glo *.gls \
	      *.idx *.ilg *.ind *.log *.out *.rubbercache *.toc
