#
# This script will install the actual plugin, advise the user to read the
# license for Oracle JRE and to re-login to have environment variables set
# properly.
#

BUILD_DIR="./var/tmp/${PKGNAME}-${VERSION}.build"

do_install() {
	mkdir -p "$BUILD_DIR"
	wget \
		--no-check-certificate \
		--header "Cookie: oraclelicense=accept-securebackup-cookie" \
		--progress=bar:force \
		-c -O $BUILD_DIR/jre.tar.gz \
		$ORACLE_JRE_URL
	echo "$ORACLE_JRE_CHECKSUM  $BUILD_DIR/jre.tar.gz" | sha256sum -c || return 1
	tar xf ${BUILD_DIR}/jre.tar.gz || return 1

	if [ -d usr/lib/jvm/jre$ORACLE_JRE_LONG_VERSION ]; then
		rm -rf usr/lib/jvm/jre$ORACLE_JRE_LONG_VERSION
	fi
	mv jre$ORACLE_JRE_LONG_VERSION usr/lib/jvm/jre$ORACLE_JRE_LONG_VERSION || return 1

	# To set the java alternative to 'oracle-jre'
	xbps-alternatives -s oracle-jre
}

case "${ACTION}" in
post)
	. usr/lib/jvm/oracle-jre-vars.sh
	do_install
	RESULT=$?
	rm -r $BUILD_DIR
	[ $RESULT -ne 0 ] && exit $RESULT
    	;;
esac

