#!/bin/bash

# posix-compliant way to source script
. ./jdkversion-settings.sh
# By sourcing jdkversion-settings, the following variables now exist
# - majorversion (JDK major version, likely 11 for GS and 17 for Expeditee)
# - fullversion (full version string of JDK)
# - bitness (32 or 64)

selfcon_ext=selfcontained-jdk
filesuffix=_x64

# lowercase OS name
thisOS=`uname -s`
thisOS=$(echo "$thisOS" | tr '[:upper:]' '[:lower:]')

#Bitness is always 64 on mac (darwin)
#bitness=`getconf LONG_BIT` # returns 32 or 64
if [ "$bitness" = "32" ]; then
    filesuffix=_i686
    #selfcon_ext=$selfcon_ext${bitness}
    echo "@@@ Need to get the 32 bit JDK"
fi

osfname=$thisOS
if [ "$thisOS" = "darwin" ] ; then
    osfname=macosx
fi
filename=zulu${fullversion}-${osfname}${filesuffix}


# Check if there's already a java tarball of the requested version.
# If not, check if it's on trac to get it from there
# If not on trac, get it from zulu

# This script will not svn add or svn commit any new JDK binary downloaded from zulu
# as this script merely allows a local download to test a new JDK binary.
# svn adding and committing should be done for all OS' JDK binaries together with
# the new script in cli-extensions top-level (download-java-all-os.sh).

# If the requested version exists locally, we're done
# If not, check if the requested version exists on trac otherwise try azure
if [ ! -f ${filename}.tar.gz ] ; then
    java_url=/main/trunk/cli-extensions/${selfcon_ext}/downloads/${filename}.tar.gz
    echo "@@@ Checking trac for requested version $fullversion"
    #e.g. svn info https://svn.greenstone.org/main/trunk/cli-extensions/selfcontained-jdk/downloads/zulu11.82.19-ca-jdk11.0.28-linux_i686.tar.gz
    #   or svn info ^/main/trunk/cli-extensions/selfcontained-jdk/downloads/zulu11.82.19-ca-jdk11.0.28-linux_i686.tar.gz
    #echo ""
    #svn info ^$java_url
    #echo ""
    if svn info ^$java_url > /dev/null 2>&1 ; then
	echo "Downloading $osfname jdk binary from TRAC at $java_url"
	svn export ^$java_url
    else
	echo "@@@ Not found on trac at $java_url."	
	java_url=https://cdn.azul.com/zulu/bin/${filename}.tar.gz
	echo "Attempting to download from official site at $java_url"
	curl $java_url > ${filename}.tar.gz
	# Can't commit the JDK file to trac from any random machine: the JDK tarballs are too large
	# So don't commit it. Next time we run it on this machine, the tarball and extracted folder
	# will be there, so downloading won't get repeated anyway.
    fi
    
    if [ ! -e ${filename}.tar.gz ] ; then	    
	echo "ERROR: failed to download JDK ${filename}.tar.gz"
	exit 1
    fi
else
    echo "Detected ${osfname} JDK binary - no need to download"
fi

# No longer are there toplevel shortcuts in the macos JDK extracted tarball
# at least from JDK 11.88.17-ca-jdk11.0.31 onward as was present in 11.82.19-ca-jdk.11.0.28
# So need to make its Contents/Home the root jdk folder.
if [ "$thisOS" = "darwin" ] ; then
    tar xvzf ${filename}.tar.gz
    if [ -d ${filename}/Contents/Home ] ; then
	mv ${filename}/Contents/Home zulu-jdk${majorversion}-${bitness}bit
	rm -rf $filename
    else
	# Can do same as for linux
	mv ${filename} zulu-jdk${majorversion}-${bitness}bit
    fi
else
    tar xvzf ${filename}.tar.gz \
	&& mv ${filename} zulu-jdk${majorversion}-${bitness}bit
fi

if [ $? = 0 ] ; then
    echo "Untarred JDK${majorversion} and renamed the directory to:"
    echo "  zulu-jdk${majorversion}-${bitness}bit"
fi



