#!/bin/sh
# postinst script for debathena-gconf2-config
#
# 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

unmodified_path_files="
42fc7033476d0ef67c6128de8ba37743
5fe9d96b799feb9265cdba9e91d5fc87
886ab9a6f0d1454dbbc7174c98f82541
c04694fa5852f2ccdb66d477e2166b30
c6707181dfe36c2df1236cf59d19eb40
d63e8f10e2be329d4c7a8300cf9adb24
dfaf90cd2b9295854aac7dafc43d3ef1
e26c499b34306eb817ffff97dfaa22fc
e85c1fff42b26376b545370be5fb0a99
f4675a2f84af10bb8d2b04305b235dff
"

# Figure out if we think that /etc/gconf/2/path has been intentionally
# modified by the user
#
# There are two types of conflicts:
#  1: User modified /etc/gconf/2/path.debathena, but left the path ->
#     path.debathena symlink in place
#  2: User changed the /etc/gconf/2/path symlink
#
# We have to figure out what's going on before undoing the diversion
# (which will happen in the debhelper block). But we can't actually do
# anything about it until afterwards, because of ucf's broken symlink
# handling.
conflict=0
path=/etc/gconf/2/path
if [ "$1" = "configure" ] && \
    dpkg --compare-versions "$2" lt-nl 1.7~; then
    if [ "$(readlink -n "$path")" = "path.debathena" ]; then
        if [ -r $path.debathena ] && \
            echo "$unmodified_path_files" | \
            grep -Fxq "$(md5sum "$path.debathena" | awk '{ print $1 }')"; then
            # Definitely pristine
            conflict=0
        else
            # User modified /etc/gconf/2/path.debathena
            conflict=1
        fi
    else
        # User changed /etc/gconf/2/path symlink
        conflict=2
    fi
fi

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

#DEBHELPER#

case "$1" in
    configure)
        if dpkg --compare-versions "$2" lt-nl 1.7~; then
            if [ "$conflict" = 0 ]; then
                ucf --purge /etc/gconf/2/path
                rm -f /etc/gconf/2/path
            else
                # Force a ucf conflict
                sed -i -re 's,^[0-9a-f]{32}  (/etc/gconf/2/path)$,00000000000000000000000000000000  \1,' /var/lib/ucf/hashfile

                # If /etc/gconf/2/path was really path.debathena
                # before, make sure it still is when we run ucf next
                if [ "$conflict" = 1 ]; then
                    if [ -r /etc/gconf/2/path.debathena ]; then
                        mv /etc/gconf/2/path.debathena /etc/gconf/2/path || :
                    else
                        mv /etc/gconf/2/path /etc/gconf/2/path.old || :
                    fi
                fi
            fi
        fi
        ucf /usr/share/gconf/default.path /etc/gconf/2/path
    ;;

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

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

exit 0


