#!/bin/sh
# postinst script for im-config
# vim: set sts=4 expandtab:

set -e

# version just before wheezy release
# acb685ae9264be3fc1800f98a70b12bb  80im-switch 1.14
# 044a2f13aa8382902dc8f47dc7da7064  80im-switch 1.16 (oldstable) - 1.22 (unstable)
# 5b34831bb3c203ced71b7efb6da4609e  80im-switch 1.23 (testing/unstable)

# 4045a8eeb0e9226cdd9f8a121ccf4c04  80im-config_launch 0.3 (stable initial)
# 8a4829f935b5561ca6e61bec6eb3893f  80im-config_launch 0.3+squeeze1 (stable), 0.4
# b243876a16f7b87c397ceef626098069  80im-config_launch 0.5
# 5ef27261ac38d704d8f766da19635461  80im-config_launch 0.6-0.17 (testing, unstable)

case "$1" in
    configure)
        IM_SWITCH_HOOK=/etc/X11/Xsession.d/80im-switch
        IM_SWITCH_DISABLE_CODE='[ -x /usr/bin/im-switch ] || return 0'
        if [ -f $IM_SWITCH_HOOK ]; then
            IM_SWITCH_HOOK_MD5SUM=$(md5sum < $IM_SWITCH_HOOK|cut -d ' ' -f 1)
            case $IM_SWITCH_HOOK_MD5SUM in
                acb685ae9264be3fc1800f98a70b12bb|044a2f13aa8382902dc8f47dc7da7064|5b34831bb3c203ced71b7efb6da4609e)
                    rm -f $IM_SWITCH_HOOK
                ;;
    
                *)
                    if [ "$( head -1 $IM_SWITCH_HOOK )" != "$IM_SWITCH_DISABLE_CODE" ] &&
                      [ "$( sed -n 15p $IM_SWITCH_HOOK )" != "$IM_SWITCH_DISABLE_CODE" ]; then
                        echo "Migrating from im-switch to im-config. Disabling: $IM_SWITCH_HOOK." >&2
                        sed -i "1 i $IM_SWITCH_DISABLE_CODE" $IM_SWITCH_HOOK
                    fi
                ;;
            esac
        fi
        IM_CONFIG_HOOK=/etc/X11/Xsession.d/80im-config_launch
        if [ -f $IM_CONFIG_HOOK ]; then
            IM_CONFIG_HOOK_MD5SUM=$(md5sum < $IM_CONFIG_HOOK|cut -d ' ' -f 1)
            case $IM_CONFIG_HOOK_MD5SUM in
                4045a8eeb0e9226cdd9f8a121ccf4c04|8a4829f935b5561ca6e61bec6eb3893f|b243876a16f7b87c397ceef626098069|5ef27261ac38d704d8f766da19635461)
                    rm -f $IM_CONFIG_HOOK
                ;;
    
                *)
                    echo "ERROR: Unknown hook file exists: $IM_CONFIG_HOOK." >&2
                ;;
            esac
        fi
    ;;

    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

