#!/bin/sh

# texhash
# Written by Thomas Esser <te@informatik.uni-hannover.de>, Okt., 1994

# texhash is intended to rebuild ls-R. Some special care is
# taken to keep ownership (if called by root) and file modes
# of ls-R. It should be called from the tex-admin after installing
# new files in $TEXMF.

progname=`basename $0`
TEXMF=`kpsexpand '$TEXMF'`

trap 'cd / ; rm -f $db_file_tmp' 1 2 15

if test ! -d "$TEXMF" ; then
	echo "$progname: fatal error: directory '$TEXMF' not found."
	echo "Please check your setup."
	exit
fi >&2

cd $TEXMF || exit

db_file=$TEXMF/ls-R
db_file_tmp=$TEXMF/ls-R-tmp$$

if test ! -w "$TEXMF" ; then
	echo "$progname: error: cannot write to $TEXMF."
	exit
fi >&2

echo -n "$progname: updating $db_file. " >&2
test -f $db_file || (touch $db_file; chmod 666 $db_file)

# copy the file to preserve as much of the permissions as possible.
cp -pf $db_file $db_file_tmp

# make sure, we can write the file:
chmod +w $db_file_tmp

# append_db depends on the next line. DO NOT CHANGE THE STRING.
echo '% This is file ls-R. Maintained by texhash and append_db.' > "$TEXMF/ls-R-tmp$$"

# the main task:
ls -LR "$TEXMF/" >> $db_file_tmp

echo "Done." >&2
mv -f $db_file_tmp $db_file
