#################################################################
# Perform a quick build of the Comprehensive LaTeX Symbol List, #
# including a complete index.                                   #
#                                                               #
# Author: Scott Pakin <scott-clsl@pakin.org>                    #
#################################################################

include definitions.ninja
subninja common-deps.ninja

# Remove old index files to speed up the initial build.
# (Technically, only the .ind file needs to be removed.)
rule remove-index
  command = $
    workdir=build-$SIZE ; $
    test -d "$$workdir" || mkdir "$$workdir" ; $
    rm -f "$$workdir/symbols-$SIZE.ind" $
          "$$workdir/symbols-$SIZE.idx" $
          "$$workdir/symbols-$SIZE.ilg" $
          "$$workdir/symbols-$SIZE-full.ind" $
          "$$workdir/symbols-$SIZE-full.idx" ; $
    touch "$$workdir/step-1.stamp"
  description = Remove index files from $$workdir

# Produce an initial build of the document.
rule run-latex-1
  command = $
    set -e ; $
    workdir=build-$SIZE ; $
    export max_print_line=1048576 ; $
    $LATEX -output-directory="$$workdir" -jobname symbols-$SIZE '\PassOptionsToClass{${SIZE}paper}{article}\input symbols' ; $
    touch "$$workdir/step-2.stamp"
  pool = console
  description = $LATEX symbols-$SIZE (run 1 of 2)

# Create an index based on the current layout.  We do some
# postprocessing of the .idx file to improve usability.  However, we
# back up the pre-pruned list so we later can get an accurate symbol
# count.
rule create-index
  command = $
    set -e ; $
    workdir=build-$SIZE ; $
    ./patch-idx "$$workdir/symbols-$SIZE.idx" ; $
    makeindex -s symbols.ist "$$workdir/symbols-$SIZE" ; $
    cp "$$workdir/symbols-$SIZE.ind" "$$workdir/symbols-${SIZE}-full.ind" ; $
    ./prune-idx "$$workdir/symbols-$SIZE.idx" $PRUNETOML ; $
    makeindex -s symbols.ist "$$workdir/symbols-$SIZE" ; $
    touch "$$workdir/step-3.stamp"
  description = makeindex symbols-$SIZE
  pool = console

# Build the document with the newly generated index.  Afterward, the
# table of contents and PDF bookmarks files should point to the
# correct index pages, but these are not yet incorporated into the
# document.
rule run-latex-2
  command = $
    set -e ; $
    workdir=build-$SIZE ; $
    export max_print_line=1048576 ; $
    $LATEX -output-directory="$$workdir" -jobname symbols-$SIZE '\PassOptionsToClass{${SIZE}paper}{article}\input symbols' ; $
    cp "$$workdir/symbols-$SIZE.pdf" . ; $
    touch "$$workdir/step-4.stamp"
  pool = console
  description = $LATEX symbols-$SIZE (run 2 of 2)

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

build build-letter/step-1.stamp : remove-index
  SIZE = letter

build build-letter/step-2.stamp build-letter/symbols-letter.pdf : run-latex-1 build-letter/step-1.stamp
  SIZE = letter

build build-letter/step-3.stamp build-letter/symbols-letter.ind build-letter/symbols-letter-full.ind : create-index build-letter/step-2.stamp
  SIZE = letter

build build-letter/step-4.stamp symbols-letter.pdf : run-latex-2 build-letter/step-3.stamp
  SIZE = letter

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

build build-a4/step-1.stamp : remove-index
  SIZE = a4

build build-a4/step-2.stamp build-a4/symbols-a4.pdf : run-latex-1 build-a4/step-1.stamp
  SIZE = a4

build build-a4/step-3.stamp build-a4/symbols-a4.ind build-a4/symbols-a4-full.ind : create-index build-a4/step-2.stamp
  SIZE = a4

build build-a4/step-4.stamp symbols-a4.pdf : run-latex-2 build-a4/step-3.stamp
  SIZE = a4
