#!/bin/sh

progname=`basename $0`

if [ ! -f "`kpsetool -w cnf texmf.cnf`" ]; then
	echo "$progname: global Kpathsea configurarion file texmf.cnf not found." >&2
	echo >&2
	echo "Please set the environment variable TETEXDIR or TEXMFCNF correctly." >&2
	echo "For details see the teTeX and the Kpathsea manual" >&2
	exit
fi

case $# in
0)	echo "usage: `basename $0` file[.idx] ..." 1>&2
	exit 1 ;;
esac

if [ ! -z "$INDEXSTYLE" ]; then
	if [ ! -f $INDEXSTYLE/gind.ist ]; then
		echo "$progname: gind.ist does not exist in \$INDEXSTYLE" >&2
		exit
	fi
	gind=gind.ist
	if [ ! -f $INDEXSTYLE/gglo.ist ]; then
		echo "$progname: gglo.ist does not exist in \$INDEXSTYLE" >&2
		exit
	fi
	gglo=gglo.ist
else
	gind=`kpsetool -w -n latex tex gind.ist`
	gglo=`kpsetool -w -n latex tex gglo.ist`
	if [ ! -f "$gind" ] || [ ! -f "$gglo" ] ; then
		echo "$progname: gind.ist or gglo.ist not found. Aborting." >&2
		echo "Maybe, you need to set INDEXSTYLE appropriately." >&2
		echo "See makeindex(1) for more information." >&2
		exit 1
	fi
fi

# remember starting directory
dir=$PWD

# loop over all files
for i
do
	destdir=`dirname $i`
	test -d $destdir||continue
	cd $destdir
	FILENAME=`basename $i .idx`
	test -f $FILENAME.idx && makeindex -s $gind $FILENAME
	test -f $FILENAME.glo && makeindex -s $gglo -o $FILENAME.gls $FILENAME.glo
	cd $dir
done
