# Remake individual chapters
foreword.ps preface.ps ${POSTSCRIPT}:  ${SIZE}/Toc/book Chapter/${SIZE}/ps \
	   Chapter/${SIZE}/nps ${SIZE}/chapternumbers 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} 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} - \
	 >Chapter/${SIZE}/ps/$@.tmp 2>${SIZE}/Toc/$$base; \
	exitcode=$$?; \
	mv Chapter/${SIZE}/ps/$@.tmp Chapter/${SIZE}/ps/$@; \
	startpage=$$page; \
	page=`awk < Chapter/${SIZE}/ps/$@ '/%%Page:/ {page=$$2 }; END {print page}'`; \
	if [ "$$prefix" != "" ]; then \
	  filename=Chapter/${SIZE}/nps/$$prefix-$@; \
	elif [ $$chapter -lt 10 ]; then \
	  filename=Chapter/${SIZE}/nps/0$$chapter-$@; \
	else \
	  filename=Chapter/${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 Chapter/${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 Chapter/${SIZE}/ps/`basename $@`;


# Appendices, index and bibliography--text version
Chapter/${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} - \
	  >Chapter/${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 $@

# 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} - \
	  >Chapter/${SIZE}/appendix.ps.tmp 2>${SIZE}/Toc/appendix
	mv Chapter/${SIZE}/appendix.ps.tmp Chapter/${SIZE}/appendix.ps
	-egrep -v ^'File|PSPIC|><' ${SIZE}/Toc/appendix
# Touch the file so that it's newer than the log files
	touch $@

