#!/bin/sh

# selfautofix: change absolute paths in texmf.cnf to $SELFAUTO references.
# Copyright: 1998, Thomas Esser. Public domain.

bindir=$1
web2c=$2

test -f "$web2c"/texmf.cnf || exit 0
grep 'original texmf.cnf --' "$web2c"/texmf.cnf >/dev/null 2>&1 || exit 0

bindirp=`echo $bindir | sed 's%/[^/][^/]*$%%'`
bindirpp=`echo $bindirp | sed 's%/[^/][^/]*$%%'`

cd $web2c || exit 0

sedfile=sed.$$
test -n "$bindir" \
  && echo "/^[ 	]*TEXMFCNF[ =]/!s%$bindir/%\$SELFAUTOLOC/%g" > $sedfile
test -n "$bindirp" \
  && echo "/^[ 	]*TEXMFCNF[ =]/!s%$bindirp/%\$SELFAUTODIR/%g" >> $sedfile
test -n "$bindirpp" \
  && echo "/^[ 	]*TEXMFCNF[ =]/!s%$bindirpp/%\$SELFAUTOPARENT/%g" >> $sedfile

cat >>$sedfile <<eof
/^%/!s%:%;%g
/^[ 	]*TEXMFCNF[ =]/s%;%:%g
eof

sed  -f $sedfile texmf.cnf >texmf.cnf.$$
test -s texmf.cnf.$$  && cat texmf.cnf.$$ > texmf.cnf
rm -f texmf.cnf.$$ $sedfile

exit 0
