#!/bin/sh
# Thomas Esser, David Aspinall, Simon Wilkinson, 1998, 1999. Public domain.
#
# Simple script to attempt to find documentation for tex files.
# Uses kpsewhich to find a .dvi, .pdf or .ps file along the
# 'TeX system documentation' ($TEXDOCS, default: $TEXMF/doc) search path.
#
# Original version by David Aspinall <da@dcs.ed.ac.uk>
#
# This version rewritten for use with bash 2 and teTeX under Linux by
# Simon Wilkinson <sxw@dcs.ed.ac.uk>
#
# Changes for web2c-7.2 resp. teTeX-0.9 and portability fixes by
# Thomas Esser <te@informatik.uni-hannover.de>, Jun 14 1998
#
# Debian specific changes:
#    Denis Barbier <barbier@imacs.polytechnique.fr>, Feb 15 2000
#  * accept gzipped files on input
#  * temporary directory set according to TMPDIR

test -f /bin/sh5 && test -z "$RUNNING_SH5" \
  && { UNAMES=`uname -s`; test "x$UNAMES" = xULTRIX; } 2>/dev/null \
  && { RUNNING_SH5=true; export RUNNING_SH5; exec /bin/sh5 $0 ${1+"$@"}; }
unset RUNNING_SH5

test -f /bin/bsh && test -z "$RUNNING_BSH" \
  && { UNAMES=`uname -s`; test "x$UNAMES" = xAIX; } 2>/dev/null \
  && { RUNNING_BSH=true; export RUNNING_BSH; exec /bin/bsh $0 ${1+"$@"}; }
unset RUNNING_BSH

# Viewing programs, according to filename extension.  (You can
# override or add to them by setting environment variables).
: ${TEXDOCVIEW_dvi='( xdvi %s ) &'}
: ${TEXDOCVIEW_pdf='( acroread %s ) &'}
: ${TEXDOCVIEW_ps='( ghostview %s ) &'}
: ${TEXDOCVIEW_html='( netscape %s ) &'}
: ${TEXDOCVIEW_txt="${PAGER-more} %s"}

# Commands run to uncompress files, according to filename extension.
: ${TEXDOCUNZIP_gz='gzip -d -c'}
: ${TEXDOCUNZIP_bz2='bzip2 -d -c'}
: ${TEXDOCUNZIP_zip='unzip -p'}

mode=viewer
help='Usage: texdoc [OPTION]... [NAME]...
  Search for NAME in the TeX documentation and start a viewer.

  --help        show this help
  -v		verbose mode: show viewer command
  -l            just list all matching files. Do not start a viewer.'

verbosemode=false
while 
  case $1 in
     -l)     mode=list;;
     -v)     verbosemode=true;;
     *-help)
             echo "$help" >&2
             exit 1;;
     -*)     echo "texdoc: option $1 not recognized" 1>&2;;
      *)     break;;
  esac
do shift; done

tmpdir=${TMPDIR-/tmp}/texdoc$$

umask 077
test -d $tmpdir && { echo "$tmpdir: directory already exists."; exit; }
trap 'rc=$?; cd / ; rm -rf $tmpdir; exit $rc' 1 2 6 13 15
mkdir $tmpdir || exit

clean_tmp ()
{
  cd /
  if test "`echo $tmpdir/*`" = $tmpdir/'*'
  then
    rm -rf $tmpdir
  fi
}

#listext=`kpsewhich --expand-brace='$TEXDOCEXT'`
#: ${listext=':.gz'}
listext='.dvi.gz:.pdf.gz:.ps.gz:.txt.gz:.dvi:.pdf:.ps:.txt:.html:""'

for name
do
  found=false
  OIFS=$IFS
  IFS=':'
  for ext in $listext; do

    filename=`kpsewhich -format='TeX system documentation' $name$ext 2>/dev/null`
    test -z "$filename" && continue
    found=true

    if test $mode = list; then
      echo $filename
    else
      dir=`echo $filename | sed 's%/[^/]*$%%'`
      ext=`echo $filename | sed 's%.*\.%%'`
      eval uncompress="\$TEXDOCUNZIP_$ext"
      if test -n "$uncompress"
      then
        ext=`echo $filename | sed -e "s|\\.$ext\$||" | sed 's%.*\.%%'`
      fi
      viewer=\$"TEXDOCVIEW_$ext"
      if test -n "$uncompress"
      then
        src=`echo "$filename" | sed -e 's%.*/%%' -e 's%\.[^.]*$%%'`
        eval "$uncompress $filename > $tmpdir/$src"
        filename=$tmpdir/$src
        viewer=`eval echo $viewer | sed -e "s|%s|$filename; rm -f $filename; clean_tmp;|g"`
      else
        viewer=`eval echo $viewer | sed -e "s|%s|$filename|g"`
      fi
      if test -z "$viewer"
      then 
        echo "Don't know how to view file type $ext" 1>&2
        echo "(matching file was $filename)" 1>&2
      else 
        $verbosemode && echo $viewer
        test -n "$dir" && test -d "$dir" && cd "$dir"
        eval $viewer
        break     # just stop after the first usable extension
      fi
    fi

  done
  $found || echo "Can't find documentation for \`$name'" 1>&2
  IFS=$OIFS
done
clean_tmp
exit 0
