#!/bin/sh
#
#  Script for converting the CGAL manuals to PDF with bookmarks. 
#  Have to do a separate run of latex to generate this because otherwise
#  the things that are linked via the hyperref package are colored in the
#  PostScript manual as well.
#
#  N.B.  gs version 6.51 works best for the basic library conversion.
#        version 6.50 gives up or segmentation faults or does other nasty
#        things.

if [ $# -ne 0 ] ; then
  DOCDIRS="$*"
elif [ -f wrapper.tex ]; then
  DOCDIRS="."
elif [ -f docdirs ]; then
  DOCDIRS=`cat docdirs`
else
  DOCDIRS=""
fi

DISTILL="/opt/adobe/AcroDist_2.1/bin/distill"

origTEXINPUTS=$TEXINPUTS

TEXINPUTS=../../examples/:../../demo:$TEXINPUTS
for DIR in $DOCDIRS; do
   TEXINPUTS=$DIR:$TEXINPUTS
   cd $DIR
   echo "now in directory $DIR..."
   CURR_DIR=`pwd`

   sed -e '
s/\\setboolean{usehyperref}{false}/\\setboolean{usehyperref}{true}/
s/\\setboolean{useminitoc}{true}/\\setboolean{useminitoc}{false}/
' wrapper.tex > pdf_wrapper.tex
   
   if [ -f docdirs_passed_tex ] ; then
      DOC_SUBDIRS=`cat docdirs_passed_tex`
   elif [ -f docdirs ] ; then
      DOC_SUBDIRS=`cat docdirs`
   else
      DOC_SUBDIRS=""
   fi
   IONLY="\includeonly{"
   for SUBDIR in ${DOC_SUBDIRS}; do
      TEXINPUTS=${SUBDIR}:${SUBDIR}_ref:$TEXINPUTS
      IONLY="$IONLY,${SUBDIR}/main,${SUBDIR}_ref/main"
   done
   IONLY="$IONLY}"

   echo $IONLY > ionly
   export TEXINPUTS
   
   # must get rid of the .aux files as they may contain table of contents
   # commands from minitoc that should not be used with the hyperref package
   if [ -f docdirs ] ; then
      \rm -f */*.aux
   fi
   echo
   echo "  ...$DIR's first LaTeX pass"
   echo
   latex pdf_wrapper

   echo
   echo "  ...$DIR's bibtex "
   echo
   bibtex pdf_wrapper
   # add table of contents line for bibliography
   if [ -f pdf_wrapper.bbl ]; then
      sed 's/\\begin{thebibliography}{.*}/&\\lcTex{\\addcontentsline{toc}{chapter}{Bibliography}}/' pdf_wrapper.bbl > new_pdf_wrapper.bbl
      \mv new_pdf_wrapper.bbl pdf_wrapper.bbl
   fi

   if [ -f pdf_wrapper.idx ]; then
      echo
      echo "  ...making $DIR's index"
      echo
      makeindex pdf_wrapper
      index_fix pdf_wrapper.ind
   fi

   echo
   echo "  ...$DIR's second LaTeX pass "
   echo
   latex pdf_wrapper

   if [ -f pdf_wrapper.ref ]; then
       echo
       echo " ... sorting reference pages "
       echo
       sort -f +2 -3 pdf_wrapper.ref > pdf_wrapper.ref_sorted
       cp pdf_wrapper.ref_sorted pdf_wrapper.ref
   fi

   echo
   echo "  ... $DIR's final LaTeX pass"
   echo
   latex pdf_wrapper

   echo
   echo "  ... creating $DIR's PostScript"
   echo

   dvips -t letter -o pdf_wrapper.ps pdf_wrapper

   echo
   echo "  ... creating $DIR's PDF"
   echo
   ps2pdf pdf_wrapper.ps
   case $CURR_DIR in
      *installation)     \mv pdf_wrapper.pdf installation.pdf;;
      *general)          \mv pdf_wrapper.pdf general_intro.pdf;;
      *kernel)           \mv pdf_wrapper.pdf kernel.pdf;;
      *kernel_d)         \mv pdf_wrapper.pdf kernel_d.pdf;;
      *basic)            \mv pdf_wrapper.pdf basic_lib.pdf;;
      *support)          \mv pdf_wrapper.pdf support_lib.pdf;;
      *use_of_stl)       \mv pdf_wrapper.pdf use_of_stl.pdf;;
      *)
              echo "UNKNOWN manual part $CURR_DIR; creating ref-manual.pdf";
              \mv pdf_wrapper.pdf ref-manual.pdf
   esac
   echo "...leaving directory $DIR"
   cd ..
done

TEXINPUTS=$origTEXINPUTS
export TEXINPUTS


syntax highlighted by Code2HTML, v. 0.9.1