#!/bin/bash
    dtxname=isodoc
    dtxtype=class
        ext=cls
     delete=$dtxname.$ext
 dtxversion=$(grep " v[0-9.]\+[a-z]\{0,1\} $dtxname $dtxtype" $dtxname.dtx |sed 's/.* v\([0-9.]\+[a-z]\{,1\}\) .*/\1/')
executables=(mk texlog_extract zip getopt pdfseparate pdflatex)

    version=1.00
     myname=$(basename $0)

<<'DOC'
= inst - install isodoc

= Synopsis
inst [options]	

Without any options, inst installs simplot iin the first writable
TEXMFMAIN, TEXMFLOCAL or TEXMFHOME tree.

Options:
-h,--help	print this help and exit
-H,--Help	print full documentation via less and exit
-V,--version	print version and exit


= Description
inst must be run in its own directory (|./inst|) and then does the following:
- generates the documentation for isodoc,
- installs isodoc in one of your TeX trees: TEXMFMAIN, TEXMFLOCAL or TEXMFHOME. 
  The first one writable by you will chosen.
- creates a zip file named |isodoc-x.yy.zip| for upload to CTAN
- cleans up

= Author and copyright
Author	Wybo Dekker
Email	U{wybo@dekkerdocumenten.nl}{wybo@dekkerdocumenten.nl}
License	Released under the U{www.gnu.org/copyleft/gpl.html}{GNU General Public License}
DOC

    die() { echo -e "$myname: $Err$@$Nor" 1>&2; exit 1; }
   help() { sed -n '/^= Synopsis/,/^= /p' $0|sed '1s/.*/Usage:/;/^= /d'; exit; }
helpall() { sed -n '/^<<.DOC.$/,/^DOC$/p' $0|sed -n '1d;$d;p'|less; exit; }
version() { echo $version; exit; }

Nor='\e[0m'    # reset color	]
Err='\e[31;1m' # light red	]

setdir() { # create installation directory
   for i in MAIN LOCAL HOME; do
      tree=$(kpsewhich --expand-var \$TEXMF$i)
      test -w $tree && break
      tree=
   done
   [[ -n $tree ]] || die "Could not find a writable TeX tree"
   insttex=${tree}/tex/latex/$dtxname
   instsrc=$tree/source/latex/$dtxname
   instdoc=$tree/doc/latex/$dtxname
   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'$dtxversion'}/,/^% }/p' $dtxname.dtx  |
sed    's/^%//;s/\\\\$//
	/<.readme>/d
	/^ }/d
	s/ \\changes.*/Changes in version '$dtxversion':/
	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
}

clean() { # clean but keep what goes in the zip
   rm -f $dtxname.{aux,fls,glo,gls,idx,ilg,ind,log,out,toc}
   rm -f examples/*/{$dtxname*,*.{pdf,aux,log,fls,out}}
   find examples -type l -delete
}

makeall() {
   grep '%<\*install>' $dtxname.dtx >/dev/null && # for self-extracting dtx files
      delete+=" $dtxname.ins" ||
      echo y |tex $dtxname.ins >/dev/null

   # install any .ttf files
   mkdir -p ~/.fonts
   find examples -name '*.ttf' -exec cp {} ~/.fonts \;
   fc-cache ~/.fonts

   readme # create the README file

   # compile all examples
   cd examples
   for i in *; do
      echo compiling $i
      cd $i
      for j in ../../{$dtxname.cls,languages/isodoc*.ldf}; do ln -sf $j; done
      mk --noprint --noview $i && mk -c $i
      test -e logoletter.pdf && pdfseparate -l 2 logoletter.pdf logo%d.pdf
      cd ..
   done
   cd ..
   # make $dtxname.pdf
   echo compiling the manual
   pdflatex --recorder --interaction=batchmode $dtxname.dtx >/dev/null ||
      die "$(texlog_extract $dtxname.log)"
   test -f $dtxname.glo && makeindex -q -s gglo.ist -o $dtxname.gls $dtxname.glo
   test -f $dtxname.idx && makeindex -q -s gind.ist -o $dtxname.ind $dtxname.idx
   pdflatex --interaction=batchmode $dtxname.dtx > /dev/null
   pdflatex --interaction=batchmode $dtxname.dtx > /dev/null
}

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

   rm -rf $insttex/* $instsrc/* $instdoc/* # remove old data in the tree
   cp -f $dtxname.$ext languages/iso* $insttex
   cp -a $dtxname.{ins,dtx} $instsrc
   cp -a README inst languages/isodoc-template.ldf $dtxname.pdf examples/* $instdoc
   mktexlsr $tree 2>/dev/null
   rm -f $delete 
   cd ..
   zipfile=$dtxname/$dtxname-$dtxversion.zip
   rm -f $zipfile
   zip -rq $zipfile $dtxname/*
   cd $dtxname
   rm -f $dtxname.pdf README
}

clean=false
options=$(getopt \
   -n $myname \
   -o hHVc \
   -l help,Help,version,clean \
   -- "$@"
) || exit 1
eval set -- "$options"
while [ $# -gt 0 ]; do
   case $1 in
   (-h|--help)	  help;;
   (-H|--Help)	  helpall;;
   (-V|--version) version;;
   (-c|--clean)	  clean=true;shift;;
   (--)		  shift;  break;;
   (*)			  break;;
   esac
done
[[ -z $1 ]] || die No arguments expected

testexecs
setdir
$clean && clean && rm -f $delete $dtxname-*.zip $dtxname.pdf README && exit 0 
makeall
installall
