#!/bin/bash
#
# Utility to update Gentoo teTeX distribution configuration files
#

PATH=/bin:/usr/bin

# Fix for all those with altered umask for root
umask 022

# Make sure we have a correct environment, bug #30432
# The list of env. vars is taken from the INSTALL file
for texvar in AFMFONTS BIBINPUTS BSTINPUTS DVILJFONTS DVIPSFONTS \
        DVIPSHEADERS GFFONTS GLYPHFONTS INDEXSTYLE MFBASES MFINPUTS \
        MFPOOL MFTINPUTS MPINPUTS MPMEMS MPPOOL MPSUPPORT OCPINPUTS \
        OFMFONTS OPLFONTS OTPINPUTS OVFFONTS OVPFONTS PKFONTS PSHEADERS \
        T1FONTS T1INPUTS TEXBIB TEXCONFIG TEXDOCS TEXFONTMAPS TEXFONTS \
        TEXFORMATS TEXINDEXSTYLE TEXINPUTS TEXMFCNF TEXMFDBS TEXMFINI \
        TEXPICTS TEXPKS TEXPOOL TEXPSHEADERS TEXSOURCES TFMFONTS TRFONTS \
        VFFONTS XDVIFONTS XDVIVFS ; do

        if [ "${!texvar}" ]; then
                if ! $(echo ${!texvar} | grep '^:\|::\|:$' &>/dev/null) ; then
                        export ${texvar}="${!texvar}:"
                fi
        fi
done

if [ "$TEXINPUTS" ]; then
        if $(echo ${TEXINPUTS} | grep '/usr/share/texmf' &>/dev/null) ; then
                export TEXINPUTS=$(echo ${TEXINPUTS} | sed -e 's|/usr/share/texmf/*:\?||g')
        elif $(echo ${TEXINPUTS} | grep '/var/lib/texmf' &>/dev/null) ; then
                export TEXINPUTS=$(echo ${TEXINPUTS} | sed -e 's|/var/lib/texmf/*:\?||g')
        fi
fi

for conf in texmf.cnf fmtutil.cnf updmap.cfg
do
        if [ -d "/etc/texmf/${conf/.*/.d}" ]
        then
                echo "Generating /etc/texmf/web2c/${conf} from /etc/texmf/${conf/.*/.d} ..."
                cat /etc/texmf/${conf/.*/.d}/* > "/etc/texmf/web2c/${conf}"
        fi
done

# configure
echo "Configuring teTeX ..."
mktexlsr &>/dev/null
texconfig-sys init &>/dev/null
texconfig-sys confall &>/dev/null
texconfig-sys font rw &>/dev/null
texconfig-sys font vardir /var/cache/fonts &>/dev/null
updmap-sys &>/dev/null

# generate
echo "Generating format files ..."
fmtutil-sys --missing &>/dev/null
echo
echo "Use 'texconfig font ro' to disable font generation for users"
echo
