#!/bin/sh
# postinst script for thailatex
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

SOURCE_TEXMF=/usr/share/texlive/texmf-dist
TARGET_TEXMF=/var/lib/texmf
BABELSTY_LOC=tex/generic/babel/babel.sty

SOURCE_BABELSTY="${SOURCE_TEXMF}/${BABELSTY_LOC}"
TARGET_BABELSTY="${TARGET_TEXMF}/${BABELSTY_LOC}"

patch_babel_sty() {
    echo -n "Customizing babel.sty..."

    if [ ! -f ${SOURCE_BABELSTY} ]; then
        printf "failed: system babel.sty does not exists.\n"
        printf "Warning: you will not be able to use [thai] option directly in \\documentclass.\n"
        return
    fi

    if [ -f ${TARGET_BABELSTY} ]; then
        sed -i.dpkg-old '/thai.ldf/d' ${TARGET_BABELSTY}
    else
        mkdir -p `dirname ${TARGET_BABELSTY}`
        cp ${SOURCE_BABELSTY} ${TARGET_BABELSTY}
    fi

    sed -i '/turkish/i\
\\DeclareOption{thai}{\\input{thai.ldf}}' ${TARGET_BABELSTY}

    echo "done."
}

case "$1" in
    configure|triggered)
        patch_babel_sty
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

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

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0


