#!/bin/bash

       NAME=ctable
    DOCTYPE=package
        EXT=sty
    VERSION=`grep " v[0-9.]\+[a-z]\{,1\} $NAME $DOCTYPE" $NAME.dtx |sed 's/.* v\([0-9.]\+[a-z]\{,1\}\) .*/\1/' `
EXECUTABLES=(zip getopt)

die() {
   echo -e "$*"
   exit 1
}

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
	-z, --zip	Create zip for CTAN (no install, developer only)

        Without any options, inst installs $NAME in the first writable
        TEXMFMAIN, TEXMFLOCAL or TEXMFHOME tree.
	EOD
   exit
}

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
}

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

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
}

clean() { # clean but keep what goes in the zip
   rm -f $NAME.{aux,fls,glo,gls,idx,ilg,ind,log,out,toc,$EXT}
   rm -f doc/{$NAME.$EXT,*.{pdf,aux,log,out}}
   rm -f doc/{s,}[0-9][0-9][a-z].*
}

Clean() {
   clean
   rm -f $NAME.pdf README
}

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

   # create the README file
   readme

   # compile all examples
   cd doc
   ln -sf ../$NAME.sty || exit 1 # use the version to be installed
   for i in [0-9][0-9]?; do 
      if [ ! -f $i.pdf -o ! -f s$i.pdf ]; then
         ./doit || exit 1
         break
      fi
   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
}

installall() {
   # install and cleanup
   echo installing in $TREE

   rm -rf $INSTTEX/* $INSTSRC/* $INSTDOC/*
   cp -f $NAME.$EXT $INSTTEX
   cp -a $NAME.{ins,dtx} $INSTSRC
   cp -a README inst $NAME.pdf $INSTDOC
   mktexlsr $TREE 2>/dev/null
   clean
}

mkzip() {
   clean
   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
