#
# postrm-tex
#
# postinst snippets for installing stuff for TeX
#
# Authors:
#	Florent Rougon <f.rougon@free.fr>
#	Norbert Preining <preining@logic.at>
#
# Please note that comments in this file are stripped before installation
# by calling a grep -v '^[ \t]*#'
# So please be careful if you add stuff here!

check_run_without_errors ()
{
    tempfile=`mktemp -p /tmp checkrun.XXXXXXXX`
    if which $1  >/dev/null; then
        echo -n "Running $*. This may take some time. ..."
	set +e
	if $* > $tempfile 2>&1 ; then
	    rm -f $tempfile
	    echo " done."
	else
	    echo
	    echo "$* failed. Output has been stored in"
	    echo "$tempfile"
	    echo "If tex-common is not configured you can ignore this error message!"
	    echo "Otherwise please include this file if you report a bug."
	    echo
	fi
	set -e
    else
        rm -f $tempfile
    fi
    return 0
}

build_format_if_format_exists ()
{
    v=`kpsewhich -var-value TEXMFSYSVAR`
    c=`kpsewhich -var-value TEXMFSYSCONFIG`
    TEXMFVAR="$v"
    TEXMFCONFIG="$c"
    export TEXMFVAR TEXMFCONFIG
    fmtcnffile=`kpsewhich --format='web2c files' fmtutil.cnf`
    X=`cat $fmtcnffile | grep "^$2[[:space:]]" || true`
    if [ ! "X$X" = "X" ] ; then
        if [ "X$1" = "X--byhyphen" ] ; then
            build_format --byhyphen $3
        else
            build_format $1 $2
        fi
    fi
}

build_format ()
{

    tempfile=`mktemp -p /tmp fmtutil.XXXXXXXX`
    echo -n "Building format(s) $1 `basename "$2"`. This may take some time. ..."
    if fmtutil-sys $1 $2 > $tempfile 2>&1 ; then
        rm -f $tempfile
        echo " done."
    else
        echo
        echo "fmtutil-sys failed. Output has been stored in"
        echo "$tempfile"
        echo "Please include this file if you report a bug."
        echo
        exit 1
    fi
}



case "$1" in
    remove|disappear)
    	# it doesn't hurt to cal all of them
        check_run_without_errors update-updmap --quiet
	check_run_without_errors update-language
	check_run_without_errors update-fmtutil
	check_run_without_errors mktexlsr
	for i in #WHATTODO# ; do
		if [ $i = map ] ; then
			check_run_without_errors updmap-sys
		fi
		if [ $i = format ] ; then
			for fmt in #FORMATS# ; do
				rm -f /var/lib/texmf/web2c/$fmt.*
			done
		fi
		if [ $i = language ] ; then
			if which fmtutil-sys >/dev/null ; then
				build_format_if_format_exists --byhyphen latex `kpsewhich language.dat`
			fi
		fi
	done
    ;;

    purge|upgrade|failed-upgrade|abort-upgrade|abort-install)
    ;;

    *)
        echo "postrm called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac


