+### -*-makefile-*-
+###
+### Build script for TeX packages
+###
+### (c) 2020 Mark Wooding
+###
+
+###----- Licensing notice ---------------------------------------------------
+###
+
+###--------------------------------------------------------------------------
+### Extension points.
+
+all::
+.PHONY: all
+
+install::
+.PHONY: install
+
+uninstall::
+.PHONY: uninstall
+
+check::
+.PHONY: check
+
+clean::; rm -f $(CLEANFILES)
+.PHONY: clean
+
+realclean:: clean; rm -f $(REALCLEANFILES)
+.PHONY: clean
+
+distdir:: recreate-distdir
+.PHONY: distdir
+
+###--------------------------------------------------------------------------
+### Project identification.
+
+VERSION := $(shell ./auto-version)
+DISTFILES += auto-version
+
+distdir:: recreate-distdir
+ $(v_at)echo "$(VERSION)" >$(distdir)/RELEASE
+
+###--------------------------------------------------------------------------
+### Tools.
+
+TEX = tex
+DVILATEX = latex
+PDFLATEX = pdflatex
+DVIPS = dvips
+
+###--------------------------------------------------------------------------
+### Installation directories.
+
+## Determine a sensible TeX base directory.
+prefix = /usr/local
+texmfdir := $(shell \
+ for d in $(prefix)/share/texmf $(prefix)/lib/texmf $(prefix)/texmf; do \
+ if [ -d $$d ]; then echo $$d; exit 0; fi; \
+ done; \
+ echo /invalid/)
+
+ifeq ($(texmfdir),/invalid/)
+ $(error "Failed to choose a TeX installation directory.")
+endif
+
+INSTALLDIRS += pkglatex
+pkglatexdir = $(texmfdir)/tex/latex/$(PACKAGE)
+
+INSTALLDIRS += pkgdoc
+pkgdocdir = $(texmfdir)/doc/latex/$(PACKAGE)
+
+###--------------------------------------------------------------------------
+### Preliminary definitions.
+
+.SECONDEXPANSION: # sorry
+
+## Silent-rules.
+V = 0
+
+v_at = $(v_at_$V)
+v_at_0 = @
+
+v_tag = $(call v_tag_$V,$1,$2)
+v_tag_0 = @printf " %-8s %s\n" "$1" "$2";
+
+v_null = $(v_null_$V)
+v_null_0 = >/dev/null
+
+v_quiet = $(v_quiet_$V)
+v_quiet_0 = -q
+
+## Runes for building rules.
+define output-targets
+$$(firstword $$($3_OUT)): $3 $4
+ $$(call v_tag,$2,$$<)$1 $$< $$(v_null)
+$$(wordlist 2,$$(words $$($3_OUT)),$$($3_OUT)): \
+ $$(firstword $$($3_OUT))
+endef
+
+###--------------------------------------------------------------------------
+### Project specific definitions.
+
+include Project.mk
+DISTFILES += Project.mk
+
+###--------------------------------------------------------------------------
+### Parse the `docstrip' installation files.
+
+parse-names = $(shell \
+ sed -n '/^\\mdwgen/,$$ s:^.*\\$1 *{\([^}]*\)}.*$$:\1:p' $2 | sort -u)
+
+define parse-insfile
+$1_DTX := $$(call parse-names,from,$1)
+$1_OUT := $$(call parse-names,mdwf,$1)
+endef
+$(foreach i,$(INS), $(eval $(call parse-insfile,$i)))
+
+DTX += $(sort $(foreach i,$(INS), $($i_DTX)))
+OUT += $(sort $(foreach i,$(INS), $($i_OUT)))
+
+###--------------------------------------------------------------------------
+### Extract output files from the `doc' files.
+
+all:: $(OUT)
+$(foreach i,$(INS), $(eval $(call output-targets,$$(TEX),TEX,$i,$($i_DTX))))
+
+DISTFILES += $(INS)
+DISTFILES += $(sort $(DTX) $(EXTRA))
+CLEANFILES += $(addsuffix .log,$(basename $(INS)))
+DISTFILES += $(OUT)
+REALCLEANFILES += $(OUT)
+
+###--------------------------------------------------------------------------
+### Build the documentation.
+
+## The main rune for running LaTeX.
+##
+## We run LaTeX in a subdirectory to prevent the temporary files (e.g., the
+## `.aux' files) from interfering with each other. The `\jobname' isn't
+## enough, because we might be building DVI and PDF versions of the same
+## document at the same time.
+run-latex = \
+ rm -rf t.$@/ && mkdir t.$@/ && cd t.$@/ && \
+ TEXINPUTS=..:$$TEXINPUTS && export TEXINPUTS && \
+ $1 "\def\indexing{n} \nonstopmode \input $<" $(v_null) && \
+ $1 "\def\indexing{y} \nonstopmode \input $<" $(v_null) && \
+ makeindex $(v_quiet) -s gind.ist $*.idx $(v_null) && \
+ $1 "\def\indexing{n} \nonstopmode \input $<" $(v_null) && \
+ mv $@ ../ && cd ../ && rm -rf t.$@/
+clean::; rm -rf t.*/
+
+## Good old-fashioned DVI.
+DVI = $(addsuffix .dvi,$(basename $(DTX)))
+pkgdoc_FILES += $(DVI)
+all:: $(DVI)
+%.dvi: %.dtx $(OUT) $(EXTRA)
+ $(call v_tag,DVILATEX,$@)$(call run-latex,$(DVILATEX))
+%.dvi: %.tex $(OUT) $(EXTRA)
+ $(call v_tag,DVILATEX,$@)$(call run-latex,$(DVILATEX))
+DISTFILES += $(DVI)
+REALCLEANFILES += $(DVI)
+
+## Build PostScript from the DVI.
+PS = $(addsuffix .ps,$(basename $(DTX)))
+pkgdoc_FILES += $(PS)
+all:: $(PS)
+%.ps: %.dvi
+ $(call v_tag,DVIPS,$@)$(DVIPS) $(v_quiet) -o $@ $<
+DISTFILES += $(PS)
+REALCLEANFILES += $(PS)
+
+## Build PDF using PDFTeX.
+PDF = $(addsuffix .pdf,$(basename $(DTX)))
+pkgdoc_FILES += $(PDF)
+all:: $(PDF)
+%.pdf: %.dtx $(OUT) $(EXTRA)
+ $(call v_tag,PDFLATEX,$@)$(call run-latex,$(PDFLATEX))
+%.pdf: %.tex $(OUT) $(EXTRA)
+ $(call v_tag,PDFLATEX,$@)$(call run-latex,$(PDFLATEX))
+DISTFILES += $(PDF)
+REALCLEANFILES += $(PDF)
+
+###--------------------------------------------------------------------------
+### Installation.
+
+install-targets = $(foreach d,$(INSTALLDIRS), install/$d)
+install:: $(install-targets)
+$(install-targets): install/%: $$($$*_FILES)
+ $(v_at)mkdir -p $(DESTDIR)$($*dir)
+ $(call v_tag,INSTALL,$($*dir))install -m$(or $($*_FILEMODE),644) \
+ $($*_FILES) $(DESTDIR)$($*dir)
+
+uninstall-targets = $(foreach d,$(INSTALLDIRS), uninstall/$d)
+uninstall:: $(uninstall-targets)
+$(uninstall-targets): uninstall/%: $$($$*_FILES)
+ $(call v_tag,UNINSTALL,$($*dir))rm -f \
+ $(addprefix $(DESTDIR)$($*dir)/,$($*_FILES))
+
+###--------------------------------------------------------------------------
+### Distribution.
+
+DISTFILES += COPYING $(wildcard README)
+DISTFILES += Makefile
+
+DISTFILES += debian/control debian/rules
+DISTFILES += debian/copyright debian/changelog
+DISTFILES += debian/compat
+
+distdir = $(PACKAGE)-$(VERSION)
+tarball = $(distdir).tar.gz
+
+distdir:: recreate-distdir $(DISTFILES)
+ $(v_at)set -e; \
+ for i in $(DISTFILES); do \
+ case $$i in */*) mkdir -p $(distdir)/$${i%/*} ;; esac; \
+ ln $$i $(distdir)/$$i; \
+ done
+
+dist: distdir
+ $(call v_tag,TAR,$(tarball))tar chzf $(tarball) $(distdir)
+ $(v_at)rm -rf $(distdir)/
+.PHONY: dist
+CLEANFILES += $(tarball)
+clean::; rm -rf $(distdir)/
+
+recreate-distdir:
+ $(v_at)rm -rf $(distdir)/
+ $(v_at)mkdir $(distdir)
+.PHONY: recreate-distdir
+
+distcheck: dist
+ rm -rf _distcheck/
+ mkdir _distcheck/ && cd _distcheck/ && \
+ tar xzf ../$(tarball) && cd $(distdir)/ && \
+ $(MAKE) check && \
+ $(MAKE) install texmfdir=../_install && \
+ $(MAKE) realclean && \
+ $(MAKE) install texmfdir=../_install && \
+ $(MAKE) uninstall texmfdir=../_install && \
+ $(MAKE) install DESTDIR=../_root && \
+ $(MAKE) uninstall DESTDIR=../_root && \
+ cd ../../ && \
+ find _distcheck/_install/ _distcheck/_root ! -type d | \
+ diff -u - /dev/null && \
+ rm -rf _distcheck
+.PHONY: distcheck
+clean::; rm -rf _distcheck/
+
+###----- That's all, folks --------------------------------------------------