#! /bin/sh

# Bourne shell script to process the Beginner's LaTeX document
# GNU Copyleft 2002, 2003 Silmaril Consultants ([email protected])
# This should probably be rewritten as a Makefile one day

# Dependencies: cat, sed, saxon, java, awk, latex, bibtex,
#               makeindex, dvips, gs, gzip, xdvi, mv, bash

echo WARNING: you must use Saxon 8 for this because earlier versions
echo fail to handle nested parameter entity references correctly.

####################################################################

# 1. Generate LaTeX source, switching any governing target to latex

# Omit tugbook format now that it's been published

for s in typebook; do

   sed -e "s+target \"html\"+target \"latex\"+" beginlatex.xml >beginlatex.XML

   saxon beginlatex.XML $s-latex.xslt beginlatex-$s.tex style=$s

# 2. Process the LaTeX to DVI, iterating to resolve cross-references

   latex \\nonstopmode\\input beginlatex-$s.tex
   bibtex beginlatex-$s
   cp beginlatex-$s.bbl beginlatex.bbl
   latex \\nonstopmode\\input beginlatex-$s.tex
   latex \\nonstopmode\\input beginlatex-$s.tex
   makeindex -s indexhead.ist beginlatex-$s
   latex \\nonstopmode\\input beginlatex-$s.tex
#    pdflatex \\nonstopmode\\input beginlatex-$s.tex
   mv beginlatex-$s.pdf beginlatex-$s-pdflatex.pdf

if [ "$1" = "short" ]; then exit; fi

# 3. Generate PostScript and PDF

   rm -f beginlatex-$s.ps.gz

   if [ "$s" = "tugbook" ]; then

       dvips -t letter -o beginlatex-$s.ps beginlatex-$s

       gs -sDEVICE=pdfwrite -sPAPERSIZE=letter \
           -sOutputFile=beginlatex-$s-psdvips.pdf  -q - <beginlatex-$s.ps

   else

       dvips -t a4 -o beginlatex-$s.ps beginlatex-$s

       gs -sDEVICE=pdfwrite -sPAPERSIZE=a4 \
           -sOutputFile=beginlatex-$s-psdvips.pdf  -q - <beginlatex-$s.ps

   fi

   gzip beginlatex-$s.ps

# 4. Pop up the preview if it's not already there

   XDVI=`ps axu | grep xdvi | grep -v grep`

   if [ -z "$XDVI" ]; then
       xdvi beginlatex-$s &
   fi

done

if [ "$1" = "medium" ]; then exit; fi

# 5. Do the single-file HTML

sed -e "s+target \"latex\"+target \"html\"+" beginlatex.xml >beginlatex.XML

saxon beginlatex.XML typebook-html.xslt beginlatex.html

# 8. Do the multi-file HTML by passing the ID value of the prelims,
#    the chapters, the appendices, the bibliography, and the index
#    through to Saxon, where the XSLT stylesheet uses them to output
#    just that section.

for id in `nsgmls -wxml -E 5000 -f/dev/null \
          /usr/local/lib/sgml/xml/xml.dcl beginlatex.xml|\
       grep -iE '^(Aid|\(chapter|\(appendix|\(preface|\(index$|\(bibliography)'|\
       grep -v IMPLIED|\
       awk 'BEGIN {z[1]="A";z[2]="B";z[3]="C"} \
           /Aid/ {id=$3} \
               {name=substr($1,2)} \
               {if($1~"preface"){name=id}} \
               {if($1~"chapter"){++c;name="chapter" c}} \
               {if($1~"appendix"){++a;name="appendix" z[a]}} \
           /\(/ {print name "," id}'`; do

       FRAG=`echo $id|awk -F, '{print $1}'`
       ID=`echo $id|awk -F, '{print $2}'`
       echo $FRAG \($ID\)
       if [ "$FRAG" = "preface" ]; then
           FRAG=$ID
       fi

   saxon beginlatex.XML typebook-html.xslt $FRAG.html fragment=$ID

done

# 9. Move the output to the parent directory and HTML directory

echo Moving output files to the right place...

mv -f beginlatex*.ps.gz ..
mv -f beginlatex*.pdf ..
mv -f *.html ../html
cp -f *.png ../html

exit 0