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

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

#DEBHELPER#

case "$1" in
    configure)
	# In 0.9~rc2-0ubuntu2, the conffile moved to /etc/xul-ext
	# and we horribly broke things.  Let's attempt to unbreak them:
	if dpkg-query -W ubufox 2>/dev/null 2>&1 && dpkg --compare-versions "$(dpkg-query -W -f '${Version}' ubufox)" ge "0.9~rc2-0ubuntu2" && [ -d /etc/firefox-3.0/pref ]; then
	    # The file should have been undiverted at this point
	    echo "*** The previous OMINOUS WARNING may be safely ignored ***" >&2
	    # If there are files in the location, move them to
	    # the new location, unless the user has already done that
	    # and that will restore any broken symlinks.
	    for f in $(ls /etc/firefox-3.0/pref/ubufox.js.debathena*); do
		mv -u "$f" /etc/xul-ext
	    done
	    new_file=/etc/xul-ext/ubufox.js
	    if [ -L "$new_file" ] && 
	       [ ! -e "$(readlink -f "$new_file")" ]; then
		# If the link target is still missing, copy over upstream's
		# new config file, or upstream's old config file, or give up
		if [ -f /etc/xul-ext/ubufox.js.dpkg-new ]; then
		    mv -f /etc/xul-ext/ubufox.js.dpkg-new /etc/xul-ext/ubufox.js
		elif [ -f /etc/xul-ext/ubufox.js.debathena-orig ]; then
		    mv -f /etc/xul-ext/ubufox.js.debathena-orig /etc/xul-ext/ubufox.js
		else
		    echo "*** Manual inspection and cleanup of /etc/xul-ext/ubufox.js may be required"
		fi
	    fi
	fi
	if [ -d /usr/lib/firefox/extensions ] && ! [ -h /usr/lib/firefox/extensions ] && ! [ -e /usr/lib/firefox/extensions/athena@mit.edu ]; then
	    ln -s /usr/lib/debathena-firefox-extension /usr/lib/firefox/extensions/athena@mit.edu
	fi
    ;;

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

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

exit 0
