#!/bin/bash

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

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

function setdir { # create installation directory
   for i in MAIN LOCAL HOME; do
      TREE=$(kpsewhich --expand-var \$TEXMF$i)
      test -w $TREE && break
      TREE=
   done
   test "$TREE" = "" && die "Could not find a writable TeX tree"
   INSTTEX=${TREE}/tex/latex/$NAME
   INSTSRC=$TREE/source/latex/$NAME
   INSTDOC=$TREE/doc/latex/$NAME
   mkdir -p $INSTTEX || die could not create $INSTTEX
   mkdir -p $INSTSRC || die could not create $INSTSRC
   mkdir -p $INSTDOC || die could not create $INSTDOC
}

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':/
	s/$\\Rightarrow\$/=>/g
	s/\\textbackslash/\\/g
	s/\\text\(sl\|it\){\([^}]\+\)}/\/\2\//g		# \textsl{...} -> /.../
	s/{\([^}]*\)}/\1/g 				# keep last, removes all {...}
       ' >README
  grep "Changes in version" README >/dev/null || die changes not detected
}

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,fls,out}}
}

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

   # create the README file
   readme

   # compile all examples
   cd examples
   for i in *; do
      cd $i
      for j in ../../{$NAME.cls,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 {
   # cleanup and install
   clean
   echo installing in $TREE
   rm -rf $INSTTEX/* $INSTSRC/* $INSTDOC/*
   cp -f $NAME.$EXT languages/iso* $INSTTEX
   cp -a $NAME.{ins,dtx} $INSTSRC
   cp -a README inst languages/template.ldf $NAME.pdf examples/* $INSTDOC
   mktexlsr $TREE 2>/dev/null
   rm $NAME.cls
   cd ..
   zipfile=$NAME/$NAME-$VERSION.zip
   rm -f $zipfile
   zip -Drq $zipfile $NAME/* -x $NAME/test/* -x $NAME.$EXT
   cd $NAME
   rm README $NAME.pdf
}

testexecs
setdir
makeall
installall
