# Create the file book.make, which is included by the main
# Makefile.  This is needed because each chapter depends
# on the previous chapter (due to the possibility of changed
# page numbers), so we need to build a chain of dependencies.
#
# Usage:
#   $0 Preface Chapters
#
# $Id: makebookmakefile,v 2.1 1997/12/07 08:34:53 grog Exp grog $
#
# $Log: makebookmakefile,v $
# Revision 2.1  1997/12/07 08:34:53  grog
# Base second edition
#
# Revision 1.14  1996/04/14 09:59:36  grog
# Checkin before path changes
#
# Revision 1.13  1996/04/08  12:21:33  grog
# Checkpoint 11 April
#
# Revision 1.11  1996/04/08  11:58:15  grog
# Get long and short versions right(er)
#
# Revision 1.9  1996/02/19  10:47:24  grog
# More mods
#
# Revision 1.8  1996/02/17  14:33:49  grog
# Minor mods
#
# Revision 1.7  1996/02/11  16:55:07  grog
# Minor mods
#
# Revision 1.6  1996/01/28  13:56:54  grog
# Checkpoint 28 January
#
# Revision 1.5  1996/01/20  11:34:21  grog
# Completely
#
# Revision 1.2  1996/01/03  11:09:36  grog
# Checkpoint 4 January 1996
#
# Revision 1.1  1995/12/16  10:59:05  grog
# Initial revision
#
# Revision 1.3  1995/12/11  14:14:23  grog
# Checkpoint 12 December
#
# Revision 1.2  1995/12/04  19:00:20  grog
# First final draft
#
# Revision 1.1  1995/10/31  16:03:38  grog
# Initial revision
#
#
# HEIGHT is the real height of the paper
HEIGHT=8.5i
# WIDTH should be 1.7" less than the real width of the paper
WIDTH=4.6i
TEXTSIZE=10		# Point size of standard text
LINESPACING=12		# Points spacing between standard text lines

SOURCES=$1
PSDOCS=$2
parms=$1		# save the original parameters
set $parms
headerstyle=2		# footer on first page, header on following
roffopts="-rex=\${EXAMPLE-SIZE} -rfirstpage=1 -r\$\$\$\$ -rL${HEIGHT} -rW${WIDTH} -rPS=${TEXTSIZE} -rLS=${LINESPACING} -rN$headerstyle"
stripcond=../tools/stripcond
BOOKMAKE=book.${SIZE}.make

echo >book.${SIZE}.make "# Makefile generated automatically by $0 on `date`"
echo >>book.${SIZE}.make '# Do not edit this file'
echo >>book.${SIZE}.make
echo >>book.${SIZE}.make HEIGHT = ${HEIGHT}
echo >>book.${SIZE}.make WIDTH  = ${WIDTH}
echo >>book.${SIZE}.make TEXTSIZE  = ${TEXTSIZE}
echo >>book.${SIZE}.make LINESPACING  = ${LINESPACING}

# base=`basename $1 .ps`		# $1 is preface, by convention
# realtarget=Chapter.${SIZE}/$base.ps
# echo >>book.${SIZE}.make $1 Chapter.${SIZE}/$1: '	Xref' $base.mm global.mm Xref chapternumbers.${SIZE} \${MACROFILE}
# # echo >>book.${SIZE}.make '	@echo' TARGET: \$@
# echo >>book.${SIZE}.make '	' @echo +++ Formatting $base to Chapter.${SIZE}/00-$base.ps
# echo >>book.${SIZE}.make '	' @\(echo \".af PN i\"\; gsoelim global.mm Xref $base.mm\) \| ${stripcond} ${SIZE} \| \\
# echo >>book.${SIZE}.make '	'  \${PIC} \| \${TBL} \| \\
# echo >>book.${SIZE}.make '	' groff $roffopts -mpic \${MACROFILE} -  \\
# echo >>book.${SIZE}.make '	' \>Chapter.${SIZE}/00-$base.ps 2\>Toc.${SIZE}/$base
# echo >>book.${SIZE}.make '	'@-egrep -v ^\'Chapter\|File\|\>\<\'  Toc.${SIZE}/$base
# echo >>book.${SIZE}.make '	'@echo 1 '>' Toc.${SIZE}/$base.firstafter
# echo >>book.${SIZE}.make '	'@rm -f $realtarget\; ln Chapter.${SIZE}/00-$base.ps $realtarget
# echo >>book.${SIZE}.make

previous=preface.ps
while [ "$1" != "" ]; do
  base=`basename $1 .ps`
  realtarget=Chapter.${SIZE}/$base.ps
  startpage=Toc.${SIZE}/`basename $previous .ps`.firstafter	# file with start page number
  echo >> book.${SIZE}.make $1 Chapter.${SIZE}/$1:  Chapter.${SIZE}/$previous $base.mm global.mm Xref chapternumbers.${SIZE} \${MACROFILE}
# echo >>book.${SIZE}.make '	@echo' TARGET: \$@
  echo >>book.${SIZE}.make '	' @title=$base\; \\
  echo >>book.${SIZE}.make '	 ' read \< $startpage page\; \\
  echo >>book.${SIZE}.make '	 ' echo -n \"+++ Chapter $base starts on page \$\$page \"\; \\
  echo >>book.${SIZE}.make '	' \(echo \".pn \$\$page\"\; gsoelim global.mm Xref chapternumbers.${SIZE} $base.mm\) \| \\
  echo >>book.${SIZE}.make '	' ${stripcond} ${SIZE} \| \\
  echo >>book.${SIZE}.make '	  ' \${PIC} \| \${TBL} \| \\
  echo >>book.${SIZE}.make '	  ' groff $roffopts -mpic \${MACROFILE} -  \\
  echo >>book.${SIZE}.make '	  ' \>$realtarget.tmp 2\>Toc.${SIZE}/$base\; \\
  echo >>book.${SIZE}.make '	' exitcode=\$\$?\; \\
  echo >>book.${SIZE}.make '	' mv $realtarget.tmp $realtarget\; \\
  echo >>book.${SIZE}.make '	' chapter=\`awk \< Toc.${SIZE}/$base \'/Chapter/ {chapter = \$\$2}\; END {print chapter}\'\`\; \\
  echo >>book.${SIZE}.make '	' chapter=\`basename \$\$chapter ,\`\; \\
  echo >>book.${SIZE}.make '	' startpage=\$\$page\; \\
  echo >>book.${SIZE}.make '	' page=\`awk \< $realtarget \'/%%Page:/ {page=\$\$2 }\; END {print page}\'\`\; \\
  echo >>book.${SIZE}.make '	' if [ \$\$chapter -lt 10 ]\; then \\
  echo >>book.${SIZE}.make '	  ' filename=Chapter.${SIZE}/0\$\$chapter-$base.ps\; \\
  echo >>book.${SIZE}.make '	' else \\
  echo >>book.${SIZE}.make '	  ' filename=Chapter.${SIZE}/\$\$chapter-$base.ps\; \\
  echo >>book.${SIZE}.make '	' fi\; \\
  echo >>book.${SIZE}.make '	' echo -n \"and ends on page \$\$page \"\; \\
  echo >>book.${SIZE}.make '	' page=\`expr \$\$page + 1\`\; \\
  echo >>book.${SIZE}.make '	' echo  \\\(\`expr \$\$page - \$\$startpage\` pages\\\): \$\$filename\; \\
  echo >>book.${SIZE}.make '	' if [ \`expr \$\$page % 2 \` -eq 0 ]\; then \\
  echo >>book.${SIZE}.make '	  ' echo \"    \(Last page is blank\)\"\; \\
  echo >>book.${SIZE}.make '	  ' page=\`expr \$\$page + 1\`\; \\
  echo >>book.${SIZE}.make '	' fi\; \\
  echo >>book.${SIZE}.make '	' echo \$\$page \> Toc.${SIZE}/$base.firstafter\; \\
  echo >>book.${SIZE}.make '	' egrep -v ^\'Chapter\|File\|\>\<\'  Toc.${SIZE}/$base\; \\
  echo >>book.${SIZE}.make '	' rm -f \$\$filename\; \\
  echo >>book.${SIZE}.make '	' ln $realtarget \$\$filename\; \\
  echo >>book.${SIZE}.make '	' if [ \$\$exitcode -ne 0 ]\; then \\
  echo >>book.${SIZE}.make '	  ' echo \"*** Exit code \$\$exitcode - format aborted\"\; \\
  echo >>book.${SIZE}.make '	' fi
  echo >>book.${SIZE}.make
  if [ "$2" = "" ]; then
    echo >> book.${SIZE}.make all: '	' Chapter.${SIZE} Toc.${SIZE} Chapter.${SIZE}/$base.ps Book.Toc
  fi
  previous=$1
  shift
done

lastchapter=$base		# for index
startpage=Toc.${SIZE}/`basename $previous .ps`.firstafter	# file with start page number of index
# Table of contents, preface and index
echo >>book.${SIZE}.make
# for base in toc preface index; do
for base in preface; do
   realtarget=Chapter.${SIZE}/$base.ps
   echo >>book.${SIZE}.make $base.ps Chapter.${SIZE}/$base.ps: '	Xref' $base.mm global.mm Xref chapternumbers.${SIZE} \${MACROFILE}
   # echo >>book.${SIZE}.make '	@echo' TARGET: \$@
   echo >>book.${SIZE}.make '	' @echo +++ Formatting $base to Chapter.${SIZE}/$base.ps
   if [ "$base" = "index" ]; then		     # arabic page numbers
     echo >>book.${SIZE}.make '	 ' @read \< $startpage page\; \\
     echo >>book.${SIZE}.make '	' \(echo \".pn \$\$page\"\; gsoelim global.mm Xref chapternumbers.${SIZE} $base.mm\) \| ${stripcond} ${SIZE} \| \\
   else if [ "$base" = "toc" ]; then
     echo >>book.${SIZE}.make '	' @\(gsoelim global.mm Xref chapternumbers.${SIZE} $base.mm\) \| \\
   else				# preface
# We have a problem with the preface.  Its page number depends on the size of 
# the table of contents, which depends on the rest of the book down to the
# preface: a perfect circular dependency.  Bet that it won't change too much,
# and ignore the dependency.
     echo >>book.${SIZE}.make '	 ' @read \< Toc.${SIZE}/toc.firstafter page\; \\
     echo >>book.${SIZE}.make '	' \(echo \".pn \$\$page\"\; gsoelim global.mm Xref chapternumbers.${SIZE} $base.mm\) \| ${stripcond} ${SIZE} \| \\
   fi; fi
   echo >>book.${SIZE}.make '	'  \${PIC} \| \${TBL} \| \\
   echo >>book.${SIZE}.make '	' groff $roffopts -mpic \${MACROFILE} -  \\
   echo >>book.${SIZE}.make '	' \>Chapter.${SIZE}/$base.ps 2\>Toc.${SIZE}/$base
   echo >>book.${SIZE}.make '	'@-egrep -v ^\'Chapter\|File\|\>\<\'  Toc.${SIZE}/$base
   if [ "$base" = "toc" ]; then
     echo >>book.${SIZE}.make '	' page=\`awk \< $realtarget \'/%%Page:/ {page=\$\$2 }\; END {print page}\'\`\; \\
     echo >>book.${SIZE}.make '	' page=\`expr \$\$page + 1\`\; \\
     echo >>book.${SIZE}.make '	' if [ \`expr \$\$page % 2 \` -eq 0 ]\; then \\
     echo >>book.${SIZE}.make '	  ' page=\`expr \$\$page + 1\`\; \\
     echo >>book.${SIZE}.make '	' fi\; \\
     echo >>book.${SIZE}.make '	' echo \$\$page \> Toc.${SIZE}/$base.firstafter\; \\
   else if [ "$base" = "preface" ]; then
     echo >>book.${SIZE}.make '	'@echo 1 '>' Toc.${SIZE}/$base.firstafter
   fi; fi
   echo >>book.${SIZE}.make
done

echo >>book.${SIZE}.make
set ${PSDOCS}
while [ "$2" != "" ]; do
  echo >>book.${SIZE}.make $2: $1
  shift
done