# Makefile for "The Complete FreeBSD" and related documents.
# Greg Lehey, LEMIS
#
# $Id: Makefile,v 2.47 2006/02/23 23:16:43 grog Exp grog $
#

# XXX while this is in the wrong place
F	=	/home/Book/FreeBSD-4
GS	=	gs

# The symbolic tag of the currently printed edition, used
# for creating errata
EDITION = fourth_edition

# Set this for debugging *roff macros
ROFFDEBUG = -b

# HEIGHT is the real height of the paper + .2" (XXX until we fix the macros)
HEIGHT.small = 9i
HEIGHT.long = 9i
HEIGHT.network = 9i
HEIGHT.bisdn = 10i
HEIGHT.ii = 10i
HEIGHT.workshop = 11i
HEIGHT.complete = 9.1875i
# XXX kludge for strange differences between individual chapters and entire book.
# XHEIGHT.complete = 9.33i
XHEIGHT.complete = 9.2i
HEIGHT.kernel = 11.9i
# For comparison with last edition HEIGHT.complete = 9i

# Margin width
MARGINS = 2
# real paper width less margins, which is now only 5.875", dammit
WIDTH.small = 4.475i
WIDTH.long = 4.475i
WIDTH.network = 4.475i
WIDTH.bisdn = 5.8i
WIDTH.workshop = 5.8i
# Well, we can hope...
WIDTH.complete = 5i
WIDTH.kernel = 7.1i
# For comparison with last edition WIDTH.complete = 4.475i

# Set the origin relocation, in points, for the finished output.  This is pretty non-critical,
# but these values give crop marks which are visible with Ghostscript
Y_ORIGIN	=	45
X_ORIGIN	=	65

FTP	=	/echunga/var/spool/ftp/.../cfbsd

TEXTSIZE = 10
LINESPACING = 12

# Point size for Dx command
EXAMPLE-SIZE = 8

SIZES = short long network bisdn complete kernel workshop
# Which book?   Should be one of ${SIZES}, or override on the invocation line
SIZE?= complete
TAGS.complete= complete long
TAGS.workshop=	workshop

SHELL = /bin/sh
XCHAR=${TOOLS}/xchar

.SUFFIXES: .mm .ps .psl .PS .txt .roff .html .ascii .pdf .ctoc

# get site-specific paths
include paths.make

PREFACE = foreword.mm 10-years.mm preface.mm

# The chapter source files
CHAPTERS.bisdn = bisdn.mm
CHAPTERS.ii = ii.mm

CHAPTERS.short = introduction.mm concepts.mm quickinstall.mm install.mm \
	   shareinstall.mm problems.mm xsetup.mm xtheory.mm unixref.mm disks.mm building.mm

# XXX This is AFU
CHAPTERS.network = netintro.mm phones.mm isp.mm netsetup.mm modems.mm ppp.mm  \
		   ii.mm isdn.mm netdebug.mm nfs.mm dickless.mm dns.mm \
		   routing.mm email.mm news.mm www.mm

CHAPTERS.long = introduction.mm concepts.mm quickinstall.mm install.mm postinstall.mm \
	shareinstall.mm problems.mm xsetup.mm xtheory.mm unixref.mm \
	disks.mm building.mm


XCHAPTERS.complete =  introduction.mm  concepts.mm \
		     quickinstall.mm shareinstall.mm install.mm \
		     postinstall.mm unixref.mm unixadmin.mm ports.mm filesys.mm \
		     disks.mm vinum.mm burncd.mm tapes.mm \
		     printers.mm netintro.mm netsetup.mm \
		     isp.mm modems.mm ppp.mm dns.mm firewall.mm \
		     netdebug.mm netclient.mm netserver.mm mua.mm \
		     mta.mm xtheory.mm starting.mm configfiles.mm \
		     current.mm upgrading.mm

LCHAPTER.complete = building.mm 

CHAPTERS.complete = ${XCHAPTERS.complete} ${LCHAPTER.complete}

# To work on:
# Ones left out of the second edition for lack of time: mobile.mm, international.mm
# security.mm 
# micronet.mm

CHAPTERS.workshop = install.mm postinstall.mm

CHAPTERS = ${CHAPTERS.${SIZE}}
XCHAPTERS = ${XCHAPTERS.${SIZE}}
LCHAPTER = ${LCHAPTER.${SIZE}}

# Appendices
APPENDICES.short = packages.mm biblio.mm getting.mm licenses.mm
APPENDICES.long = packages.mm biblio.mm getting.mm licenses.mm
APPENDICES.network = 
APPENDICES.complete =  biblio.mm evolution.mm 
# APPENDICES.long =  packages.mm biblio.mm hardware.mm getting.mm hwconfig.mm \
#	difference.mm licenses.mm

APPENDICES = ${APPENDICES.${SIZE}}

DIRECTORIES = ${SIZE} ${SIZE} ${SIZE}/Toc ${SIZE}/txt ${SIZE}/ps \
	      ${SIZE}/nps ${SIZE}/pdf ${SIZE}/roff

# The final documents
PSDOCS = ${SIZE}/chapters.ps ${SIZE}/appendix.ps ${SIZE}/ps/toc.ps

TXTDOCS = ${SIZE}/chapters.txt ${SIZE}/appendix.txt ${SIZE}/txt/toc.txt

# and files that we create during the build
AUXFILES = ${SIZE}/toc.mm ${SIZE}/tables.mm ${SIZE}/index.mm ${SIZE}/ids.mm ${SIZE}/Xref \
	${SIZE}/chapternumbers packages_body.mm

VPATH = ${SIZE}

# The sources for all chapters
SOURCES = ${CHAPTERS} ${APPENDICES} 

# The sources for chapters, index and TOC
ALL-SOURCES = ${PREFACE} ${CHAPTERS} ${APPENDICES} ${AUXFILES} global.mm reset.mm grog.mm

# The output files with toc, index and crossref information
TOCFILES = ${SIZE}/Toc/toc ${SIZE}/Toc/book ${SIZE}/Toc/appendix

# And the corresponding version for html output
TOCFILESH = ${SIZE}/Toc/${SOURCES:.mm=.html}

# Sources to be put in review archive
REVIEW-SOURCES =  ${SIZE}/chapters.ps ${SIZE}/ps/toc.ps ${SOURCES}

# All files to be maintained by RCS
RCS-FILES = ${PREFACE} ${SOURCES} Makefile makebookmakefile paths.make tidy.el grog.mm \
	merge getch filter getchapternumbers freebie.sty title.${SIZE} \
	makeshortsect makebisdnsect makenetworksect makelongsect makesect errata \
	mkpackages mkpackages-sorted todo backcover mkthumbnail INDEX \
	mkids global.mm reset.mm dear-reviewer-1 dear-reviewer-2 strip2nl howto.image README \
	errata.mm errata.html textintro.mm mkctoc

# Use this kludge to stop RCS from entering ID strings
RCSIDSTRING   = Id

# Short ToCs
CTOCFILES	=	${CHAPTERS:%=ctoc/%} ${APPENDICES:%=ctoc/%} ctoc/preface.mm


# PostScript and PDF for the chapters
POSTSCRIPT = ${SOURCES:.mm=.ps}
PDF = ${SOURCES:.mm=.pdf}
TEXTDOCS = ${SIZE}/txt/toc.txt ${SOURCES:.mm=.txt}

# Base names without suffix
BASENAMES  = ${PREFACE:.mm=} ${SOURCES:.mm=}

ALLBASES = ${ALL-SOURCES:.mm=}

# Why doesn't this work?
# TOOLS=`cd ../tools; pwd`
TOOLS=/home/Book/tools

# The distribution files, for testing
dist = installing-FreeBSD

.SUFFIXES: .mm .ps .psl .PS

# These targets build the complete book

# Main entry.  Make the PDF version of the book.
all ${SIZE}/book.pdf:  ${DIRECTORIES} ${SIZE}/book.ps
	ps2pdf ${SIZE}/book.ps  ${SIZE}/book.pdf

# Build the book in PostScript form
# The dependencies here include the tools we use to build the book:
# some of them need rebuilding every time.
book.${SIZE}:	${DIRECTORIES} ctocfiles massage ${SIZE}/book.ps

# Build the book proper, including TOC and index.  The dependency texts makes the
# individual documents.
complete/book.ps: ${SIZE}/chapternumbers title.${SIZE} ${ALL-SOURCES} texts 
	@echo +++ $@
	cp ${SIZE}/ps/toc.ps ${SIZE}/toc.ps
	cat ${SIZE}/toc.ps ${SIZE}/chapters.ps ${SIZE}/appendix.ps > ${SIZE}/book.ps
	emacs -batch ${SIZE}/toc.ps -l $F/massageps.el -f massageps -f exit
	emacs -batch ${SIZE}/chapters.ps -l $F/massageps.el -f massageps -f exit
	emacs -batch ${SIZE}/appendix.ps -l $F/massageps.el -f massageps -f exit
# Touch the file so that it's newer than the log files
	emacs -batch ${SIZE}/book.ps -l $F/massageps.el -f massageps -f exit

# Build the book in ASCII from, including TOC and index.
${SIZE}/book.txt book.txt: title.${SIZE} ${ALL-SOURCES} texts.ascii
	@echo +++ $@
	cat ${SIZE}/txt/toc.txt ${SIZE}/chapters.txt \
	  ${SIZE}/appendix.txt | sed 's:.::g' >${SIZE}/book.txt
	rm -f ${SIZE}/*~
# Touch the file so that it's newer than the log files
	touch $@

# Make thumbnails
thumbnails: massageps.el chapters
	cat ${SIZE}/ps/toc.ps ${SIZE}/chapters.ps \
	  ${SIZE}/appendix.ps >${SIZE}/thumbnail.ps
	emacs -batch ${SIZE}/thumbnail.ps -l $F/massageps.el -f thumbnail -f exit
	rm -f ${SIZE}/thumbnail.ps~

texts:
	@echo +++ $@
	make ${PSDOCS} ${AUXFILES} SIZE=${SIZE}
	make ${PSDOCS} ${AUXFILES} SIZE=${SIZE}
	make ${PSDOCS} ${AUXFILES} SIZE=${SIZE}

texts.ascii:
	@echo +++ $@
	make ${TXTDOCS} ${AUXFILES} SIZE=${SIZE}
	make ${TXTDOCS} ${AUXFILES} SIZE=${SIZE}
	make ${TXTDOCS} ${AUXFILES} SIZE=${SIZE}

# Main bulk of the book: all the numbered chapters
${SIZE}/chapters.ps chapters: ${CHAPTERS} ${SIZE}/ids.mm ${SIZE}/Xref 
	@if [ -f $@ ]; then \
	  ls -tl ${CHAPTERS} ${SIZE}/ids.mm ${SIZE}/Xref $@; \
	else \
	  echo +++ $@ " doesn't exist, rebuilding"; \
	fi
	(cat ${SIZE}/ids.mm global.mm ${SIZE}/Xref ${SIZE}/chapternumbers; \
          for i in ${XCHAPTERS}; do ${SOELIM} $$i reset.mm; done; \
          ${SOELIM} ${LCHAPTER} ) | \
	  ${TOOLS}/stripcond ${TAGS.${SIZE}} | ${PIC} | ${TBL} | \
	  groff ${ROFFDEBUG} -U -rex=${EXAMPLE-SIZE} -r$$$$ -rL${HEIGHT.${SIZE}} -rW${WIDTH.${SIZE}} \
	    -rPS=${TEXTSIZE} \
	    -rLS=${LINESPACING} -rfirstpage=1 -mpic ${MACROFILE} - \
	  >${SIZE}/chapters.ps.tmp 2>${SIZE}/Toc/book
	mv ${SIZE}/chapters.ps.tmp ${SIZE}/chapters.ps
	-egrep -v ^'File|PSPIC|><' ${SIZE}/Toc/book
	grep %%Pages: ${SIZE}/chapters.ps | head -1 | awk '{print $$2}' > appendix-start
# Touch the file so that it's newer than the log files
	touch $@

# Main bulk of the book in ASCII: all the numbered chapters
${SIZE}/chapters.txt: ${CHAPTERS} ${SIZE}/ids.mm ${SIZE}/Xref 
	@if [ -f $@ ]; then \
	  ls -tl ${CHAPTERS} ${SIZE}/ids.mm ${SIZE}/Xref $@; \
	else \
	  echo +++ $@ " doesn't exist, rebuilding"; \
	fi
	(cat ${SIZE}/ids.mm global.mm ${SIZE}/Xref ${SIZE}/chapternumbers; \
          for i in ${CHAPTERS}; do ${SOELIM} $$i reset.mm; done) | \
	  ${TOOLS}/stripcond ${TAGS.${SIZE}} | ${PIC} | ${TBL} | \
	   nroff -rex=${EXAMPLE-SIZE} -r$$$$ -rL${HEIGHT.${SIZE}} -rW7.9i -rPS=${TEXTSIZE} \
	    -rPS=${TEXTSIZE} \
	    -rLS=${LINESPACING} -rfirstpage=1 -mpic ${MACROFILE} - \
	  >${SIZE}/chapters.txt.tmp 2>${SIZE}/Toc/book
	mv ${SIZE}/chapters.txt.tmp ${SIZE}/chapters.txt
	-egrep -v ^'File|PSPIC|><' ${SIZE}/Toc/book
# XXX	grep %%Pages: ${SIZE}/chapters.txt | head -1 | awk '{print $$2}' > appendix-start
	grep '^><Last Page' ${SIZE}/Toc/book | tail -1 | awk '{print $$3}' > appendix-start
# Touch the file so that it's newer than the log files
	touch $@

# Appendices, index and bibliography
${SIZE}/appendix.ps appendix: ${APPENDICES} ${AUXFILES} ${SIZE}/Xref appendix-start
	@@if [ -f $@@ ]; then \
	  ls -tl $@@ ${APPENDICES} ${AUXFILES} ${SIZE}/Xref; \
	else \
	  echo +++ $@@ " doesn't exist, rebuilding"; \
	fi
	read < appendix-start page; page=`expr $$page + 1`; \
	(echo .nr % $$page; cat global.mm ${SIZE}/Xref ${SIZE}/chapternumbers ${SIZE}/ids.mm; \
	 for i in ${APPENDICES} ${SIZE}/index.mm; do ${SOELIM} $$i reset.mm; done; ${SOELIM} grog.mm) | \
	  ${TOOLS}/stripcond ${TAGS.${SIZE}} | ${PIC} | ${TBL} | \
	  groff ${ROFFDEBUG} -U -rex=${EXAMPLE-SIZE} -r$$$$ -rL${HEIGHT.${SIZE}} -rW${WIDTH.${SIZE}} -rPS=${TEXTSIZE} \
	    -rLS=${LINESPACING} -rfirstpage=1 -mpic ${MACROFILE} - \
	  >${SIZE}/appendix.ps.tmp 2>${SIZE}/Toc/appendix
	mv ${SIZE}/appendix.ps.tmp ${SIZE}/appendix.ps
	-egrep -v ^'File|PSPIC|><' ${SIZE}/Toc/appendix
# Touch the file so that it's newer than the log files
	touch $@

# Title matter, table of contents and preface
${SIZE}/ps/toc.ps toc toc.ps: title.${SIZE} ${AUXFILES} ${SIZE}/Xref ctoc/preface.mm
	@@if [ -f $@@ ]; then \
	  ls -tl $@@ title.${SIZE} ${PREFACE} ${AUXFILES} ${SIZE}/Xref; \
	else \
	  echo +++ $@@ " doesn't exist, rebuilding"; \
	fi
	${SOELIM} ${SIZE}/Xref ${SIZE}/ids.mm global.mm title.${SIZE}  ${SIZE}/chapternumbers ${SIZE}/toc.mm \
	  reset.mm foreword.mm reset.mm 10-years.mm reset.mm preface.mm  | \
	  ${TOOLS}/stripcond ${TAGS.${SIZE}} | ${PIC} | ${TBL} | \
	  groff ${ROFFDEBUG} -U -rex=${EXAMPLE-SIZE} -r$$$$ -rL${HEIGHT.${SIZE}} -rW${WIDTH.${SIZE}} -rPS=${TEXTSIZE} \
	    -rLS=${LINESPACING} -mpic ${MACROFILE} - \
	  > ${SIZE}/ps/toc.ps.tmp 2>${SIZE}/Toc/toc
	mv ${SIZE}/ps/toc.ps.tmp ${SIZE}/ps/toc.ps
	-egrep -v ^'File|PSPIC|><' ${SIZE}/Toc/toc
	rm -f ${SIZE}/nps/00-toc.ps
	ln ${SIZE}/ps/toc.ps ${SIZE}/nps/00-toc.ps
# Touch the file so that it's newer than the log files
	touch $@

# Appendices, index and bibliography--text version
${SIZE}/appendix.txt: ${APPENDICES} ${AUXFILES} ${SIZE}/Xref appendix-start
	@if [ -f $@ ]; then \
	  ls -tl $@ ${APPENDICES} ${AUXFILES} ${SIZE}/Xref; \
	else \
	  echo +++ $@ " doesn't exist, rebuilding"; \
	fi
	read < appendix-start page; \
	(echo ".pn $$page"; cat reset.mm global.mm ${SIZE}/Xref ${SIZE}/chapternumbers; \
	 for i in ${APPENDICES} ${SIZE}/index.mm; do ${SOELIM} $$i reset.mm; done; ${SOELIM} grog.mm) | \
	  ${TOOLS}/stripcond ${TAGS.${SIZE}} | ${PIC} | ${TBL} | \
	   nroff -rex=${EXAMPLE-SIZE} -r$$$$ -rL${HEIGHT.${SIZE}} -rW7.9i -rPS=${TEXTSIZE} \
	    -rLS=${LINESPACING} -rfirstpage=1 -mpic ${MACROFILE} - \
	  >${SIZE}/appendix.txt 2>${SIZE}/Toc/appendix
	-egrep -v ^'File|PSPIC|><' ${SIZE}/Toc/appendix
# Touch the file so that it's newer than the log files
	touch $@

${SIZE}/chapternumbers: ${SOURCES} getchapternumbers .FORCE
	@echo +++ chapternumbers
#	-ls -rlt ${SIZE}/chapternumbers ${SOURCES} getchapternumbers
	./getchapternumbers ${SIZE} ${SOURCES} > $@.temp
	@-cmp $@ $@.temp 2>/dev/null; \
	if [ $$? -ne 0 ]; then \
	  echo Chapter numbers have changed; \
	  mv $@.temp $@; \
	else \
	  rm $@.temp; \
	  touch $@; \
	fi

# Create the auxiliary files.  We touch them in case they haven't
# changed; otherwise we'll repeat the target the next time, although
# we have already established that there's nothing to do.
${SIZE}/Xref:	${TOCFILES} getchapternumbers
	@echo +++ $@
	${TOOLS}/make-xref $@ ${TOCFILES} $$newer

${SIZE}/Xref.html: getchapternumbers .FORCE
	@echo +++ $@
	${TOOLS}/make-xref-html $@ ${SIZE}/Toc ${SOURCES:.mm=.html}

X:	${TOCFILES} getchapternumbers .FORCE
	@echo +++ $@
	@newer=`find ${SIZE}/Toc -type f -newer ${SIZE}/Toc/book`; \
	${TOOLS}/make-xref ${SIZE}/Xref ${TOCFILES} $$newer >/dev/null
	touch ${SIZE}/Xref

${SIZE}/index.mm:	${TOCFILES}
	@echo +++ $@
	${TOOLS}/make-index $@ ${TOCFILES}

${SIZE}/toc.mm:	${TOCFILES}
	@echo +++ $@
	${TOOLS}/make-toc $@ ${TOCFILES}

${SIZE}/tables.mm:	${TOCFILES}
	@echo +++ $@
	${TOOLS}/make-table $@ ${TOCFILES}

# Create dummy toc files.  If we try to really make them, we end up with
# circular dependencies.
${SIZE}/Toc/toc ${SIZE}/Toc/book ${SIZE}/Toc/1 ${SIZE}/Toc/1X ${SIZE}/Toc/2 ${SIZE}/Toc/4 \
		${SIZE}/Toc/8 ${SIZE}/Toc/appendix ${SIZE}/Toc/X:  ${SIZE}/Toc
	@echo +++ $@
	@echo Creating dummy $@
	touch $@

tools:
	@echo +++ $@
	(cd ${TOOLS}; make)

clean:
	@echo +++ $@
	rm -f *.toc *.ps *.ascii *~ core core.* *.even *.odd Contents Index make.log *.PS 
	rm -rf ${SIZES} Chapter *.tar.gz
	rm -f book.longmakefile paths.make filelist appendix-start
	rm -f ${AUXFILES} ${RCS-FILES} totidy massage*.el
	rm -f index.mm STATUS* packages_body_sorted.mm
	rm -f appendix-start ktrace.out *.pp Make.log

realclean: clean
	@echo +++ $@
	rm -f ${RCS-FILES} makebookmakefile totidy

# Make individual chapters without checking dependencies on earlier chapters
all-chapters: ${POSTSCRIPT} foreword.ps 10-years.ps preface.ps

packages.mm::	packages_body.mm packages_body_sorted.mm

packages_body.mm:  mkpackages INDEX
	mkpackages

packages_body_sorted.mm:  mkpackages-sorted
	-ls -tl packages_body_sorted.mm mkpackages-sorted
	mkpackages-sorted

review:	tools STATUS ${CHAPTERS:.mm=.rv}

orphan XXX:
	rm -f ${SIZE}/[0-9].ps ${SIZE}/[A-Z].ps ${SIZE}/*.txt
	@echo +++++ Making individual chapters in PostScript
	make all-chapters toc.ps 
	@echo +++++ Making individual chapters in ASCII
	@echo make  ${CHAPTERS:.mm=.txt} ${APPENDICES:.mm=.txt} toc.ps toc.txt
	make  ${CHAPTERS:.mm=.txt} ${APPENDICES:.mm=.txt} toc.ps toc.txt
	@echo packing files
	make ftpreview 

release: mkthumbnail
	gzip < ${SIZE}/book.ps >/var/spool/ftp/pub/.FINAL/book.ps.gz
	mkthumbnail < ${SIZE}/book.ps 1 2000 | gzip  >/var/spool/ftp/pub/.FINAL/thumbnail.ps.gz

# Put the review chapters on the ftp site.  Currently unused and deprecated.
ftpreview: dear-reviewer STATUS ${FTP}/ps ${FTP}/txt
	gzip < ${SIZE}/book.ps > ${FTP}/book.ps.gz
	rm -f ${FTP}/ps/*  ${FTP}/txt/* STATUS*
	cd ${SIZE}/txt; \
	sed 's:.::g' < toc.txt > 00-toc.txt; \
	cp -p [0-9A-G]* ${FTP}/txt; \
	tar czvf ${FTP}/txt.tar.gz [0-9]*.txt [A-Z]*.txt
	-cd ${SIZE}/nps; \
	for i in [0-9]*.ps [A-Z]*.ps; do \
	  echo $$i; \
	  gzip -c $$i > ${FTP}/ps/$$i.gz; \
        done
	(cd ${FTP}/txt; gzip *)
	chmod 644 ${FTP}/txt/* ${FTP}/ps/* ${FTP}/*
	chmod 755 ${FTP}/txt ${FTP}/ps

ps2pdf:
	mkdir -p ${SIZE}/pdf
	cd ${SIZE}/ps; \
	for i in [a-z]*; do \
	  echo $$i; \
	  $(GS) -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=a4 \
		-sOutputFile=../pdf/`basename $$i .ps`.pdf -c save pop -f \
		$$i; \
	done

# Make all review texts
fullreview: ${POSTSCRIPT}  ${TEXTDOCS} dear-reviewer STATUS ${WWW} ps2pdf

# Make all review texts and install them on the web site area
installreview: fullreview wwwreview

newdir:
	rm -rf ${SIZE}

wwwreview:
	rm -f ${WWW}/*
	cp dear-reviewer.html ${WWW}/cfbsd.html
	cd ${SIZE}/txt; \
	for i in [a-z]*; do cp -p $$i ${WWW}/$$i; done; \
	sed 's:.::g' < toc.txt > ${WWW}/toc.txt
	cp -p ${SIZE}/pdf/* ${SIZE}/STATUS.ps ${SIZE}/STATUS.txt ${WWW}
	chmod 644 ${WWW}/*

dear-reviewer:  dear-reviewer-1 dear-reviewer-1a dear-reviewer-2 URLS
	cat dear-reviewer-1 > $@.html
	TZ=Etc/UTC date >> $@.html
	cat dear-reviewer-1a URLS dear-reviewer-2  >> $@.html

STATUS:	${PREFACE} ${CHAPTERS} ${APPENDICES}
	rm -f $@
	echo ".ll 6.5i" >> $@
	echo ".ce" >> $@
	echo "Chapter status at `TZ=Etc/UTC date`" >> $@
	echo ".sp 1.5v" >> $@
	echo ".TB \"Chapter status `TZ=Etc/UTC date`\"" >> $@
	echo ".TS" >> $@
	echo "tab(#) ;" >> $@
	echo "| r | l | l | l | r | r | l | ." >> $@
	echo "_" >> $@
	echo "Chap-#Source file#Revision#Modified (UTC)#First#Pages#Status" >> $@
	echo "ter####Page" >> $@
	echo "=" >> $@
	-ch=0; \
	for i in preface.mm ${CHAPTERS}; do \
	  psfile=${SIZE}/ps/`echo $$i | sed 's:.mm$$:.ps:'`; \
	  if [ -f $$psfile ]; then \
	    pages=`grep "%%Pages" $$psfile | head -n 1 | awk '{print $$2}'`; \
	  else \
	    pages="?"; \
	  fi; \
	  firstpage=`egrep "^Chapter.*\\<$$i" ${SIZE}/Toc/book | sed 's:,::g' | awk '{print $$4}'`; \
	  status=`grep '\\" STATUS' $$i | sed 's/:.*://; s/^.*STATUS://'`; \
	  RCSID="$$ch#`grep \\$$${RCSIDSTRING}: $$i | awk '{print  $$3 "#" $$4 }'`"; \
	  rcsdiff $$i >/dev/null 2>&1; \
	  if [ $$? != 0 ]; then \
	    RCSID="$$RCSID*#`TZ=Etc/UTC ls -l $$i | awk '{print $$7 \" \" $$6 \", \" $$8}'`"; \
	  else \
	    RCSID="$$RCSID#`grep \\$$${RCSIDSTRING}: $$i | awk '{print  $$5 " " $$6 }'`"; \
	  fi; \
	  echo $$RCSID#$$firstpage#$$pages#$$status >> $@; \
	  ch=`expr $$ch \\+ 1`; \
	done
	echo "_" >> $@
	echo "App-#Source file#Revision#Modified (UTC)#First#Pages#Status" >> $@
	echo "endix####Page" >> $@
	echo "_" >> $@
	-ch=65; \
	for i in ${APPENDICES}; do \
	  psfile=${SIZE}/ps/`echo $$i | sed 's:.mm$$:.ps:'`; \
	  if [ -f $$psfile ]; then \
	    pages=`grep "%%Pages" $$psfile | head -n 1 | awk '{print $$2}'`; \
	  else \
	    pages="?"; \
	  fi; \
	  firstpage=`egrep "^Appendix.*\\<$$i" ${SIZE}/Toc/book | sed 's:,::g' | awk '{print $$4}'`; \
	  status=`grep '\\" STATUS' $$i | sed 's/:.*://; s/^.*STATUS://'`; \
	  RCSID="$$ch#`grep \\$$${RCSIDSTRING}: $$i | awk '{print  $$3 "#" $$4 }'`"; \
	  rcsdiff $$i >/dev/null 2>&1; \
	  if [ $$? != 0 ]; then \
	    RCSID="$$RCSID*#`TZ=Etc/UTC ls -l $$i | awk '{print $$7 \" \" $$6 \", \" $$8}'`"; \
	  else \
	    RCSID="$$RCSID#`grep \\$$${RCSIDSTRING}: $$i | awk '{print  $$5 " " $$6 }'`"; \
	  fi; \
	  echo $$RCSID#$$firstpage#$$pages#$$status >> $@; \
	  ch=`expr $$ch \\+ 1`; \
	done
	echo "_" >> $@
	echo ".TE" >> $@
	echo ".sp" >> $@
	echo "Note: a \\f(CW*\\fP symbol after the version number indicates that the source file has been modified since the last checkin.  In this case, the column \`\`Modified'' shows the date of the last modification.  The version information of any such file may be inaccurate." >> $@
	${TBL} STATUS | groff ${ROFFDEBUG} -U >${SIZE}/STATUS.ps -rW${WIDTH.${SIZE}}
	${TBL} STATUS | nroff > ${SIZE}/STATUS.txt

URLS:	preface.mm ${CHAPTERS} ${APPENDICES}
	rm -f $@
	echo '    <a href="toc.pdf">Preface and table of contents in PDF format</a>' >> $@
	echo '    <br />' >> $@
	echo '     <a href="toc.txt">Preface and table of contents in ASCII</a>' >> $@
	ch=1; \
	for j in ${CHAPTERS}; do \
	  i=`basename $$j .mm`; \
	  echo "    <p>" >> $@; \
	  echo "    <a href=\"$$i.pdf\">Chapter $$ch ($$i) in PDF format</a>" >> $@; \
	  echo "    <br />" >> $@; \
	  echo "     <a href=\"$$i.txt\">Chapter $$ch ($$i) in ASCII</a></p>" >> $@; \
	  echo >> $@; \
	  ch=`expr $$ch + 1`; \
	done

# Remake individual chapters
foreword.ps 10-years.ps preface.ps ${POSTSCRIPT}:  ${SIZE}/Toc/book ${SIZE}/ps \
	   ${SIZE}/nps ${SIZE}/chapternumbers ${SIZE}/ids.mm 
	make ctoc/`basename ${@:.ps=.mm}`
	-@base=${@:.ps=}; \
	page=`egrep "^Chapter.*\\<$$base.mm" ${SIZE}/Toc/book | sed 's:,::g' | awk '{print $$4}'`; \
	RCSID=`grep \\$$${RCSIDSTRING}: $$base.mm | awk '{print  $$3 " v" $$4 " (" $$5 " " $$6 ") " }'`; \
	timestamp=`echo $$RCSID | sed 's:.*(::; s/:[0-9][0-9]).*$$//; s#[:/ ]##g'`; \
	rcsdiff $$base.mm >/dev/null; \
	if [ $$? != 0 ]; then \
	  RCSID="$$RCSID (modified `TZ=Etc/UTC ls -lT $$base.mm | awk '{print $$7 \" \" $$6 \" \" $$9 \", \" $$8}'` UTC)"; \
	fi; \
	echo "^Chapter.*\\<$$base.mm"; \
	chapter=`egrep "^Chapter.*\\<$$base.mm" ${SIZE}/Toc/book | sed 's:,::g' | awk '{print $$2}'`; \
	if [ "$$chapter" = "" ]; then \
	  setit=`egrep "^Appendix.*\\<$$base.mm" ${SIZE}/Toc/appendix`; \
	  if [ "$$setit" != "" ]; then \
	    set $$setit; \
	    prefix=`${XCHAR} $$2`; \
	    page=`echo $$4 | sed 's:,::'`; chapter=$$6; \
	  else \
	    page=1; \
	    prefix=00; \
	  fi; \
	fi; \
	echo base $$base; \
	echo -n "+++ Chapter $$chapter ($@) starts on page $$page "; \
	(echo ".pn $$page";  echo ".ds RCS-ID $$RCSID"; \
	   (${SOELIM} ${SIZE}/ids.mm ${MACROFILE} ; echo ".ds RCS-ID $$RCSID"; \
	   ${SOELIM} ${SIZE}/Xref ${SIZE}/chapternumbers global.mm $$base.mm | \
	${TOOLS}/stripcond ${TAGS.${SIZE}} | \
	 ${PIC} 2>/dev/tty | ${TBL})) | \
	 groff ${ROFFDEBUG} -U -rex=${EXAMPLE-SIZE} -rfirstpage=1 -r$$$$ -rL${XHEIGHT.${SIZE}} -rW${WIDTH.${SIZE}} \
	    -rPS=${TEXTSIZE} -rLS=${LINESPACING} - \
	 >${SIZE}/ps/$@.tmp 2>${SIZE}/Toc/$$base; \
	exitcode=$$?; \
	mv ${SIZE}/ps/$@.tmp ${SIZE}/ps/$@; \
	startpage=$$page; \
	page=`awk < ${SIZE}/ps/$@ '/%%Page:/ {page=$$2 }; END {print page}'`; \
	if [ "$$prefix" != "" ]; then \
	  filename=${SIZE}/nps/$$prefix-$@; \
	elif [ $$chapter -lt 10 ]; then \
	  filename=${SIZE}/nps/0$$chapter-$@; \
	else \
	  filename=${SIZE}/nps/$$chapter-$@; \
	fi; \
	echo -n "and ends on page $$page "; \
	page=`expr $$page + 1`; \
	echo \(`expr $$page - $$startpage` pages\): $$filename; \
	if [ `expr $$page % 2 ` -eq 0 ]; then \
	  echo " (Last page is blank)"; \
	  page=`expr $$page + 1`; \
	fi; \
	egrep -v ^'Chapter|File|PSPIC|><' ${SIZE}/Toc/$$base; \
	rm -f $$filename; \
	ln ${SIZE}/ps/$@ $$filename; \
	if [ $$exitcode -ne 0 ]; then \
	  echo "*** Exit code $$exitcode - format aborted"; \
	fi;
# Set the destination timestamp to the RCS date.
# Why would I want to do that?
#	TZ=Etc/GMT touch -t $$timestamp ${SIZE}/ps/`basename $@`;
	
# All chapters in PDF
pdf:	${PDF}

# Remake individual chapters in PDF.  This is probably obsolete.
preface.pdf ${CHAPTERS:.mm=.pdf} ${APPENDICES:.mm=.pdf}: ${@:.pdf=.ps} ${SIZE}/pdf
	make ${@:.pdf=.txt} 
	$(GS) -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=a4 \
		-sOutputFile=${SIZE}/pdf/$@ -c save pop -f \
		${SIZE}/ps/${@:.pdf=.ps}

# Review stuff, yet again
preface.rv ${CHAPTERS:.mm=.rv} ${APPENDICES:.mm=.rv}: ${@:.rv=.pdf} ${@:.rv=.txt}
	@-base=${@:.rv=}; \
	echo "^Chapter.*\\<$$base.mm"; \
	chapter=`egrep "^Chapter.*\\<$$base.mm" ${SIZE}/Toc/book | sed 's:,::g' | awk '{print $$2}'`; \
	if [ "$$chapter" = "" ]; then \
	  setit=`egrep "^Appendix.*\\<$$base.mm" ${SIZE}/Toc/appendix`; \
	  if [ "$$setit" != "" ]; then \
	    set $$setit; \
	    prefix=`${XCHAR} $$2`; \
	    page=`echo $$4 | sed 's:,::'`; chapter=$$6; \
	  else \
	    page=1; \
	    prefix=00; \
	  fi; \
	fi; \
	echo base $$base; \
	if [ "$$prefix" != "" ]; then \
	  filename=$$prefix-$$base; \
	elif [ $$chapter -lt 10 ]; then \
	  filename=0$$chapter-$$base; \
	else \
	  filename=$$chapter-$$base; \
	fi; \
	echo chapter $$chapter, prefix $$prefix, filename $$filenam

txt:	${TEXTDOCS}

${TEXTDOCS}: ${SIZE}/Toc/book ${SIZE}/txt ${SIZE}/chapternumbers  ${SIZE}/Xref ${SIZE}/ids.mm
	@echo +++ $@
	-@base=${@:.txt=}; \
	page=`egrep "^Chapter.*\\<$$base.mm" ${SIZE}/Toc/book | sed 's:,::g' | awk '{print $$4}'`; \
	RCSID=`grep \\$$${RCSIDSTRING}: $$base.mm | awk '{print  $$3 " v" $$4 " (" $$5 " " $$6 ")" }'`; \
	mod=`grep \\$$${RCSIDSTRING}: $$base.mm | awk '{print  $$9}'`; \
	if [ X$$mod = Xgrog ]; then \
	  RCSID="$$RCSID (modified `TZ=Etc/UTC ls -lT $$base.mm | awk '{print $$7 \" \" $$6 \" \" $$9 \", \" $$8}'` UTC)"; \
	fi; \
	chapter=`egrep "^Chapter.*\\<$$base.mm" ${SIZE}/Toc/book | sed 's:,::g' | awk '{print $$2}'`; \
	if [ "$$chapter" = "" ]; then \
	  setit=`egrep "^Appendix.*\\<$$base.mm" ${SIZE}/Toc/appendix`; \
	  if [ "$$setit" != "" ]; then \
	    set $$setit; \
	    prefix=`${XCHAR} $$2`; \
	    page=`echo $$4 | sed 's:,::'`; chapter=$$6; \
	  else \
	    page=1; \
	    prefix=00; \
	  fi; \
	fi; \
	(echo ".pn $$page";  echo ".ds RCS-ID $$RCSID"; \
	   ${SOELIM} global.mm ${SIZE}/ids.mm; echo ".ds RCS-ID $$RCSID"; \
	   ${SOELIM} ${SIZE}/Xref ${SIZE}/chapternumbers $$base.mm) | \
	${TOOLS}/stripcond ${TAGS.${SIZE}} | \
	 ${PIC} | ${TBL} | \
	 nroff -rex=${EXAMPLE-SIZE} -rfirstpage=1 -r$$$$ -rL${HEIGHT.${SIZE}} -rW7.9i \
	    -rPS=${TEXTSIZE} -rLS=${LINESPACING} -mpic ${MACROFILE} - 2>${SIZE}/Toc/$$base \
	    | sed 's:.::g' >${SIZE}/txt/$@.tmp; \
	exitcode=$$?; \
	mv ${SIZE}/txt/$@.tmp ${SIZE}/txt/$@; \
	if [ "$$prefix" != "" ]; then \
	  filename=${SIZE}/txt/$$prefix-$@; \
	elif [ $$chapter -lt 10 ]; then \
	  filename=${SIZE}/txt/0$$chapter-$@; \
	else \
	  filename=${SIZE}/txt/$$chapter-$@; \
	fi; \
	rm -f $$filename; \
	ln ${SIZE}/txt/$@ $$filename; \
	if [ $$exitcode -ne 0 ]; then \
	  echo "*** Exit code $$exitcode - format aborted"; \
	fi; \
	touch ${SIZE}/txt/`basename $@`; \

# Make individual chapters in ASCII for CD-ROM
preface.roff ${CHAPTERS:.mm=.roff} ${APPENDICES:.mm=.roff}:	${SIZE}/Toc/book ${SIZE}/roff \
			${SIZE}/chapternumbers  ${SIZE}/Xref ${SIZE}/ids.mm
	@echo +++ $@
	@base=${@:.roff=}; \
	page=`egrep "^Chapter.*\\<$$base.mm" ${SIZE}/Toc/book | sed 's:,::g' | awk '{print $$4}'`; \
	RCSID=`grep \\$$${RCSIDSTRING}: $$base.mm | awk '{print  $$3 " v" $$4 " (" $$5 " " $$6 ")" }'`; \
	mod=`grep \\$$${RCSIDSTRING}: $$base.mm | awk '{print  $$9}'`; \
	if [ X$$mod = Xgrog ]; then \
	  RCSID="$$RCSID (modified `TZ=Etc/UTC ls -lT $$base.mm | awk '{print $$7 \" \" $$6 \" \" $$9 \", \" $$8}'` UTC)"; \
	fi; \
	chapter=`egrep "^Chapter.*\\<$$base.mm" ${SIZE}/Toc/book | sed 's:,::g' | awk '{print $$2}'`; \
	if [ "$$chapter" = "" ]; then \
	  setit=`egrep "^Appendix.*\\<$$base.mm" ${SIZE}/Toc/appendix`; \
	  if [ "$$setit" != "" ]; then \
	    set $$setit; \
	    prefix=`${XCHAR} $$2`; \
	    page=`echo $$4 | sed 's:,::'`; chapter=$$6; \
	  else \
	    page=1; \
	    prefix=00; \
	  fi; \
	fi; \
	(echo ".pn $$page";  echo ".ds RCS-ID $$RCSID"; \
	   ${SOELIM} global.mm ${SIZE}/ids.mm; echo ".ds RCS-ID $$RCSID"; \
	   ${SOELIM} ${SIZE}/Xref ${SIZE}/chapternumbers $$base.mm) | \
	${TOOLS}/stripcond ${TAGS.${SIZE}} | \
	 ${PIC} | ${TBL} | \
	 nroff -rex=${EXAMPLE-SIZE} -rfirstpage=1 -r$$$$ -rL${HEIGHT.${SIZE}} -rW7.9i \
	    -rPS=${TEXTSIZE} -rLS=${LINESPACING} -mpic ${MACROFILE} - 2>${SIZE}/Toc/$$base \
	     >${SIZE}/roff/$@.tmp; \
	exitcode=$$?; \
	mv ${SIZE}/roff/$@.tmp ${SIZE}/roff/$@; \
	if [ "$$prefix" != "" ]; then \
	  filename=${SIZE}/roff/$$prefix-$@; \
	elif [ $$chapter -lt 10 ]; then \
	  filename=${SIZE}/roff/0$$chapter-$@; \
	else \
	  filename=${SIZE}/roff/$$chapter-$@; \
	fi; \
	rm -f $$filename; \
	ln ${SIZE}/roff/$@ $$filename; \
	if [ $$exitcode -ne 0 ]; then \
	  echo "*** Exit code $$exitcode - format aborted"; \
	fi
# Touch the file so that it's newer than the log files
	@touch ${SIZE}/roff/`basename $@`

# ${CHAPTERS:.mm=.pp} ${APPENDICES:.mm=.pp}: ${AUXFILES}
# 	base=${@:.pp=}; \
# 	page=`egrep "^*\<$$base\>" ${SIZE}/Toc/book | sed 's:,::g' | awk '{print $$4}'`; \
# 	(echo ".pn $$page"; ${SOELIM} ${MACROFILE} global.mm ${SIZE}/Xref ${SIZE}/chapternumbers $$base.mm) | \
# 	${TOOLS}/stripcond ${TAGS.${SIZE}} | ${PIC} | ${TBL} >$@
# Touch the file so that it's newer than the log files
#	touch $@

# Make preprocessor output for individual chapters, for debugging.
# This should go into groff and produce the same output as the .ps target.
${CHAPTERS:.mm=.pp} ${APPENDICES:.mm=.pp}: ${AUXFILES}
	base=${@:.pp=}; \
	page=`egrep "^Chapter.*\\<$$base.mm" ${SIZE}/Toc/book | sed 's:,::g' | awk '{print $$4}'`; \
	RCSID=`grep \\$$${RCSIDSTRING}: $$base.mm | awk '{print  $$3 " v" $$4 " (" $$5 " " $$6 ") " }'`; \
	timestamp=`echo $$RCSID | sed 's:.*(::; s/:[0-9][0-9]).*$$//; s#[:/ ]##g'`; \
	mod=`grep \\$$${RCSIDSTRING}: $$base.mm | awk '{print  $$9}'`; \
	if [ X$$mod = Xgrog ]; then \
	  RCSID="$$RCSID (modified `TZ=Etc/UTC ls -lT $$base.mm | awk '{print $$7 \" \" $$6 \" \" $$9 \", \" $$8}'` UTC)"; \
	fi; \
	echo "    $$base.mm, RCS ID: $$RCSID"; \
	echo "^Chapter.*\\<$$base.mm"; \
	chapter=`egrep "^Chapter.*\\<$$base.mm" ${SIZE}/Toc/book | sed 's:,::g' | awk '{print $$2}'`; \
	if [ "$$chapter" = "" ]; then \
	  setit=`egrep "^Appendix.*\\<$$base.mm" ${SIZE}/Toc/appendix`; \
	  if [ "$$setit" != "" ]; then \
	    set $$setit; \
	    prefix=`${XCHAR} $$2`; \
	    page=`echo $$4 | sed 's:,::'`; chapter=$$6; \
	  else \
	    page=1; \
	    prefix=00; \
	  fi; \
	fi; \
	echo base $$base; \
	echo -n "+++ Chapter $$chapter ($@) starts on page $$page "; \
	(echo ".pn $$page";  echo ".ds RCS-ID $$RCSID"; \
	   (${SOELIM} global.mm ${SIZE}/ids.mm; echo ".ds RCS-ID $$RCSID"; \
	   ${SOELIM} ${SIZE}/Xref ${SIZE}/chapternumbers $$base.mm | \
	${TOOLS}/stripcond ${TAGS.${SIZE}} | \
        ${PIC} | ${TBL})) > $@
# Touch the file so that it's newer than the log files
	touch $@

pageframe: massage
	if [ ! -d ${SIZE}/prod ]; then \
	  mkdir -p ${SIZE}/prod; \
	fi
	cp -p ${SIZE}/book.ps ${SIZE}/prod
	emacs -batch ${SIZE}/book.ps -l $F/massageps.el -f makeframe -f exit
	rm -f ${SIZE}/book.ps~

massagetex.el massageps.el: massage

# Copy the tools massage macros, substituting our page dimensions in points
massage:	.FORCE
	@H=`echo ${HEIGHT.${SIZE}} | awk '{print $$1 * 72}'`; \
	W=`echo ${WIDTH.${SIZE}} ${MARGINS} | awk '{print ($$1 + $$2) * 72}'`; \
	echo Crop marks: Height $$H points, Width $$W points; \
	sed < ${TOOLS}/massageps.el > massageps.el "s:@YORIG@:${Y_ORIGIN}:; s:@XORIG@:${X_ORIGIN}:; s:@HEIGHT@:$$H:g; s:@WIDTH@:$$W:"; \
#	sed < ${TOOLS}/massagetex.el > massagetex.el "s:@YORIG@:${Y_ORIGIN}:; s:@XORIG@:${X_ORIGIN}:; s:@HEIGHT@:$$H:g; s:@WIDTH@:$$W:"

# Check in with current date.
ci:
	-ci -d  ${RCS-FILES} totidy
	co -M Makefile paths.make

# Yes, make checks out automatically, but it doesn't use the -M option, so 
# it will remake even if it's not needed.
co:
	-co -l -M -q ${RCS-FILES} totidy

save:	.FORCE
	rm -f save/*
	cp -p * save

errata:  ${SOURCES}
	rcsdiff -wu -r${EDITION} ${SOURCES} >$@

${SIZE}/errata.ps:  errata.ps
${SIZE}/errata.txt:  errata.txt

${SIZE}/errata.ascii:  ${SIZE}/errata.txt
	sed < ${SIZE}/errata.txt 's:.::g' >${SIZE}/errata.ascii

install-errata: ${SIZE}/errata.ps  ${SIZE}/errata.txt ${SIZE}/errata.ascii
	-cp -p ${SIZE}/errata.ps /var/spool/ftp/pub/cfbsd/errata-3.ps
	-cp -p ${SIZE}/errata.txt /var/spool/ftp/pub/cfbsd/errata-3.txt
	-sed < ${SIZE}/errata.txt 's:.::g' > /var/spool/ftp/pub/cfbsd/errata-3.ascii
	rm -f /var/spool/ftp/pub/cfbsd/errata-3
	-ln /var/spool/ftp/pub/cfbsd/errata-3.ascii /var/spool/ftp/pub/cfbsd/errata-3
	-cp -p errata.html /usr/local/www/data/errata-3.html

# Make everything that goes on the CD-ROM
install-cd:   ${SIZE} book.txt package-doc merge makesect
	mv ${SIZE}/book.txt book
	find book -name *~ | xargs rm -f
	tar czf cdbook.tar.gz book
	scp cdbook.tar.gz freefall.freebsd.org:

archive:
	tar cf - ${RCS-FILES} ${SIZE}/book.ps ${SIZE}/ps/toc.ps | gzip >book.tar.gz

ps.tar.gz ps:
	tar cf - ${SIZE}/book.ps ${SIZE}/ps/toc.ps | gzip >ps.tar.gz

srcarchive book.${SIZE}src.tar.gz:
	(cd ${dist}; make clean)
	tar cfh - ${dist} | gzip >book.${SIZE}.tar.gz

book.${SIZE}.tar.gz: book.${SIZE}
	tar cvzf $@ ${REVIEW-SOURCES}	

incdiffs diffs:	${SOURCES} .FORCE
	rm -rf $@
	mkdir $@
	for i in ${BASENAMES}; do \
	  if [ "$@" = "diffs" ]; then \
	    rcsdiff -r4.1 -uw $$i.mm  >$@/$$i || true; \
	  else \
	    rcsdiff -uw $$i.mm >$@/$$i; \
	  fi; \
	  if [ ! -s $@/$$i ]; then \
	    rm -f $@/$$i; \
	  fi \
	done

# ${RCS-FILES}:
#	-co -q -M $@

${SOURCES}:	global.mm # ${SIZE}/chapternumbers

# paths.make:
# 	@echo +++ $@
# 	@if [ -f paths.make.orig ]; then \
# 	  echo ; \
# 	  @echo ++++++ This source tree doesn\'t contain a paths.make.; \
# 	  @echo ++++++ This probably means that you have forgotten to; \
# 	  @echo ++++++ configure it.  I am creating one from the file; \
# 	  @echo ++++++ paths.make.orig, but you will probably not be; \
# 	  @echo ++++++ happy unless you edit it.  See the file INSTALL; \
# 	  @echo ++++++ for further confusing information.; \
# 	  echo ; \
# 	  cp paths.make.orig paths.make; \
# 	else \
# 	  @echo +++ Checking out paths.make; \
# 	  co -M -q paths.make; \
# 	fi

${DIRECTORIES} fmt/exlist fmt/figlist fmt/index fmt/tablist fmt/toc ${SIZE}/pdf ctoc:
	@echo +++ Creating $@
	mkdir -p $@

# This target does *not* apply to the chapters.  See the target starting
# with 'preface.ps' for that.
.mm.ps:
	@echo +++ $@
	${SOELIM} $< | ${TOOLS}/stripcond ${TAGS.${SIZE}} | \
	   ${PIC} | ${TBL} | \
	   groff ${ROFFDEBUG} -U -rex=${EXAMPLE-SIZE} -r$$$$ -rL${HEIGHT.${SIZE}} -rW${WIDTH.${SIZE}} -rPS=${TEXTSIZE} \
	    -rLS=${LINESPACING} -mpic ${MACROFILE} - \
	   >${SIZE}/$@ 2>${SIZE}/Toc/$@;
	-egrep -v ^'File|PSPIC|><' ${SIZE}/Toc/$@

.mm.txt:
	@echo +++ $@
	${SOELIM} $< | ${TOOLS}/stripcond ${TAGS.${SIZE}} | \
	   ${PIC} | ${TBL} | \
	   nroff -rex=${EXAMPLE-SIZE} -r$$$$ -rL${HEIGHT.${SIZE}} -rW7.9i -rPS=${TEXTSIZE} \
	    -rLS=${LINESPACING} -mpic ${MACROFILE} - 2>${SIZE}/Toc/$@ \
	   >${SIZE}/$@;
	-egrep -v ^'File|PSPIC|><' ${SIZE}/Toc/$@

.mm.html: tools
	@echo +++ $@
	(echo .nr html 1; echo .nh; echo .nf; echo .na; cat ${SIZE}/Xref.html; \
	   (${SOELIM} $< | ${TOOLS}/setfont)) | ${TOOLS}/stripcond ${TAGS.${SIZE}} | ${PIC} | ${TBL} | \
	   nroff -rex=${EXAMPLE-SIZE} -r$$$$ -rL50000i -rW7.9i -rPS=${TEXTSIZE} \
	    -rLS=${LINESPACING} -mpic ${HTMACROFILE} - 2>${SIZE}/Toc/$@ \
	   >${SIZE}/$@;
	# Now make the tables and pictures.
	(${SOELIM} $< | ${TOOLS}/setfont -t) | ${TOOLS}/stripcond ${TAGS.${SIZE}} | ${PIC} | ${TBL} | \
	   groff ${ROFFDEBUG} -U -rex=${EXAMPLE-SIZE} -r$$$$ -rL${HEIGHT.${SIZE}} \
	      -rW${WIDTH.${SIZE}} -rPS=${TEXTSIZE} \
	    -rLS=${LINESPACING} -mpic ${MACROFILE} - \
	   >${SIZE}/$@.tables 2>${SIZE}/Toc/$@;
	-egrep -v ^'File|PSPIC|><' ${SIZE}/Toc/$@

html:	
	-make -k ${SOURCES:.mm=.html} Xref.html

# Extract the RCS IDs from the source files
IDSRC	=	title.${SIZE} preface.mm ${SOURCES} ${MACROFILE}
${SIZE}/ids.mm:	${IDSRC}  mkids
	./mkids ${IDSRC}
# XXX FIXME
	mv ids.mm ${SIZE}

info:
	@echo This cave under construction 
	@echo ALL-SOURCES: ${ALL-SOURCES}
	@echo ALLBASES: ${ALLBASES}
	@echo APPENDICES: ${APPENDICES}
	@echo AUXFILES: ${AUXFILES}
	@echo BASENAMES : ${BASENAMES }
	@echo CHAPTERS.SIZE: ${CHAPTERS.${SIZE}}
	@echo CHAPTERS.complete: ${CHAPTERS.complete}
	@echo CHAPTERS: ${CHAPTERS}
	@echo DIRECTORIES: ${DIRECTORIES}
	@echo IDSRC: ${IDSRC}
	@echo POSTSCRIPT: ${POSTSCRIPT}
	@echo PDF: ${PDF}
	@echo PSDOCS: ${PSDOCS}
	@echo RCS-FILES: ${RCS-FILES}
	@echo REVIEW-SOURCES: ${REVIEW-SOURCES}
	@echo SIZE: ${SIZE}
	@echo SOURCES: ${SOURCES}
	@echo TOCFILES: ${TOCFILES}
	@echo TOCFILESH: ${TOCFILESH}
	@echo TOOLS: ${TOOLS}
	@echo CTOCFILES: ${CTOCFILES}
	@echo WIDTH: ${WIDTH.${SIZE}}
	@echo HEIGHT: ${HEIGHT.${SIZE}}

# XXX 
# Get rid of this stuff, which is just there to be able to build an index
# without appendices
# 
index.ps: ${SIZE}/index.mm
	page=${START_PAGE}; \
	(echo ".pn $$page"; cat reset.mm global.mm; ${SOELIM} ${SIZE}/index.mm) | \
	  ${TOOLS}/stripcond ${TAGS.${SIZE}} | ${PIC} | ${TBL} | \
	  groff ${ROFFDEBUG} -U -rex=${EXAMPLE-SIZE} -r$$$$ -rL${HEIGHT.${SIZE}} -rW${WIDTH.${SIZE}} -rPS=${TEXTSIZE} \
	    -rLS=${LINESPACING} -rfirstpage=1 -mpic ${MACROFILE} - \
	  >${SIZE}/appendix.ps.tmp 2>${SIZE}/Toc/appendix
	mv ${SIZE}/appendix.ps.tmp ${SIZE}/appendix.ps
	-egrep -v ^'File|PSPIC|><' ${SIZE}/Toc/appendix
# Touch the file so that it's newer than the log files
	touch $@

package-doc: packages_body.mm packages_body_sorted.mm packages.ps packages.txt
	mv ${SIZE}/packages.ps  ${SIZE}/packages.txt book/docs
	make TAGS="${TAGS.${SIZE}} sorted" packages.ps packages.txt
	mv ${SIZE}/packages.ps book/docs/packages-by-category.ps
	mv ${SIZE}/packages.txt book/docs/packages-by-category.txt

grep:
	grep ${GR} ${SOURCES}

bigtext: ${CHAPTERS} ${APPENDICES}
	cat ${CHAPTERS} ${APPENDICES} >bigtext

.txt.ascii:
	sed < $< > $@ 's:.::g'

# Stop a race condition when building certain parts of the book.
# The 9999 is a fictive page number.
appendix-start:
	echo 9999 > appendix-start

names:
	@echo ${CHAPTERS} ${APPENDICES}

ymake:
	while :; do  \
	  for i in ${SOURCES}; do \
	    dest=`basename $$i .mm`.ps; \
	    ddest=complete/ps/$$dest; \
	    if [ $$i -nt $$ddest ]; then \
	       ls -lt $$i $$ddest; \
	       echo make $$dest; \
	       make $$dest; \
	       touch $$ddest; \
	    fi; \
	  done; \
	  if [ title.${SIZE} -nt ${SIZE}/ps/toc.ps -o \
               preface.mm -nt ${SIZE}/ps/toc.ps -o \
               toc.mm -nt ${SIZE}/ps/toc.ps ]; then \
	    make toc.ps; \
	  fi; \
	  sleep 1; \
	done

ctocfiles: ctoc ${CTOCFILES}

${CTOCFILES}: ${@:ctoc/%=%}
	mkctoc ${@:ctoc/%=%} ${SIZE}

# For workshops
workshop/book.ps: title.${SIZE} ${ALL-SOURCES} texts
	@echo +++ $@
	mv ${SIZE}/chapters.ps ${SIZE}/book.ps
	emacs -batch ${SIZE}/book.ps -l $F/massageps.el -f massageps2 -f exit
	rm -f ${SIZE}/*~
# Touch the file so that it's newer than the log files
	touch $@

# XXX The stuff below until the next XXX is a set of quick hacks to
# get the build working before I finishe the Grand New Scheme Of
# Things.

# We don't have a ctoc for foreward, but we need this
# to avoid an error message
ctoc/foreword.mm ctoc/10-years.mm:
	touch $@

# XXX end of particularly ugly hacks.

# Dummy target.  If it's included as a dependency, it causes the
# target to be executed, since there's no file called .FORCE
.FORCE:
