#!/bin/sh -e

PATH=/sbin:/bin:/usr/sbin:/usr/bin

multiarch=@multiarch@
priority=@priority@
browser_dirs="@browser_dirs@"
PLUGIN=@plugin_name@
basedir=/@basedir@
old_basedir=/usr/lib/jvm/java-6/openjdk
PLUGINPTH=$basedir/jre/lib/@archdir@/$PLUGIN
OLD_PLUGINPTH=$old_basedir/jre/lib/@archdir@/$PLUGIN
NPPLUGINPTH=$basedir/jre/lib/@archdir@/IcedTeaNPPlugin.so
LAST_NPP_VERSION=6b18~pre2-2

case "$1" in
    configure)
	# IcedTeaPlugin -> IcedTeaNPPlugin update
	plugin_update=
	if [ -n "$LAST_NPP_VERSION" ] && [ "$1" = configure ] && [ -n "$2" ]; then
	    plugin_update=$(dpkg --compare-versions "$2" lt "$LAST_NPP_VERSION" && echo yes || echo no)
	fi

	for browser_dir in $browser_dirs; do
	    if [ $browser_dir = xulrunner-addons ]; then
		browser=xulrunner-1.9
	    else
		browser=$browser_dir
	    fi

	    if [ "$plugin_update" = yes ]; then
		update-alternatives --quiet --remove \
		    $browser-javaplugin.so \
		    $NPPLUGINPTH || true
	    fi

	    if [ -n "$multiarch" ] && [ "$DPKG_MAINTSCRIPT_ARCH" != $(dpkg --print-architecture) ]; then
		priority=$(expr $priority - 1)
	    fi

            if [ -n "$multiarch" ] && [ -n "$2" ]; then
                if [ -n "$(update-alternatives --list $browser-javaplugin.so 2>/dev/null | grep ^$old_basedir/)" ]; then
		    update-alternatives --remove $browser-javaplugin.so $OLD_PLUGINPTH || true
                fi
	    fi

            if [ -z "$(update-alternatives --list $browser-javaplugin.so 2>/dev/null | grep ^$basedir/)" ]; then
		update-alternatives --quiet --install \
		    /usr/lib/$browser_dir/plugins/libjavaplugin.so \
		    $browser-javaplugin.so \
		    $PLUGINPTH \
		    $priority
	    fi
	done
esac

#DEBHELPER#

exit 0
