
# The book's build procedure is much less complicated than it once was,
# but still is not simple.  The remaining complexity comes not because
# the author wanted to break from standard LaTeX book format; he didn't
# and for the most part hasn't.  It is partly for small reasons, like
# the need for an extra digit of section number (10.10 instead of just
# 10.9) in the book's table of contents; but it is also because the
# book---much of which was written many years ago---still extensively
# uses the antique (though nevertheless capable) PSTricks, which a
# newer book would probably eschew in favor of PGF/TikZ.  The book
# resembles other software with long development histories in that
#
#   * it continues to rely to some extent on older techniques recent
#   practice deprecates and * its author, while writing some of the
#   book's older parts years ago, had less mastery of certain
#   techniques than now.
#
# The world does not stand still!  The book has been updated in many
# ways at various times to keep abreast with the times but remains
# subject to the exigencies and mild infirmities to which any long
# development is subject.
#
# Actually, for what it does, the build procedure is a good, clean
# one---or so the author believes.  If you find yourself maintaining
# the book and do not understand at first what the extra parts of the
# build procedure do, then you can remove the parts one by one until
# you reach a comfortable position, and then add the parts back one by
# one as you climb the learning curve.  The build does little or
# nothing in a tricky way.  The author develops on a standard Debian
# platform; you might try that if appropriate and you think that it
# will help.  Good luck.
#
# On the other hand, if you do not yet know make(1), sed(1), bash(1)
# and C++, well, those are standard tools; sorry.  It's hard, but that
# is the way it has to be.  The author has tried to avoid doing things
# in *nonstandard* ways, not to avoid doing them in standard ways!  The
# author has even refrained from inflicting Perl on you, confining its
# use strictly to the optional development helpers (and has refrained
# from inflicting the Autotools even on himself); and of course he has
# written all narrative solely in English.  For certain tasks, one
# needs certain skills; so you will probably need to learn the standard
# tools make(1), sed(1) and bash(1), along with LaTeX and the English
# language of course, decently thoroughly to grok this source.
# (Earlier versions wanted you to read C++, too, but---thanks to
# improvements in the relevant PDF tools---current versions build
# without C++.)
#
# Some comments on the last point seem in order.  If you have peeked at
# various free-software sources from time to time but have not really
# understood most of their content very well, well, it must be admitted
# that some of it is not very understandable.  However, a lot of it is
# understandable; you just have to learn how to understand it.  There
# is no substitute for taking the trouble to learn.  The author stood
# in your place as recently as 1996; he had to learn, too, mostly by
# reviewing sources like the one you are reading now, not
# understanding, going away and reading dense manuals, returning,
# understanding a little more, etc.  Esoteric tools and software
# written in poor style, you can mostly ignore, but you cannot afford
# permanently to ignore sophisticated but standard techniques just
# because they look cryptic to you right now, if you want to learn to
# program free software.  You want to learn increasingly sophisticated
# standard techniques gradually over time.
#
# The author has taken moderate pains to ensure that this particular
# source---outside the *.tex files which, admittedly, are not too
# neat---makes a decent model to study, if improving your ability to
# maintain free software and to contribute to its development happens
# to be your goal.  By design, to read this source requires generally
# relevant rather than generally irrelevant programming skills.
#
# Anyway, back in 1996 the author wished that one of the free-software
# source files he was then trying to understand had taken the trouble to
# explain what the preceding paragraphs have tried to explain.  That
# might have saved the author some grief.  If the paragraphs have
# illuminated anything worth illuminating in your estimation, why, there
# they are, and make of them what you will.
#
#

SHELL     := /bin/bash

out       := derivations
author    := Thaddeus H. Black
class     := book

def       := thb.sty
main0     := main
pics      := pics
bib       := bib.bib
tmpl0     := template
dobd      := dobuilddraft
dobd0     := dobd0.tex
# Possibly confusingly, the "0" in "dobd0" means
# roughly the opposite of the "0" in "main0".

clsdir    := /usr/share/texlive/texmf-dist/tex/latex/base
dobd-null := /dev/null

# Earlier versions of the book directly invoked GhostScript at PDF
# level at 1.4.  Current versions let tools or TEX source set the
# level (see \pdfversion, \pdfcompresslevel and \pdfobjcompresslevel
# in PDFLaTeX's manual) but, in case future versions again directly set
# the level here: the author cannot say definitively at which PDF level
# it is best to build the book.  Plainly, the higher the level, the
# greater the number of readers who will have trouble opening the PDF.
# As it happens, while programming build tools for earlier versions,
# the author read parts of Adobe's PDF Reference 1.7, learning that
# level 1.5 brings some potentially interesting features like image
# transparency, but that level 1.6 brings features of another
# character.  Level 1.6 brings advanced features like encryption,
# interactive form pages and three-dimensional rendering, which are
# complex (thus suspected to break build tools) and probably mostly,
# maybe even wholly, useless to the book.
#
# As far as the writer knows, as of Debian 10 buster in Sept. 2019,
# the PDF standard version defaulted to by tools was 1.5.  (Notes like
# these tend to be read many years after they are written, don't they?
# Well, if the year is 2999, then the writer is long gone, the PDF
# format is probably obsolete, early-21st-century English is perhaps
# ill understood, and so on; so you can update the note as you see fit
# for whatever purpose you have in mind.)
#
#

# The following are for sed(1) to adapt $(cls).  No parameter $(l1a)
# is formally defined, but this only because the corresponding
# length, 1.5em, is not altered; logically, l1a := 1.5em is there.
# Observe that $(l2a) = $(l1a) + $(l1b), and likewise
# that $(l3a) = $(l2a) + $(l2b); otherwise the entries in the book's
# table of contents would not fall visually into proper vertical line.
# Observe however that per the original $(cls0) the pattern does not
# carry through to $(l4a).
p1   := \\newcommand\*\\l@
p2   := \\@dottedtocline
l1b  := 2.5em
l2a  := 4.0em
l2b  := 3.3em
l3a  := 7.3em

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

main        := $(main0).tex
cls0        := $(class).cls
cls         := $(out)-$(cls0)
dobd1       := $(dobd).tex
ch          := $(\
                filter-out $(main) $(def) \
                $(wildcard $(tmpl0)-*.tex), \
                $(wildcard *.tex)\
              )
bibtex      := bibtex    $(main0).aux
makeindex   := makeindex $(main0).idx
exists-dobd := [ -h "$(dobd1)" -a -e "$(dobd1)" ]
dobd-target := $$(readlink "$(dobd1)")
latex        = latex    -shell-restricted $<
pdflatex     = pdflatex -shell-restricted $<

.PHONY: all FORCE clean-for-pdf clean-for-dvi cleanless2 cleanless clean
all : $(out).pdf

$(out).pdf   : $(main) $(def) $(cls) $(ch) $(bib) | clean-for-pdf
	rm -fv $(dobd1) && ln -sv $(dobd-null) $(dobd1)
	$(pdflatex)
	$(bibtex)
	@# Do not invoke $(latex) here but issue the command directly.
	latex -jobname $(main0)-$(pics) $<
	dvips -o $(main0)-$(pics).ps $(main0)-$(pics).dvi
	ps2pdf   $(main0)-$(pics).ps
	$(pdflatex)
	$(makeindex)
	$(pdflatex)
	mv -v $(main0).pdf $@
# You may but need not invoke the %.ps and %.dvi targets, which may not
# do what you expect.  These are not for principal use but are to build
# a quick PS draft without bibliography or index for the author's
# immediate inspection during writing.  (The author knows no way to
# build a quick *PDF* draft as long as PSTricks is used, but the
# quick PS draft serves the purpose.)
$(out).ps    : $(main0).dvi
	dvips -t letter -o $@ $<
$(main0).dvi : $(main) $(def) $(cls) $(ch) $(dobd0) | clean-for-dvi
	rm -fv $(dobd1) && ln -sv $(dobd0) $(dobd1)
	$(latex)
	$(latex)

# Do you like unreadable sed(1) expressions?  Here are some.  Actually,
# if you know sed(1) and also understand the escaping role of the dollar
# sign ($) in make(1), then the expressions are not *quite* as
# unreadable as they look.  [If you do not, then you might learn when
# you have some time: sed(1) and make(1) are standard tools worth
# learning.]  Comments tend to grow outdated, so you might be wary of
# these words, but at the time of this writing the sole purpose of the
# sed(1) operation here is to create and modify a local copy of LaTeX's
# book class, to allow just a tad extra space between the section and
# subsection numbers and their respective titles in the book's table of
# contents (the creator of LaTeX, Leslie Lamport, evidently estimated
# section number 10.10 to be unlikely, but the book Derivations happens
# to have such sections).
$(cls) :
	sed >$(cls) $(clsdir)/$(cls0) \
	  -e 's/^\(\\ProvidesClass{\)$(class)\(}\)[[:space:]]*$$/\1$(out)-$(class)\2/' \
	  -e 's/^\($(p1)\(section\|figure\){$(p2){1}{\)\([^{}]*\)\(}{\)\([^{}]*\)\(}}\)[[:space:]]*$$/\1\3\4$(l1b)\6/' \
	  -e 's/^\($(p1)subsection{$(p2){2}{\)\([^{}]*\)\(}{\)\([^{}]*\)\(}}\)[[:space:]]*$$/\1$(l2a)\3$(l2b)\5/' \
	  -e 's/^\($(p1)subsubsection{$(p2){3}{\)\([^{}]*\)\(}{\)\([^{}]*\)\(}}\)[[:space:]]*$$/\1$(l3a)\3\4\5/'

# Despite that standard PDF tools are now invoked rather than the
# old (now deleted) local 'btool', the source still specially emits
# a *.pgn pagination file during the build.  This *.pgn is unused at
# present but remains interesting, practically harmless, and
# potentially someday again useful; so there seems little reason to
# cease emitting it, even though unused.

# The "clean" and "cleanless" targets are for external use.  To "make
# cleanless" removes the various intermediate TeX working files, but
# leaves intact the final output document files.
clean-for-pdf :
	if $(exists-dobd) && [ "$(dobd-target)" != "$(dobd-null)" ]; then \
	  $(MAKE) cleanless2; \
	fi
clean-for-dvi :
	if $(exists-dobd) && [ "$(dobd-target)" != "$(dobd0)" ]; then \
	  $(MAKE) cleanless2; \
	fi
cleanless2    :
	rm -fv *.{aux,bbl,blg,idx,ilg,ind,log,lof,lot,toc,pgn,out,bak} \
	  *-$(pics).{dvi,ps,pdf}
cleanless     : cleanless2
	rm -fv *.{cls,dvi} $(dobd1)
clean         : cleanless
	rm -fv *.{ps,pdf}

