#! /bin/sh -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>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see /usr/doc/packaging-manual/
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

DIR=/tmp
tmpdir=$DIR/ibm-jdk
url=http://www.ibm.com/java/jdk/118/linux/
archive=ibm-jdk-l118-linux-x86.tgz
needfile=jdk118/include/java_lang_ThreadGroup.h
altfiles="java jre jar javakey appletviewer"
docdir=/usr/share/doc/ibm-jdk1.1
libdir=/usr/lib/ibm-java

unpack_tarball()
{
    rv=1
    while [ $rv -eq 1 ]; do
	rv=0
        if [ ! -e "$DIR/$archive" ]; then
	    echo "This package is an installer package, it does not actually"
	    echo "contain the IBM JDK. You will need to go download the IBM"
	    echo "JDK by hand. Please visit"
            echo ""
            echo "	$url"
            echo ""
            echo "now and download. The file should be owned by root,"
            echo "be named $archive and be copied to /tmp."
            rv=1
        else
            if [ ! -r "$DIR/$archive" ]; then
                echo "Error: $DIR/$archive is not readable."
                rv=1
            fi
        fi
	if [ $rv -eq 0 ]; then
	    og=`ls -l $DIR/$archive 2>/dev/null | { read x x o g x; echo $o.$g; }`
	    if [ "$og" != root.root ]; then
		echo "The file $DIR/$archive must be owned by root.root."
                rv=1
	    fi
	fi
	if [ $rv -eq 0 ]; then
            # Ok, we have an archive now. Let's try unpacking it.
	    rm -rf $libdir/tmp
	    mkdir -p $libdir/tmp
            cd $libdir/tmp
            tar zxfp $DIR/$archive
	    # there are two versions of the tarball. One without toplevel
	    # directory and one with jdk118
	    if [ -d $libdir/tmp/jdk118 ]; then
		rm -rf $libdir/jdk118
		mv $libdir/tmp/jdk118 $libdir/jdk118
		rm -rf $libdir/tmp
	    else
		mv $libdir/tmp $libdir/jdk118
	    fi
            if [ ! -e "$libdir/$needfile" ]; then
                echo "Error: $DIR/$archive does not contain a file"
                echo "named $libdir/$needfile ."
                echo "The file may be corrupt. Please try again to download it"
		echo "from $url."
                echo "If it still doesn't work, file a bug report on this package."
                rv=1
            fi
        fi
	if [ $rv -eq 1 ]; then
	    echo ""
	    echo -n "[Press RETURN to try again, 'no' + RETURN to abort] "
	    read foo
	    case "$foo" in [nN]|[nN][oO])
		echo "Abort installation of IBM JDK"
		exit 1
	    esac
	fi
    done
}

case "$1" in
    configure)
	last_configured_version=$2
	if dpkg --compare-versions "$last_configured_version" lt 1.1.8-1; then
	    unpack_tarball
	    unpacked=yes
	elif [ ! -f $libdir/$needfile ]; then
	    unpack_tarball
	    unpacked=yes
	fi

	# Actually install the files, but first, fix permissions.
        chown -R root.root $libdir
        chmod -R go=rX $libdir
        chmod -R u+rw $libdir

	if [ ! -d $docdir ]; then
	    mkdir $docdir
	fi
	ln -sf ../share/doc/ibm-jdk1.1 /usr/doc/ibm-jdk1.1
	for i in CHANGES.ADK README README.ADK; do
	    ln -sf ../../../lib/ibm-java/jdk118/$i $docdir/$i
	done
	ln -sf ../../../lib/ibm-java/jdk118/LICENSE $docdir/copyright
	ln -sf ../../../lib/ibm-java/jdk118/demo $docdir/examples

	cp -p $libdir/.java_wrapper $libdir/jdk118/bin/.java_wrapper
	cp -p $libdir/.jre_wrapper $libdir/jdk118/bin/jre

	if [ "$unpacked" = yes ]; then
	    echo "$DIR/$archive has been unpacked and installed."
	    echo "You can now delete it, if you wish."
	fi

	# update runtime alternatives
	for f in jre jre_g rmiregistry; do
	    update-alternatives --install \
		/usr/bin/$f $f $libdir/jdk118/bin/$f 118
	done
	# update alternatives
	for f in java jar javakey appletviewer \
	    java_g javac javac_g javah javah_g javaverify javaverify_g \
            javadoc javap native2ascii rmic serialver jdb
	do
	    update-alternatives --install \
		/usr/bin/$f $f $libdir/jdk118/bin/$f 118
	done
	;;

    abort-upgrade|abort-remove|abort-deconfigure)
	rm -rf /usr/share/doc/ibm-jdk1.1 /usr/doc/ibm-jdk1.1 $libdir/jdk118
	;;

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

#DEBHELPER#

exit 0
