#
# postinst-tex
#
# postinst snippets for registering hyphenation patterns, font maps, and formats
#
# 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!
#
# Let vim know that we don't want tabs
# vim:set expandtab: #

dhit_create_fontmaps ()
{
    tempfile=$(mktemp -p /tmp updmap.XXXXXXXX)
    printf "Running updmap-sys. This may take some time... "
    if updmap-sys > $tempfile 2>&1 ; then
        rm -f $tempfile
        echo "done."
    else
        exec >&2
        echo
        echo "updmap-sys failed. Output has been stored in"
        echo "$tempfile"
        echo "Please include this file if you report a bug."
	echo
	echo "Sometimes, not accepting conffile updates in /etc/texmf/updmap.d"
	echo "causes updmap-sys to fail.  Please check for files with extension"
	echo ".dpkg-dist or .ucf-dist in this directory" 
        echo
        exit 1
    fi
}


dhit_build_hyphen_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)
    # this is slow, and since we hope/assure that all ini files
    # are installed if the format is active, we don't need the
    # extra check fmtutil does when called with catcfg
    # X=$(fmtutil-sys --catcfg | grep "^$1[[:space:]]" || true)
    X=$(grep "^[[:space:]]*$1[[:space:]]" $fmtcnffile || true)
    if [ -n "$X" ] ; then
        dhit_build_format --byhyphen "$2"
    fi
}

dhit_build_format ()
{

    tempfile=$(mktemp -p /tmp fmtutil.XXXXXXXX)
    printf "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
        exec >&2
        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
    configure|abort-upgrade|abort-remove|abort-deconfigure)
        # All of the following needs an installed and configured
        # basic TeX system, so check this.
        if dhit_libkpathsea_configured; then
            for i in #WHATTODO# ; do
                if [ "$i" = map ] ; then
                    if which updmap-sys >/dev/null; then
                        dhit_create_fontmaps
                    fi
                fi
                if [ "$i" = format ] ; then
                    for fmt in #FORMATS# ; do
                        dhit_build_format --byfmt $fmt
                    done
                fi
                if [ "$i" = language ] ; then
                    dhit_build_hyphen_format_if_format_exists \
                        latex $(kpsewhich language.dat)
                fi
            done
        fi
    ;;

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