#!/bin/dash
# getjava rev. [1]
# ^ keep that line as is for the updater, only increase the revision number
#
# jamesbond 2015, 2019, 2021
# JakeSFR 2018, 2019, 2020, 2021
# MIT license

# We support only the latest version
# Previous, EOL versions are not available without an account @Oracle

MYPATH="$(readlink -f "$0")"
WORKDIR="/tmp/getjava.$$"
JDK_URL="https://www.oracle.com/java/technologies/javase-downloads.html"
UPDATE_URL='http://distro.ibiblio.org/fatdog/other/getjava.sh'

#COMP_MODE="-comp lzo -Xcompression-level 1"
COMP_MODE=${COMP_MODE:-"-comp xz -Xbcj x86"}

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

check_root() {
	if [ $(id -u) -ne 0 ]; then
		echo "Must run as root." && exec su -c "$MYPATH $@"
	fi
}

check_url() {
	if [ -z "$1" ]; then
		msg "Could not get JDK download URL."
		msg "You may want to update this script by running:"
		msg "${0##*/} --update"
		return 1
	fi
}

# Update itself
update_self() {
	local oldrev newrev tmpdl='getjava.sh.tmp'

	echo "Updating..."

	if curl -f -R -k -s "$UPDATE_URL" -o "$tmpdl"; then
		# get revisions
		oldrev="$(sed -n 's/# getjava.*\[\([0-9]\+\)\].*/\1/p;' "$MYPATH" | head -n 1)"
		newrev="$(sed -n 's/# getjava.*\[\([0-9]\+\)\].*/\1/p;' "$tmpdl" | head -n 1)"

		if [ "$oldrev" -a "$newrev" ] && [ $newrev -gt $oldrev ]; then
			mv "$tmpdl" "$MYPATH"
			chmod +x "$MYPATH"
			echo "Successfully updated to revision [${newrev}]."
		else
			echo "The latest revision [${oldrev}] is already installed."
		fi

	else
		echo "Update failed!"
	fi
}

# Based on https://gist.github.com/n0ts/40dd9bd45578556f93e7
download() {
	msg "Searching for the latest version of JDK ..."

	check_url "$JDK_URL" || exit 1

	DL_URL=$(
		curl -L -s $JDK_URL | \
		sed -n "s#.*\(https://.*linux-x64_bin.tar.gz\).*#\1#p" | \
		head -n 1	# just in case
	)

	check_url "$DL_URL" || exit 1

	# used in makesfs()
	FULL_VERSION=$(echo ${DL_URL##*/} | cut -f2 -d '-' | cut -f1 -d '_')

	msg "Downloading ${DL_URL##*/} ..."

	#curl -C - -LR#OH "Cookie: oraclelicense=accept-securebackup-cookie" -k $DL_URL
	curl -C - -LR#O --cookie "oraclelicense=accept-securebackup-cookie" -k "$DL_URL" || exit 1
}

makesfs() {
	msg "Converting to SFS ..."

	cd $WORKDIR &&
	mkdir -p workroot && cd workroot &&
	tar -xf ../jdk* && rm -f ../jdk* &&

	mkdir -p ./opt ./usr/bin &&
	mv ./jdk* ./opt/java-$FULL_VERSION &&
	ln -sf java-$FULL_VERSION ./opt/java &&
	
	# create a wrapper that extends LD_LIBRARY_PATH (req. for JDK 15+)
	cat << "EOF" > ./usr/bin/jdk_wrapper
#!/bin/sh

LD_LIBRARY_PATH="/opt/java/lib:$LD_LIBRARY_PATH"	# the order matters!
exec /opt/java/bin/${0##*/} "$@"
EOF
	chmod +x ./usr/bin/jdk_wrapper
	
	# create symlinks to a wrapper
	for p in jar jarsigner java javac javadoc javah javap javaws jconsole jvisualvm policytool keytool; do
		[ -e ./opt/java/bin/$p ] && ln -sf jdk_wrapper ./usr/bin/$p
	done && 
	
	cd .. &&
	mksquashfs workroot $HOME/java-jdk-$FULL_VERSION-x64.sfs -noappend $COMP_MODE &&
	rm -rf workroot

}

confirm() {
	local p
	if test -t 0; then
		cat << EOF
This script will download the latest version of Java to turn it into an SFS.
Press [Enter] to continue, or Ctrl-C to cancel.
EOF
		read p
	fi
}

doit() {
	download
	makesfs
	msg "Done - SFS saved to $HOME"
}


### main
check_root "$@"

trap '[ -e $WORKDIR ] && cd $WORKDIR && cd .. && rm -r $WORKDIR; exit' INT HUP 0
mkdir -p $WORKDIR; cd $WORKDIR

case "$1" in
	"")
		confirm
		doit
		;;
		
	--noprompt)
		doit
		;;
		
	-U|--update)
		update_self
		;;

	-h|--help|*)
		cat << EOF
${0##/*} [--noprompt] [-U|--update]

A simple script to download the latest version of Java and turn it
into an SFS.

Running with no parameters will ask for your confirmation.
--noprompt will bypass the confirmation.
Note that if you don't run in a terminal (e.g you launch the script
from GUI), it will not ask for confirm and run as if --nopropmt is
specified.

-U or --update to update the script with the latest version.

EOF
		;;
esac
