#!/bin/sh

# makeindex -- a Kpathsea wrapper for the makindex index processor.
# (C) 1996, Thomas Esser. Idea and first version by Matthias Clasen.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# We need to search for makeindex.bin, since I want to create a
# symlink to it. The reason behind is to call makeindex.bin via
# that symlink (its name will be makeindex) to get "makeindex"
# in the verbose output, rather than "makeindex.bin"

check_for_binary()
{
  testbin=$1;
  o=$IFS; IFS=:; eval set $PATH; IFS=$o;
  for i
  do
    if [ -x "$i/$testbin" ]; then
      echo "$i/$testbin"
      return 0
    fi
  done
  return 1
}

# for kpsetool and verbose messages:
prog=`basename $0`

# We search along "kpsetool -w tex ...", so map INDEXSTYLE into TEXINPUTS.
INDEXSTYLE=`kpsetool -v -n $prog '$INDEXSTYLE'`
test -n "$INDEXSTYLE" && { TEXINPUTS="$INDEXSTYLE"; export TEXINPUTS; }
unset INDEXSTYLE

mkidxbin=`check_for_binary $prog.bin`
if test ! -x "$mkidxbin"; then
  echo "$prog: cannot execute $prog.bin. Aborted." >&2
  exit 1
fi
TEMPDIR=${TMPDIR-/tmp}/mkidx.$$
trap 'cd /; rm -rf $TEMPDIR' 1 2 15
test -d $TEMPDIR || mkdir $TEMPDIR
ln -s $mkidxbin $TEMPDIR/$prog

# The following is needed to handle envvars like INDEXSTYLE=~: correctly.
# We put the INDEXSTYLE from texmf.cnf into a temp. config file and
# set TEXMFCNF:
echo "TEXINPUTS = `kpsetool -v -n $prog '$INDEXSTYLE'`" > $TEMPDIR/texmf.cnf
TEXMFCNF="$TEMPDIR:$TEXMFCNF"; export TEXMFCNF

# If KPATHSEA_DEBUG is set, do not redirect kpsetool's stderr to /dev/null.
debug=
test "x${KPATHSEA_DEBUG}${KPATHSEA_DEBUG-x}" = xx && debug='2>/dev/null'

# Analyze the positional arguments. Only -s is "special".
unset expargs
while test "x$1" != x; do
  expargs="$expargs $1"
  case "$1" in
  -s)
     case "$2" in
     "")
         echo "Expected -s <stylefile>" >&2
         echo "Usage: $prog [-ilqrcg] [-s sty] [-o ind] [-t log] [-p num] [idx0 idx1 ...]" >&2
         exit 1;;
     *.ist)
        eval style=\`kpsetool -w -n $prog tex \"$2\" $debug\`;;
     *)
        eval style=\`kpsetool -w -n $prog tex \"$2.ist\" $debug\`;;
     esac
     if test -z "$style" || test ! -f "$style"; then
       echo "Index style file $2 not found." >&2
       echo "Usage: $prog [-ilqrcg] [-s sty] [-o ind] [-t log] [-p num] [idx0 idx1 ...]" >&2
       exit 1
     fi
     # remove ./ from the filename: it is part of the verbose messages...
     style=`echo "$style" | sed 's/^\.\///'`
     expargs="$expargs $style"
     shift;;
  esac
  shift
done

$TEMPDIR/$prog $expargs
rm -rf $TEMPDIR
