#!/bin/sh

set -e

install-info --quiet --section teTeX teTeX --menuentry="Kpathsea" --description="File lookup along search paths." /usr/share/info/kpathsea.info
install-info --quiet --section teTeX teTeX --menuentry="Web2c" --description="TeX, Metafont and companion programs." /usr/share/info/web2c.info
install-info --quiet --section teTeX teTeX --menuentry="dvips" --description="Translating TeX DVI files to PostScript." /usr/share/info/dvips.info
install-info --quiet --section teTeX teTeX --menuentry="LaTeX2e" --description="LaTeX2e help 1.6." /usr/share/info/latex.info

PATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11
envvars="
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
"
for i in $envvars; do
    unset $i || true
done

#unset TETEXDIR || true
#unset TEXMF || true
#unset TEXINPUTS || true
TEXCONFIG_P=/usr/bin/texconfig
TEXMFC=/etc/texmf/texmf.cnf
umask 022

if [ -e $TEXMFC.dpkg-dist ] 
then
    if fgrep -q TEXMFMAIN $TEXMFC
    then
	if fgrep -q /usr/share/texmf $TEXMFC
	then
	    true;
	else
	    echo
	    echo You decided to keep the old $TEXMFC,
	    echo but since the location of the texmf tree changed,
	    echo I have to use the new one anyway.
	    echo Please merge your personal changes back into it.
	    echo The old $TEXMFC is in $TEXMFC.dpkg-old
	    echo
	    mv $TEXMFC $TEXMFC.dpkg-old
	    mv $TEXMFC.dpkg-dist $TEXMFC
	fi
    else
	echo
	echo You decided to keep the old tetex-0.4 $TEXMFC,
	echo but since the format of the file changed,
	echo I have to use the new one anyway.
	echo Please merge your personal changes back into it.
	echo The old $TEXMFC is in $TEXMFC.dpkg-old
	echo
	mv $TEXMFC $TEXMFC.dpkg-old
	mv $TEXMFC.dpkg-dist $TEXMFC
    fi
fi

if [ ! -L /usr/lib/texmf/web2c ]
then
  if [ -d /usr/lib/texmf/web2c ]
  then
    cp -a /usr/lib/texmf/web2c/* /var/lib/texmf/web2c || true
    rm -fr /usr/lib/texmf/web2c
  fi
fi
ln -sf /var/lib/texmf/web2c /usr/share/texmf/

if [ x"$1" = xconfigure ]
then
    if [ -e $TEXCONFIG_P -a -e /usr/share/texmf/dvips/config ]
    then
#    $TEXCONFIG_P font options appendonlydir varfonts
	/usr/bin/mktexlsr
	$TEXCONFIG_P font vardir /var/spool/texmf
	TEMPFILE=`tempfile -p tex`
	echo "Running initex. This may take some time. ..."
	$TEXCONFIG_P init > $TEMPFILE
	echo "Output of initex is in $TEMPFILE"
	echo
    fi
fi
echo Fixing permissions of ls-R files ...
chmod -v 644 /var/lib/texmf/ls-R /var/lib/texmf/ls-R.local /var/spool/texmf/ls-R 2>/dev/null | fgrep changed || true

# register xdvi for mime
if [ -x /usr/sbin/update-mime ]; then
    update-mime
fi

if [ -x /usr/sbin/update-xaw-wrappers ]; then
    update-xaw-wrappers
fi

update-alternatives --install /usr/X11R6/bin/xdvi.bin xdvi.bin /usr/X11R6/bin/xdvi.real 30
update-alternatives --install /usr/X11R6/bin/oxdvi.bin oxdvi.bin /usr/X11R6/bin/oxdvi.real 30

echo
echo If you want to change the default settings,
echo use $TEXCONFIG_P to configure teTeX.
echo 

# Automatically added by dh_installdocs
if [ "$1" = "configure" ]; then
	if [ -d /usr/doc -a ! -e /usr/doc/tetex-bin -a -d /usr/share/doc/tetex-bin ]; then
		ln -sf ../share/doc/tetex-bin /usr/doc/tetex-bin
	fi
fi
# End automatically added section

