#!/bin/dash
# getjava
# jamesbond 2015
# MIT license

WORKDIR=/tmp/getjava/work.$$
#COMP_MODE="-comp lzo -Xcompression-level 1"
COMP_MODE=${COMP_MODE:-"-comp xz -Xbcj x86"}
BUILD_CHECK=${BUILD_CHECK:-40}
ORACLE_MAGIC_URL=${ORACLE_MAGIC_URL:-"http://distro.ibiblio.org/fatdog/web/java_oracle_magic"}
#ORACLE_MAGIC=e9e7ea248e2c4826b92b3f075a80e441 #downloaded from above

setarch() {
	case $(uname -m) in
		x86_64)
			ARCH=x64
			LIBDIR=lib64 ;;
		i?86)
			ARCH=i586
			LIBDIR=lib ;;
		*) msg Unsupported architecture.
		   exit 1 ;;
	esac
}

# $@ - text
msg() {
	echo "$@"
}

get_latest_version() {
	msg "Finding latest available version ... "
	#LATEST=1.8.0_31
	
	# this is not always accurate - sometimes the version in this file lags behind the real one
	#LATEST=$(wget -qO - http://java.com/applet/JreCurrentVersion2.txt | tr -d '\r\n') # 1.8.0_31
	#UPDATE=${LATEST#*_}
	#JDKVER=${LATEST%.*}; JDKVER=${JDKVER#1.}
	#VERSION=${JDKVER}u${UPDATE}
	
	# this from SFR is more accurate
	VERSION="$(wget -qO - https://www.java.com/en/download/linux_manual.jsp | grep -Eo '[0-9]+ Update [0-9]+' | sed 's/ Update /u/')"
	msg Latest version is $VERSION
	ORACLE_MAGIC=$(wget -qO - "$ORACLE_MAGIC_URL" | sed "/$VERSION/!d;s/.*://")
	if [ -z $ORACLE_MAGIC ]; then
		msg No magic code has been provided for $VERSION yet. Contact Fatdog team.
		exit 1
	fi
}

# $1-version
get_build() {
	msg "Finding latest build ... "
	# wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie"
	# http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-linux-x64.tar.gz
	BUILD=""
	for p in $(seq $BUILD_CHECK -1 1); do
		p=$(printf "%0.2d" $p)
		msg "Check whether build $p is available ... "
		wget -qS --spider --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" \
		"http://download.oracle.com/otn-pub/java/jdk/$VERSION-b$p/$ORACLE_MAGIC/jdk-$VERSION-linux-$ARCH.tar.gz" 2>&1 |
		grep -q "HTTP.*200 OK" && BUILD=$p && break
	done
	[ $BUILD ] && msg yes, will use this build $BUILD. ||
	{ msg Cannot find build number, exiting. ; exit 1; }
}

# $1-version
download() {
	msg "Downloading Java $VERSION build $BUILD ..."
	wget -c --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" \
	"http://download.oracle.com/otn-pub/java/jdk/$VERSION-b$BUILD/$ORACLE_MAGIC/jdk-$VERSION-linux-$ARCH.tar.gz"
}

makesfs() {
	cd $WORKDIR &&
	mkdir -p workroot &&
	(cd workroot; mkdir -p opt usr/bin usr/$LIBDIR/mozilla/plugins; ) &&
	
	tar -xf jdk-$VERSION-linux-$ARCH.tar.gz -C workroot &&
	mv workroot/jdk* workroot/opt/java-$VERSION &&
	
	ln -sf java-$VERSION workroot/opt/java &&
	ln -sf /opt/java/bin/java workroot/usr/bin/java &&
	ln -sf /opt/java/jre/lib/amd64/libnpjp2.so workroot/usr/$LIBDIR/mozilla/plugins/libnpjp2.so &&
	
	mksquashfs workroot $HOME/java-jdk-$VERSION-x86_64.sfs -noappend $COMP_MODE &&
	rm -r workroot
}

trap 'rm -r $WORKDIR; exit' INT HUP 0

### main
setarch
mkdir -p $WORKDIR; cd $WORKDIR
get_latest_version
get_build
download
makesfs
msg done
