#! /bin/sh

set -e

multiarch=@multiarch@
priority=@priority@
basedir=/@basedir@
mandir=$basedir/jre/man
priority7=@priority7@
base7dir=/@base7dir@
man7dir=$base7dir/jre/man
priority8=@priority8@
base8dir=/@base8dir@
man8dir=$base8dir/jre/man
tools='javaws itweb-settings'
srcext=1.gz
dstext=1.gz

case "$1" in
configure)
   if [ -z "$2" ]; then
        update_alternatives=y
    fi
    if [ -n "$multiarch" ] && [ -n "$2" ]; then
        for i in $tools; do
            if [ -z "$(update-alternatives --list $i 2>/dev/null | grep ^$basedir/)" ]; then
                update_alternatives=y
                break
            fi
        done
    fi
    if [ "$update_alternatives" != y ] && [ $priority -gt 1060 ]; then
        for i in $tools; do
            oldp=$(update-alternatives --query java | awk -v b=$basedir '/^Alternative:/ && $2~b {p=1} /^Priority:/ && p {print $2; exit}')
            if [ -n "$oldp" ] && [ "$oldp" -le 1060 ]; then
                update_alternatives=y
                break
            fi
        done
    fi

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

    for i in $tools; do
      if [ -f $basedir/jre/bin/$i ]; then   
       # setup java6 in update-alternatives
       unset slave1 || true
       if [ -e $mandir/man1/$i.$srcext ]; then
          slave1="--slave \
          /usr/share/man/man1/$i.$dstext \
    	        $i.$dstext \
       	        $mandir/man1/$i.$srcext"
       	fi
       	update-alternatives \
       	    --install \
       	    /usr/bin/$i \
       	    $i \
       	    $basedir/jre/bin/$i \
       	    $priority \
       	    $slave1
      fi
      if [ -f $base7dir/jre/bin/$i ]; then
        # setup java7 in update-alternatives    
        unset slave1 || true
        if [ -e $man7dir/man1/$i.$srcext ]; then
            slave1="--slave \
                /usr/share/man/man1/$i.$dstext \
                $i.$dstext \
                $man7dir/man1/$i.$srcext"
        fi
        update-alternatives \
            --install \
            /usr/bin/$i \
            $i \
            $base7dir/jre/bin/$i \
            $priority7 \
            $slave1

	# 6's priority is higher, but 6 is not installed, set it to 7
	if [ $priority -gt $priority7 ] && [ ! -f $basedir/jre/bin/$i ]; then
	    update-alternatives --set $i $base7dir/jre/bin/$i
	fi

	# 7's priority is higher, but 7 is not installed, set it to 6
	if [ $priority7 -gt $priority ] && [ ! -f $base7dir/jre/bin/$i ]; then
	    update-alternatives --set $i $basedir/jre/bin/$i
	fi
      fi
      if [ -f $base8dir/jre/bin/$i ]; then
        # setup java8 in update-alternatives
        unset slave1 || true
        if [ -e $man8dir/man1/$i.$srcext ]; then
            slave1="--slave \
                /usr/share/man/man1/$i.$dstext \
                $i.$dstext \
                $man8dir/man1/$i.$srcext"
        fi
        update-alternatives \
            --install \
            /usr/bin/$i \
            $i \
            $base8dir/jre/bin/$i \
            $priority8 \
            $slave1

	# 7's priority is higher, but 7 is not installed, set it to 8
	if [ $priority7 -gt $priority8 ] && [ ! -f $base7dir/jre/bin/$i ]; then
	    update-alternatives --set $i $base8dir/jre/bin/$i
	fi

	# 8's priority is higher, but 8 is not installed, set it to 7
	if [ $priority8 -gt $priority7 ] && [ ! -f $base8dir/jre/bin/$i ]; then
	    update-alternatives --set $i $base7dir/jre/bin/$i
	fi
      fi
    done
    fi
esac

#DEBHELPER#
