###  -*- mode: Makefile -*-
# #########################################################
# Last update: Tue Apr 16 18:20:59 2002

# Makefile.root.footbib
# ----------------------
# This Makefile is intended for maintaining latex docs having the
# following structure.


#           root file:
#           ----------
#           ...
#           % input-start
#           \input{file-1.tex}
#           \input{file-2.tex}
#           \input{file-3.tex}
#           ...
#           % input-end
#           ...

# i.e. there is a root file which \input's a series of `subfiles'.  It
# is a generalization of the Makefile.summary makefile, with footbib
# support for bibliographic references as footnotes.

# The Makefile handles automagically all the dependencies regarding
# latex source files, gnuplot generated .pslatex images and .eps
# (Encapsulated Postscript) files. Although it could conceivably work for other
# possible operation protocols, the following is the procedure
# for which it surely works (or at least has been designed for).

# * Create a latex root file which \input's all sub files within two
#   markers used by this Makefile:

#           ...
#           % input-start   <<<<Marker for start
#           \input{subfile-1.tex}
#           \input{subfile-2.tex}
#           \input{subfile-3.tex}
#           ...
#           % input-end     <<<<Marker for end
#           ...
# * Each newly added sub file needs only to be \input'd at the root file
#   level, while its dependency will be handled by this Makefile
# * Graphics support is for gnuplot and xfig/psfrags. The gnuplot
#   graphics is to be created with:

#           set term pslatex auxfile
#           set out '2000-07-31a-03.pslatex'
#           ...
#   where the .pslatex extension in the output file is REQUIRED. The
#   xfig/psfrags pictures are to be created within xfig, exported as
#   Encapsulated Postscript files and further processed (maybe) with the
#   psfrags package at the source level
# * Specific edb-* target for dvi/ghostview viewing and printing are
#   introduced in order to support the gb-db-exec-field function
#   (written by myself) for viewing/printing specific records from
#   within EDB
#########################################################
# need an echo which supports echo "\tfoo"
ECHO = echo
# your favorite touch (/usr/local/bin/touch does not work on dschsun1: upgrade?)
TOUCH = /sbin/touch
# 1970 will confuse make on RedHat-7.1, 1971 will not: mysteries
VERY_OLD_DATE = 197101010000
LATEX = latex
# uncomment, eg, if you want to include png images
# LATEX = pdflatex
LATEX_OPT=
DVIPS_OPT =
PS2PDF_OPT =
A2PS_OPTS = -2 -STumble=true
N_COPIES = 1
PS_VIEWER = gv
# PDF_VIEWER = xpdf
PDF_VIEWER = mupdf
ADDITIONAL_REMOVES=
# TEX_ROOT is the name of the root latex file *without* the .tex extension
TEX_ROOT=000-root

WEB_INSTALLER=./web-installer
WEB_INSTALLER_ARGS=

# .make.vars must already exist
# (just `touch .make.vars' if it does not exist)
# .make.vars is not intended for editing: it is written by the collect target
-include ./.make.vars


# local customizations are in file .make.local, which MUST exist (in
# any case!) in current directory
-include ./.make.local

# different action depending on the LATEX value
# LATEX==latex             --> tex->dvi->ps->pdf
# LATEX==pdflatex|lualatex --> tex->pdf
PDF_STRAIGHT=$(if $(filter pdflatex lualatex,$(LATEX)),true,false)


default : collect
	test $(PDF_STRAIGHT) = false && make dvi || :
	test $(PDF_STRAIGHT) = true && make pdf-from-tex || :


dvi : collect
	test $(LATEX) = latex && make $(TEX_ROOT).dvi || :

dvi! :
	test $(LATEX) = latex && { \
	make xclean;\
	make dvi;\
	};\
	:

# do this only when LATEX==latex
ifeq ($(LATEX),latex)
$(TEX_ROOT).dvi : $(TEX_INCLUDES)\
	$(PSLATEX_FILES) \
	$(EPS_FILES) \
	$(TEX_ROOT).aux \
	$(BBL_FILES)


	$(LATEX) $(LATEX_OPT) $(TEX_ROOT);\
	if [ $$? -ne 0 ]; then \
	sleep 1;\
	$(TOUCH) $(TEX_ROOT).aux;\
	fi
	@#
	@# try to guess if we have to run bibtex
	@#
	make bibtex
	@#
	@# run latex until references and labels are OK
	@# handle also hyperref's rerun's
	@#
	while ( egrep --text 	-e'Rerun to get cross-references' \
			-e'hyperref Warning: Rerun' \
		$(TEX_ROOT).log 1>/dev/null 2>&1; \
	[ $$? -eq 0 ] ) ;\
	do \
	$(LATEX) $(LATEX_OPT) $(TEX_ROOT);\
	done
	@#
	@# run latex until table of contents is OK
	@#
	while ( egrep --text 'No file '$(TEX_ROOT)'\.toc' $(TEX_ROOT).log \
		1>/dev/null 2>&1; \
	[ $$? -eq 0 ] ) ;\
	do \
	$(LATEX) $(LATEX_OPT) $(TEX_ROOT);\
	done
	if [ $$? -ne 0 ]; then \
	sleep 1;\
	$(TOUCH) $(TEX_ROOT).aux;\
	fi
	@# If there are no warnings, then `egrep' will return with an
	@# error status (1, non zero). This will cause `make' to
	@# give up on the current rule. The no-op command `:' after
	@# `egrep' avoids this.
	egrep --text '[Ww]arning' $(TEX_ROOT).log;:
	@# spot out any error if we run in \nonstopmode
	@echo
	@egrep --text '^ *!' $(TEX_ROOT).log || \
	echo "no errors in $(TEX_ROOT).log: good";:
	@echo
else
$(TEX_ROOT).dvi :
	:
endif


# do this only when LATEX==pdflatex
ifeq ($(PDF_STRAIGHT),true)
$(TEX_ROOT).pdf : $(TEX_INCLUDES)\
	$(PSLATEX_FILES) \
	$(EPS_FILES) \
	$(TEX_ROOT).aux \
	$(BBL_FILES)


	test $(PDF_STRAIGHT) = true && { \
	$(LATEX) $(LATEX_OPT) $(TEX_ROOT);\
	if [ $$? -ne 0 ]; then sleep 1;\
	$(TOUCH) $(TEX_ROOT).aux;\
	fi;\
	make bibtex;\
	while ( egrep --text 	-e'Rerun to get cross-references' \
			-e'hyperref Warning: Rerun' \
		$(TEX_ROOT).log 1>/dev/null 2>&1; \
	[ $$? -eq 0 ] ) ;\
	do $(LATEX) $(LATEX_OPT) $(TEX_ROOT);\
	done ;\
	while ( egrep --text 'No file '$(TEX_ROOT)'\.toc' $(TEX_ROOT).log \
		1>/dev/null 2>&1; \
	[ $$? -eq 0 ] ) ;\
	do $(LATEX) $(LATEX_OPT) $(TEX_ROOT);\
	done ;\
	if [ $$? -ne 0 ]; then sleep 1;\
	$(TOUCH) $(TEX_ROOT).aux;\
	fi;\
	egrep --text '[Ww]arning' $(TEX_ROOT).log;:;\
	echo;\
	egrep --text '^ *!' $(TEX_ROOT).log || \
	echo "no errors in $(TEX_ROOT).log: good";:;\
	echo;\
	};\
	:
# otherwise, ps -> pdf (via ps2pdf)
else
$(TEX_ROOT).pdf :
	make pdf-from-ps
endif

# This wouldn't be needed for normal operation. HOWEVER, it very often
# happens to me that, when I create a new source file to be included by
# the root file, I cut'n'paste from the list of already included files
# and often it ends up with, e.g.:
#
#               \input{005-buffers.tex}
#               \input{006-buffers.tex}
#                          ^^^^^^^
# instead of, for instance:
#
#               \input{005-buffers.tex}
#               \input{006-titration.tex}
#                          ^^^^^^^^^
# i.e., I update the number, but I forget to update the string.
#
# The consequence is that the root file will try to include a
# non-existent .tex file (006-buffers.tex). This, in turn, will make
# make try to find a rule for the non-existent file, which will make
# it fall back onto the .DEFAULT target, which then will set up an
# infinite loop.
#
# Putting this target will not hurt normal operation, while avoiding the
# infinite loop.
$(TEX_INCLUDES):


collect :
	test ! -f ./$(TEX_ROOT).tex && { \
	echo;echo;\
	echo "No ./$(TEX_ROOT).tex found: generate/edit .make.local";\
	echo;echo;\
	exit 1;\
	} || :
	rm -f .make.vars
	@#
	@# catch included tex files in $(TEX_ROOT)
	@# included files MUST be between the lines:
	@# `% input-start' and `% input-end'
	@# NOTE: must group  `tex_includes=...' with the `for' loop,
	@# otherwise the shell variable will not persist across different
	@# command lines
	@#
	@# 2008-11-05
	tex_includes=`ls *.tex 2>/dev/null`;\
	$(ECHO) "TEX_INCLUDES = \\" >> .make.vars;\
	for f in $${tex_includes};do \
	$(ECHO) "\t$$f \\" >> .make.vars;\
	done;:
	$(ECHO) >> .make.vars
	@#
	@# if a .gnu file exists and a corresponding
	@# .pslatex/.pstex/.eps file does not exist, then we touch
	@# a .pslatex file with old date (Jan 1st, 1970). This will enforce
	@# a gnuplot run on the .gnu file (see the .gnu.pslatex implicit rule)
	@# NOTE: this may not always do what we want.
	@#
	@#
	@# The following was  not a good idea:
	@# 	for f in *.gnu;do \\
	@# \"if no file name is found matching the pattern then
	@# the pattern is passed, unchanged, as an argument\" .
	@# This means that the for loop is
	@# executed even if no *.gnu files are present, with f='*.gnu';
	@# this will lead to generation of the file '*.pslatex', which
	@# is not really what we want
	@# This will work properly, but necessitates of some caution.
	@# In fact:
	@# if there no `.gnu' files are present, then the `for' loop would
	@# return 1 (non zero status, error): this would cause `make' to
	@# give up on the current rule. The no-op command `:' after the
	@# loop closing `done' avoids this.
	for f in `ls *.gnu 2>/dev/null`;do \
	pslatex_file=`basename $$f .gnu`.pslatex;\
	eps_file=`basename $$f .gnu`.eps;\
	pstex_file=`basename $$f .gnu`.pstex;\
	if [ ! -f $$pslatex_file -a ! -f $$eps_file -a ! -f $$pstex_file ];\
	then $(TOUCH) -am -t $(VERY_OLD_DATE) $$pslatex_file;\
	fi;\
	done;:
	@#
	@# collect .pslatex file names
	@#
	$(ECHO) "PSLATEX_FILES = \\" >> .make.vars
	for f in `ls *.pslatex 2>/dev/null`;do \
	$(ECHO) "\t$$f \\" >> .make.vars;\
	done;:
	$(ECHO) >> .make.vars
	@#
	@# collect .eps file names
	@#
	$(ECHO) "EPS_FILES = \\" >> .make.vars
	for f in `ls *.eps 2>/dev/null`;\
	do $(ECHO) "\t$$f \\" >> .make.vars;\
	done;:
	$(ECHO) >> .make.vars
	@#
	@# collect bbl files
	@#
	$(ECHO) "BBL_FILES = \\" >> .make.vars
	for f in `ls *.bbl 2>/dev/null`;\
	do $(ECHO) "\t$$f \\" >> .make.vars;\
	done;:
	$(ECHO) >> .make.vars


bibtex :
	@#
	@# try to guess if we have to run bibtex: if so, handle the case
	@# in which we have used the {footbib,bibunits,multibib} packages
	@#
	@# First, strip all newlines from $(TEX_ROOT).log, as they
	@# will disturb egrep
	cat $(TEX_ROOT).log|tr -d '\n' > $(TEX_ROOT).log.no-newlines
	@# ---------------
	@# Footbib support
	@# ---------------
	egrep --text 'Package footbib Warning: Citation.*on page.*undefined' \
	$(TEX_ROOT).log.no-newlines 1>/dev/null 2>&1 && { \
	for f in `ls *.fb.aux 2>/dev/null` $(TEX_ROOT).aux;\
	do bibtex `basename $$f .aux`;\
	done; \
	} || :
	@# ---------------
	@# Bibunits support
	@# ---------------
	egrep --text 'Citation .*undefined on input line'  \
	$(TEX_ROOT).log.no-newlines 1>/dev/null 2>&1 && { \
	for f in `ls bu*.aux 2>/dev/null` $(TEX_ROOT).aux;\
	do bibtex `basename $$f .aux`;\
	done; \
	} || :
	@# ---------------
	@# Multibib support
	@# ---------------
	egrep --text 'Citation .*undefined on input line'  \
	$(TEX_ROOT).log.no-newlines 1>/dev/null 2>&1 || \
	egrep --text '^No file bu[0-9][0-9]*\.bbl\.'  \
	$(TEX_ROOT).log.no-newlines 1>/dev/null 2>&1 && { \
	for f in `ls *.aux 2>/dev/null` $(TEX_ROOT).aux;\
	do bibtex `basename $$f .aux`;\
	done; \
	} || :
	@# House keeping
	rm -f $(TEX_ROOT).log.no-newlines
	@# Run latex twice after bibtex
	$(LATEX) $(LATEX_OPT) $(TEX_ROOT);\
	$(LATEX) $(LATEX_OPT) $(TEX_ROOT);\
	egrep --text '[Ww]arning' $(TEX_ROOT).log || : ;\
	:


$(TEX_ROOT).aux:

$(TEX_ROOT).bbl:

$(TEX_ROOT).ps : $(TEX_ROOT).dvi
	test $(LATEX) != pdflatex && { \
	dvips -N0 $(DVIPS_OPT) $(TEX_ROOT)  ;\
	echo ;\
	egrep --text '^ *!' $(TEX_ROOT).log || \
	echo "no errors in $(TEX_ROOT).log: good";: ;\
	echo ;\
	};\
	:



ps : collect
	test $(LATEX) != pdflatex && { \
	make $(TEX_ROOT).ps;\
	echo;\
	egrep --text '^ *!' $(TEX_ROOT).log || \
	echo "no errors in $(TEX_ROOT).log: good";:;\
	echo;\
	};\
	:

ps! :
	test $(LATEX) != pdflatex && { \
	make xclean;\
	make ps;\
	};\
	:

viewps psview view-ps ps-view :
	test $(LATEX) != pdflatex && { \
	make ps ;\
	( $(PS_VIEWER) $(TEX_ROOT).ps & ) ;\
	};\
	:

# EDB version: no background
edb-viewps edb-psview : ps
	$(PS_VIEWER) $(TEX_ROOT).ps

# handle cases when either LATEX=pdflatex or LATEX=latex
pdf : collect
	test $(PDF_STRAIGHT) = false && make pdf-from-ps || :
	test $(PDF_STRAIGHT) = true && make pdf-from-tex || :

# when LATEX=pdflatex
pdf-from-tex : $(TEX_ROOT).pdf

# when LATEX=latex
pdf-from-ps : $(TEX_ROOT).ps
	ps2pdf $(PS2PDF_OPT)  $(TEX_ROOT).ps
	touch pdf-from-ps
	@echo
	@egrep --text '^ *!' $(TEX_ROOT).log || \
	echo "no errors in $(TEX_ROOT).log: good";:
	@echo

pdf! :
	make xclean;\
	make pdf;\
	:

viewpdf pdfview : pdf
	case $(PDF_VIEWER) in \
	xpdf) \
	$(PDF_VIEWER) -raise -remote `pwd|tr -d /` $(TEX_ROOT).pdf & \
	;; \
	mupdf) \
	$(PDF_VIEWER) $(TEX_ROOT).pdf & \
	;; \
	esac ;\
	:

reloadpdf pdfreload : pdf
	case $(PDF_VIEWER) in \
	xpdf) \
	$(PDF_VIEWER) -remote `pwd|tr -d /` -raise -reload \
	;; \
	mupdf) \
	the_pid=$$(lsof|egrep mupdf|egrep $(TEX_ROOT).pdf|awk '{print $$2}') ;\
	kill -HUP $${the_pid} \
	;; \
	esac ;\
	:


# print lpr : dvi
# 	dvips $(DVIPS_OPT) -o\|lpr $(TEX_ROOT).dvi

print lpr copies : dvi
	counter=0 ;\
	while test $$counter -lt $(N_COPIES) ;\
	do \
	dvips $(DVIPS_OPT) -o\|lpr $(TEX_ROOT) ;\
	counter=`expr $$counter + 1` ;\
	done

a2ps : dvi
	counter=0 ;\
	while test $$counter -lt $(N_COPIES) ;\
	do \
	a2ps $(A2PS_OPTS) $(TEX_ROOT).dvi ;\
	counter=`expr $$counter + 1` ;\
	done


view : dvi
	xdvi $(TEX_ROOT) &

# EDB version: no background
edb-view : dvi
	xdvi $(TEX_ROOT)


ifeq ($(LATEX),latex)
whole all complete : input-all
	make 000-all.dvi
	@# 000-all.dvi always remade
	mv 000-all.dvi 000-all-dvi
	rm -f 000-all.aux 000-all.log 000-all.ps 000-all.bbl 000-all.blg\
		000-all.toc 000-all.tex
else
whole all complete : input-all
	make TEX_ROOT=000-all pdf
	mv 000-all.pdf 000-all-pdf
	rm -f 000-all.ps 000-all.log
	rm -f 000-all{,.fb}.aux 000-all{,.fb}.bbl 000-all{,.fb}.blg
	rm -f 000-all.toc 000-all.tex
	rm -f 000-all.pre 000-all.out 
endif

input-all :
	rm -f 000-all.tex
	sed -e '/% input-start/,$$d' $(TEX_ROOT).tex > 000-all.tex
	echo "% input-start" >> 000-all.tex
	sed -n -e '/% input-start/,/% input-end/p' $(TEX_ROOT).tex |\
	sed -e '/% input-start/d'  |\
	sed -e '/% input-end/d'  |\
	sed -E -e 's|^[[:space:]%]+||' |\
	sed -e 's|^ *||'>> 000-all.tex
	sed -n -e '/% input-end/,$$p' $(TEX_ROOT).tex >> 000-all.tex
	sed -i -e '/^.*%%* *allIgnore.*$$/d' 000-all.tex
	make TEX_ROOT=000-all collect


ifeq ($(LATEX),latex)
000-all.dvi : $(TEX_INCLUDES)\
	$(PSLATEX_FILES) \
	$(EPS_FILES) \
	000-all.aux \
	$(BBL_FILES) \


	$(LATEX) $(LATEX_OPT) 000-all;\
	if [ $$? -ne 0 ]; then \
	sleep 1;\
	$(TOUCH) 000-all.aux;\
	fi
	@#
	@# try to guess if we have to run bibtex
	@#
	make bibtex
	@#
	@# run latex until references and labels are OK
	@# handle also hyperref's rerun's
	@#
	while ( egrep --text 	-e'Rerun to get cross-references' \
			-e'hyperref Warning: Rerun' \
		000-all.log 1>/dev/null 2>&1; \
	[ $$? -eq 0 ] ) ;\
	do \
	$(LATEX) $(LATEX_OPT) 000-all;\
	done
	@#
	@# run latex until table of contents is OK
	@#
	while ( egrep --text 'No file '000-all'\.toc' 000-all.log \
		1>/dev/null 2>&1; \
	[ $$? -eq 0 ] ) ;\
	do \
	$(LATEX) $(LATEX_OPT) 000-all;\
	done
	if [ $$? -ne 0 ]; then \
	sleep 1;\
	$(TOUCH) 000-all.aux;\
	fi
	@# If there are no warnings, then `egrep' will return with an
	@# error status (1, non zero). This will cause `make' to
	@# give up on the current rule. The no-op command `:' after
	@# `egrep' avoids this.
	egrep --text '[Ww]arning' 000-all.log;:
	@# spot out any error if we run in \nonstopmode
	@echo
	@egrep --text '^ *!' 000-all.log || \
	echo "no errors in 000-all.log: good";:
	@echo

000-all.aux:

000-all.bbl:
else
000-all.dvi :
	:
endif

# this uncomments all `\input's' between `% input-start'
# and `% input-end' in 000_summary.tex
a-z :
	sed -e '/% input-start/,$$d' $(TEX_ROOT).tex > 000-all.tex
	echo "% input-start" >> 000-all.tex
	sed -n -e '/% input-start/,/% input-end/p' $(TEX_ROOT).tex |\
	sed -e '/% input-start/d'  |\
	sed -e '/% input-end/d'  |\
	sed -E -e 's|^[[:space:]%]+||' |\
	sed -e 's|^ *||'>> 000-all.tex
	sed -n -e '/% input-end/,$$p' $(TEX_ROOT).tex >> 000-all.tex
	mv -f 000-all.tex $(TEX_ROOT).tex

ifeq ($(LATEX),latex)
viewall allview all-view view-all:
	@if test -f 000-all-dvi;\
	then xdvi 000-all-dvi & \
	else $(ECHO) "000-all-dvi has to be built with \`make all'" ;\
	fi


printall allprint print-all all-print lprall lpr-all :
	lpr 000-all-dvi
endif

force ! : collect
	$(LATEX) $(LATEX_OPT) $(TEX_ROOT)
	if [ $$? -ne 0 ]; then \
	sleep 1;\
	$(TOUCH) $(TEX_ROOT).aux;\
	fi
	@#
	@# try to guess if we have to run bibtex
	@#
	if [ "`sed -n -e '/Citation/p' $(TEX_ROOT).log| \
	sed -n -e '/undefined/p'`" != "" ]; then \
	bibtex $(TEX_ROOT);\
	$(LATEX) $(LATEX_OPT) $(TEX_ROOT);\
	fi
	@#
	@# run latex until references and labels are OK
	@#
	while ( egrep --text 'Rerun' $(TEX_ROOT).log 1>/dev/null 2>&1; \
	[ $$? -eq 0 ] ) ;\
	do \
	$(LATEX) $(LATEX_OPT) $(TEX_ROOT);\
	done
	if [ $$? -ne 0 ]; then \
	sleep 1;\
	$(TOUCH) $(TEX_ROOT).aux;\
	fi
	@# Have to do like this: don't know why
	(egrep --text '[Ww]arning' $(TEX_ROOT).log;\
	if [ $$? -ne 0 ]; then \
	:;\
	fi)
	@# spot out any error if we run in \nonstopmode
	@echo
	@egrep --text '^ *!' $(TEX_ROOT).log || \
	echo "no errors in $(TEX_ROOT).log: good";:
	@echo

viewtest : test.ps
	$(PS_VIEWER) test.ps &

test.ps : test.dvi
	dvips -N0 test.dvi

test.dvi : test.tex test.pslatex
	(\
	$(LATEX) $(LATEX_OPT) test;\
	if [ $$? -ne 0 ]; then \
	sleep 1;\
	$(TOUCH) test.aux;\
	fi;\
	while ( egrep --text 'Rerun' test.log 1>/dev/null 2>&1; \
	[ $$? -eq 0 ] ) ;\
	do \
	$(LATEX) $(LATEX_OPT) test;\
	done;\
	if [ $$? -ne 0 ]; then \
	sleep 1;\
	$(TOUCH) test.aux;\
	fi;\
	egrep --text '[Ww]arning' test.log;\
	if [ $$? -ne 0 ]; then \
	:;\
	fi;\
	)


test.pslatex : test.gnu test.dat
	gnuplot test.gnu

#
# install something in WEB space
# WEB installation is too specific: each project will have its own
# web installer
#
web-install webinstall installweb install-web :
	test -x $(WEB_INSTALLER) && \
	$(WEB_INSTALLER) $(WEBINSTALLER_ARGS) ||\
	:


clean :
	rm -f $(TEX_ROOT).dvi $(TEX_ROOT).log $(TEX_ROOT).ps test.dvi test.ps
	rm -f $(TEX_ROOT).pdf
	rm -f *.bak

xclean :
	rm -f *.dvi *.log *.bak *.aux *.bbl *.blg $(TEX_ROOT).ps *.pslatex
	rm -f egpl-*.gnuplot  # our egpl
	rm -f $(TEX_ROOT).toc
	rm -f $(TEX_ROOT).pdf
	rm -f $(TEX_ROOT).out # hyperref
	rm -rf $(TEX_ROOT)
	rm -f pdf-from-ps
	test "$(ADDITIONAL_REMOVES)" != "" && rm -rf $(ADDITIONAL_REMOVES)||:

checklog cl :
	egrep --text '^ *!' $(TEX_ROOT).log;:

# This is the default target: we use it for generating a dvi file
# containing only selected input entries of the root latex
# file. E.g. suppose the root file inputs the following:
# 	  % input-start
# 	  \input{000intro.tex}
# 	  \input{1416.tex}
# 	  \input{1418.tex}
# 	  \input{1421.tex}
# 	  % input-end
# and we want to have only the parts of the document corresponding to
# input files 000intro.tex 1416.tex and 1421.tex. Then we just say:
#     make 000intro 1416 1421

.DEFAULT :
	ON_LIST= ;\
	for token in $(MAKECMDGOALS);do \
	egrep "^[% ]*.input *{.*$${token}.*\.tex}" $(TEX_ROOT).tex || continue;\
	the_source="$$(egrep "^[% ]*.input *{.*$${token}.*\.tex}" \
	$(TEX_ROOT).tex | \
	sed -e's|^[% ]*.input *{||' | \
	sed -e's|}.*$$||'|tr '\n' ' ')" ;\
	ON_LIST="$${the_source} $${ON_LIST}" ;\
	done ;\
	test "$${ON_LIST}" != "" && { \
	make ON_LIST="$${ON_LIST}" switch-on ;\
	 } ;\
	sed -n -e'/% input-start/,/% input-end/p' $(TEX_ROOT).tex;\
	:

# Comment all the input entries in the root file
# Then, uncomment the entries in ON_LIST
switch-on :
	if test $(ON_LIST)_ = _;then exit 0; fi ;\
	make comment-all ;\
	for f in $(ON_LIST) ;\
	do sed -e '/% input-start/,$$d' $(TEX_ROOT).tex > 000-tmp.tex ;\
	sed -n -e '/% input-start/,/% input-end/p' $(TEX_ROOT).tex |\
	sed -E -e'/input\{'$$f'/ s/^[[:space:]%]+//' -e'/input\{'$$f'/ s/^  *//' \
			>> 000-tmp.tex ;\
	sed -n -e '/% input-end/,$$p' $(TEX_ROOT).tex |\
	sed -e '/% input-end/d' >> 000-tmp.tex;\
	mv -f 000-tmp.tex $(TEX_ROOT).tex;\
	done

comment-all :
	sed -e '/% input-start/,$$d' $(TEX_ROOT).tex > 000-all.tex
	echo "% input-start" >> 000-all.tex
	sed -n -e '/% input-start/,/% input-end/p' $(TEX_ROOT).tex |\
	sed -e '/% input-start/d'  |\
	sed -e '/% input-end/d'  |\
	sed -e 's|^[ \t]*%%*||' |\
	sed -e 's|^ *||' | sed -e's|^|%% |'>> 000-all.tex
	sed -n -e '/% input-end/,$$p' $(TEX_ROOT).tex >> 000-all.tex
	mv -f 000-all.tex $(TEX_ROOT).tex

# These 3 targets solve the following problem:
# => link this makefile in an empty dir
# => type make: an endless loop starts
# The explanation:
# for some (unknown to me) reason, `include some_file' creates the target
# `some_file' (try echo $@)
# If the target `some_file' is not explicit, the target .DEFAULT is
# run. Since the target .DEFAULT (above) runs make recursively, an
# infinite loop results because every make run include's some_file
# which is stuffed (for the same unknown reason) into $@
# This evilish loop can be interrupted by placing explicit (dummy)
# rules for some_file, which is what we do below:


.make.vars :

.make.local :
	mv -f .make.local .make.local.`date +"%Y-%m-%d@%H-%M-%S"` \
		2>/dev/null || :
	echo "# need an echo which supports echo \"\\tfoo\"" >> .make.local
	echo "ECHO = echo -e" >> .make.local
	echo "TOUCH = /bin/touch" >> .make.local
	echo "VERY_OLD_DATE = 197101010000" >> .make.local
	echo "LATEX = latex" >> .make.local
	echo "# uncomment, eg, if you want to include png graphics" >> .make.local
	echo "# LATEX = pdflatex" >> .make.local
	echo "LATEX_OPT = -shell-escape" >> .make.local
	echo "DVIPS_OPT =" >> .make.local
	echo "PS2PDF_OPT =" >> .make.local
	echo "A2PS_OPTS = -2 -STumble=true" >> .make.local
	echo "N_COPIES = 1" >> .make.local
	echo "PS_VIEWER = gv" >> .make.local
	echo "PDF_VIEWER = mupdf" >> .make.local
	echo "ADDITIONAL_REMOVES =" >> .make.local
	echo "WEB_INSTALLER = ./web-installer" >> .make.local
	echo "WEB_INSTALLER_ARGS =" >> .make.local
	echo "TEX_ROOT = 000-root" >> .make.local

.make.targets :

# allow additional (local) targets
-include .make.targets

.SUFFIXES:
.SUFFIXES:       .pslatex .gnu .ps .dvi .eps .tex .pdf

.PHONY: clean xclean

.gnu.pslatex:
	gnuplot $<


.gnu.eps:
	@#
	@# if foo.gnu foo.fig and foo.eps all exist,
	@# then it is likely (but this may not always do what we want)
	@# that foo.eps was created from xfig (from foo.fig
	@# created by gnuplot). If so, don't `gnuplot foo.gnu'
	@#
	if [ ! -f `basename $< .gnu`.fig ];\
	then \
	gnuplot $<;\
	fi

ifeq ($(LATEX),latex)
.tex.dvi:
	$(LATEX) $(LATEX_OPT) $<
	@# spot out any error if we run in \nonstopmode
	@echo
	@egrep --text '^ *!' $$(basename $< .tex).log || \
	echo "no errors in $$(basename $< .tex).log: good";:
	@echo
else
.tex.dvi:
	:
endif

ifeq ($(LATEX),latex)
.dvi.ps:
	dvips -N0 $(DVIPS_OPT)  $<
	@# spot out any error if we run in \nonstopmode
	egrep --text '^ *!' $$(basename $< .dvi).log;:
else
.dvi.ps:
	:
endif

ifeq ($(LATEX),latex)
.ps.pdf:
	ps2pdf $(PS2PDF_OPT)  $<
	@# spot out any error if we run in \nonstopmode
	@echo
	@egrep --text '^ *!' $$(basename $< .ps).log || \
	echo "no errors in $$(basename $< .ps).log: good";:
	@echo
else
.ps.pdf:
	:
endif

ifeq ($(PDF_STRAIGHT),true)
.tex.pdf:
	$(LATEX) $(LATEX_OPT) $<
	@# spot out any error if we run in \nonstopmode
	@echo
	@egrep --text '^ *!' $$(basename $< .tex).log || \
	echo "no errors in $$(basename $< .tex).log: good";:
	@echo
else
.tex.pdf:
	:
endif
