#!/bin/sh

# Unix script to start an executable jar archive with a minimum version
# requirement on a Unix machine.  
#
# parts of script are from  http://www.gimlisys.com/articles-detect-java.html
# and  https://stackoverflow.com/questions/7334754/correct-way-to-check-java-version-from-bash-script  
#
# This script should work for bash, dash (as used in debian) and Bourne shell-compatible sh (like in FreeBSD).
# This script should also work for future Java versions, e.g. 1.10 or 2.x
# 
# If you found a bug or an improvement, please send a mail to
# peafactory
#            @
#              eck.cologne
#
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#>>>    YOU CAN/MUST SET THE FOLLOWING FIVE VARIABLES:                                        >>>
#>>>    PEA, JAR, PATH_TO_JAR, REQUIRED_FIRST_VERSION_NUMBER, REQUIRED_SECOND_VERSION_NUMBER  >>>
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# 1. the name of the application
#    this will be displayed before starting
PEA="Calendar Lock PEA 1.7"

# 2. the executable jar archive to start (you must type the extension .jar)
JAR="calendar.jar"

# 3. the path of the jar archive
#    If this file is in the same directory as the executable jar archive, 
#    then this variable should remain empty
PATH_OF_JAR=""

# 4. and 5. Set the required version as two numbers: 
#    only the first two numbers (separated by dots) are taken into account
#    e.g. for 1.8.1_11 set
#    REQUIRED_FIRST_VERSION_NUMBER to 1 and REQUIRED_SECOND_VERSION_NUMBER to 8
REQUIRED_FIRST_VERSION_NUMBER=1
REQUIRED_SECOND_VERSION_NUMBER=8


######################################################################################################

# the java version to use (do not set this variable)
_JAVA=

# Function to check the version. If the version is suitable,
# this function executes the PEA and exits this script. 
# The variable _JAVA must be set before
check_version_and_run(){

	# to test future version: 
	#REQUIRED_FIRST_VERSION_NUMBER=2
	# to test future version: 
	#REQUIRED_SECOND_VERSION_NUMBER=11

	# get the version number
    	version=$("$_JAVA" -version 2>&1 | awk -F '"' '/version/ {print $2}')
    	# echo using java version is "$version"

	# get the first number of the version:
	# bash only: FIRST_VERSION_NUMBER="$( cut -d '.' -f 1 <<< "$version" )"; 
	FIRST_VERSION_NUMBER="$(echo "$version" | cut -d '.' -f 1)"; 
	# echo first "$FIRST_VERSION_NUMBER"
	
	# check if value is number
	case $FIRST_VERSION_NUMBER in
		''|*[!0-9]*) 
			# set variable to null and return
			_JAVA=
			return ;;
		*)  ;;
	esac

	# get the second number of the version
	SECOND_VERSION_NUMBER="$(echo "$version" |  cut -d '.' -f 2- )"; 
	SECOND_VERSION_NUMBER="$(echo "$SECOND_VERSION_NUMBER" |  cut -d '.' -f 1 )";
	#  echo second "$SECOND_VERSION_NUMBER"
	
	# check if value is number
	case $SECOND_VERSION_NUMBER in
		''|*[!0-9]*) 
			# set variable to null and return
			_JAVA=
			return ;;
		*)  ;;
	esac


	# check first number: continue if this is greater, otherwise reset _JAVA and return
	if  [  "$FIRST_VERSION_NUMBER" -gt "$REQUIRED_FIRST_VERSION_NUMBER" ]; 
		then
                # check if path is empty
                # if not, change directory
                if [ $PATH_OF_JAR ]
	                then
                        cd $PATH_OF_JAR
                fi

			    # start the PEA:
			    echo 'started JRE: ' 
			    ($_JAVA -version)
			    echo Start "$PEA"
			    nohup >/dev/null $_JAVA -jar $JAR
			    exit
 		else         
			:
	fi

	# equal, then check second number
	if  [  "$FIRST_VERSION_NUMBER" -eq "$REQUIRED_FIRST_VERSION_NUMBER" ]; 
		then
			:
 		else         
			# set variable to null and return
			_JAVA=
			return
	fi

	# check second number: run the PEA and exit script if succeeds, 
	# otherwise reset _JAVA and return
	if [ "$SECOND_VERSION_NUMBER" -ge "$REQUIRED_SECOND_VERSION_NUMBER" ]; 
    		then
                # check if path is empty
                # if not, change directory
                if [ $PATH_OF_JAR ]
	                then
                        cd $PATH_OF_JAR
                fi

			    # start the PEA:
			    echo 'started JRE: ' 
			    ($_JAVA -version)
			    echo Start "$PEA"
			    nohup >/dev/null $_JAVA -jar $JAR
			    exit
    		else              
			    # set variable to null and return
			    _JAVA=
			    return
    	fi
}


######################################################################
# 1. Check default Java to see if it exists and if version is adequate
# alternative:  command -v java >/dev/null 2>&1 || { echo >&2 "java is not installed..."; }

# check if java command is registered:
if command -v java 2>/dev/null; 
	then
		_JAVA="java"
		# check version and execute if required
		check_version_and_run
		# this is only executed if version check failed
		echo 'Your current default version of java is too old. Try to find more recent version...'
	else
		echo 'java is not registered. Try to find a java version...'
fi

######################################################################
# 2. Check JAVA_HOME environment variable (Linux...)
if  $JAVA_HOME
	then
		_JAVA=$JAVA_HOME/bin/java
		check_version_and_run
		# this is only executed if version check failed
		echo 'Your JAVA_HOME environment version is too old. Try to find more recent version...'
	else
		echo 'JAVA_HOME not set. Try to find suitable java version...'
fi


######################################################################
# 3. Use 'locate' to search for other possible java candidates and
#    check their versions.

for JAVA_EXE in $( locate bin/java | grep java$ | xargs echo )
	do
		_JAVA=$(echo $JAVA_EXE)
		#_JAVA=`echo $JAVA_EXE`
		check_version_and_run
		# this is only executed if version check failed
		echo 'Located java version failed. Search continues...'
	done

######################################################################
# Give error message
echo 'Cant find suitable java version.' 
echo 'If you want to use this program, you need to install a new Java Runtime Environment.'
# keep the shell:
$SHELL
