#!/bin/sh -e

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

multiarch=@multiarch@
priority=@priority7@
browser_dirs="@browser_dirs@"
PLUGIN=@plugin_name@
basedir=/@base7dir@
old_basedir=/usr/lib/jvm/java-7/openjdk
PLUGINPTH=$basedir/jre/lib/@archdir@/$PLUGIN
OLD_PLUGINPTH=$old_basedir/jre/lib/@archdir@/$PLUGIN

case "$1" in
    configure)
	for browser_dir in $browser_dirs; do
	    if [ $browser_dir = xulrunner-addons ]; then
		browser=xulrunner-1.9
	    else
		browser=$browser_dir
	    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
	    elif [ $priority -gt 1060 ]; then
		oldp=$(update-alternatives --query $browser-javaplugin.so | awk -v b=$basedir '/^Alternative:/ && $2~b {p=1} /^Priority:/ && p {print $2; exit}')
		if [ -n "$oldp" ] && [ "$oldp" -le 1060 ]; then
		  update-alternatives --quiet --install \
		    /usr/lib/$browser_dir/plugins/libjavaplugin.so \
		    $browser-javaplugin.so \
		    $PLUGINPTH \
		    $priority
		fi
	    fi
	done
esac

#DEBHELPER#

exit 0
