#!/bin/bash

       NAME=isodoc
    DOCTYPE=class
        EXT=cls
    VERSION=`grep " v[0-9.]\+ $NAME $DOCTYPE" $NAME.dtx |sed 's/.* v\([0-9.]\+\) .*/\1/' `
EXECUTABLES=(pdfseparate zip unzip getopt)

function die {
   echo -e "$*"
   exit 1
}

function help {
   cat <<-EOD
	This is inst - install the $NAME LaTeX $DOCTYPE
	Options:
	-h, --help	print this help
	-c, --clean     remove all files created by the installation, except the zip file
	-C, --Clean	remove all files that can be regenerated
	-z, --zip	Create zip for CTAN (developer only)

	Without any options, inst creates $NAME.{$EXT,ins,pdf} and installs these, 
	together with isodocsymbols.sty,
	in the local TeX tree (\$TEXMFLOCAL) or, if that is not writable,
	in the user´s TeX tree (\$TEXMFHOME). Also, a zip file is created for upload to CTAN.
	EOD
   exit
}

function setdir { # create installation directory
   TREE=$(kpsewhich --expand-var '$TEXMFLOCAL')
   test -w $TREE || TREE=$(kpsewhich --expand-var '$TEXMFHOME')
   test "$TREE" = "" && die Could not find value for $TREE
   INSTDIR=${TREE}/tex/latex/$NAME
   SOURCE=$TREE/source/latex/$NAME
   mkdir -p $INSTDIR || die could not create $INSTDIR
   mkdir -p $SOURCE || die could not create $SOURCE
}

function testexecs { # test presence of executables
   for i in ${EXECUTABLES[*]}; do
      type $i &> /dev/null || die executable $i not found
   done
}

function readme { # generate the README file
  sed -n "/^%<\*readme>/,/^%<\/readme>/p;/\\\\changes{v$VERSION}/,/^% }/p" $NAME.dtx  |
  sed  "s/^%//;s/\\\\\\\\$//;/<.readme>/d;/^ }/d;s/ \\\\changes.*/Changes in version $VERSION:/" >README
}

function clean { # clean but keep what goes in the zip
   rm -f $NAME.{aux,fls,glo,gls,idx,ilg,ind,log,out,toc}
   rm -f examples/*/{$NAME*,*.{pdf,aux,log,out}}
}

function Clean {
   clean
   rm -f $NAME.{$EXT,pdf} README
}

function makeall {
   echo y |tex $NAME.ins >/dev/null
   # install any .ttf files
   mkdir -p ~/.fonts
   find examples -name '*.ttf' -exec cp {} ~/.fonts \;
   fc-cache ~/.fonts

   # compile all examples

   cd examples
   for i in *; do
      cd $i
      for j in ../../{$NAME.cls,isodocsymbols.sty,languages/isodoc*.ldf}; do ln -sf $j; done
      read TEX <$i.tex
      if [[ $TEX =~ ^%! ]]; then TEX=${TEX#%!}; else TEX=pdflatex;fi
      $TEX --interaction=batchmode $i >/dev/null || die error compiling $i
      $TEX --interaction=batchmode $i >/dev/null || die error compiling $i
      test -e logoletter.pdf && pdfseparate -l 2 logoletter.pdf logo%d.pdf
      cd ..
   done
   cd ..
   # make $NAME.pdf
   pdflatex --recorder --interaction=batchmode $NAME.dtx >/dev/null ||
      die "`texlog_extract $NAME.log`"
   test -f $NAME.glo && makeindex -q -s gglo.ist -o $NAME.gls $NAME.glo
   test -f $NAME.idx && makeindex -q -s gind.ist -o $NAME.ind $NAME.idx
   pdflatex --interaction=batchmode $NAME.dtx > /dev/null
   pdflatex --interaction=batchmode $NAME.dtx > /dev/null
}

function installall {
   # install and cleanup
   echo installing in $INSTDIR
   mv -f $NAME.{$EXT,pdf} $INSTDIR
   cp -a isodocsymbols.sty languages $INSTDIR
   cp -a $NAME.{ins,dtx} $SOURCE
   mktexlsr $TREE 2>/dev/null
   clean
}

function mkzip {
   clean
   readme
   cd ..
   zipfile=$NAME/$NAME-$VERSION.zip
   rm -f $zipfile
   zip -Drq $zipfile $NAME/* -x $NAME/test/*
   cd $NAME
   Clean
}

testexecs
setdir

if ! options=$(getopt -o hcz \
                      -l help,clean,zip -- "$@"); then exit 1; fi
eval set -- "$options"

while [ $# -gt 0 ]; do
   case $1 in
   -c|--clean) Clean; exit;;
   -z|--zip)   makeall; mkzip; exit;;
   -h|--help)  help;;
   (--)        shift; break;;
   (*)         break;;
   esac
   shift
done
test "$1" = "" || die No arguments expected

makeall
installall
