#!/bin/bash
#
# This script file is called by the OS X Installer before installing any
# files. If this script returns 0, the installation will procede. If this
# script returns 96, the installation will stop and display a message
# saying the installation could not procede and mention the name of
# the HOBOware application.
#
# InstallCheck is a Java application that checks what version of OS X is
# installed and what version of Java is installed. It returns 0 if these
# are acceptable and non-zero if they are not acceptable.
#
# Note - Executable property has been set in Subversion
#
echo ""
echo "==================================================" >>  ~/HOBOwareInstall.txt
date >> ~/HOBOwareInstall.txt
chmod 666 ~/HOBOwareInstall.txt

#let retval=112 to display error message 16, see "InstallationCheck.strings" in Install_resources/en.lproj etc.
#let retval=113 to display error message 17, see "InstallationCheck.strings" in Install_resources/en.lproj etc.
#let retval=114 to display error message 18, see "InstallationCheck.strings" in Install_resources/en.lproj etc.
let retval=0

#
# F I R S T  -  C H E C K   T H A T   O P E R A T I N G   S Y S T E M   V E R S I O N   I S   H I G H   E N O U G H
# At this point, checking that it is 10.5 or higher
#
OSVersion=$(sw_vers -productVersion)
echo "Operating System: OS X $OSVersion" >> ~/HOBOwareInstall.txt 

# Parse the operating system version string into MajorOSVersion, MinorOSVersion and ReleaseOSLevel
# We don't worry about ReleaseOSLevel yet
#
oldifs="$IFS"
IFS=.

let vCount=0
for v in $OSVersion
do
  let vCount=vCount+1

  # first pass (vCount == 1) gives major version
  if [ $vCount -eq 1 ]
  then
    MajorOSVersion=$v
  # second pass (vCount == 2) gives minor version
  elif [ $vCount -eq 2 ]
  then
    MinorOSVersion=$v
  # third pass (vCount == 3) gives release level
  elif [ $vCount -eq 3 ]
  then
    ReleaseOSLevel=$v
  fi
done
IFS="$oldifs"

# Now check that the three numbers are in range
# Actually, we don't worry about ReleaseOSLevel yet

if [ $MajorOSVersion -lt 10 ]
then
	echo "** OS X version is too low (should be 10.5 or higher but is $OSVersion)" >>  ~/HOBOwareInstall.txt
	let retval=112
elif [ $MajorOSVersion -eq 10 ]
then
	if [ $MinorOSVersion -lt 5 ]
	then
		echo "** OS X version is too low (should be 10.5 or higher but is $OSVersion)" >>  ~/HOBOwareInstall.txt
		let retval=112
	fi
fi

#
# N E X T  -  C H E C K   T H A T   J A V A   V I R T U A L   M A C H I N E   V E R S I O N   I S   H I G H   E N O U G H
# At this point, checking that it is 1.6 or higher
#
FullJVMString=$(/usr/libexec/java_home --version 1.6+)
echo "Path to JVM version: $FullJVMString" >> ~/HOBOwareInstall.txt

JVMVersion=$(/usr/libexec/java_home --version 1.6+ | grep -o '[0-9]*\.[0-9]*\.[0-9]*')
echo "JVM Version number: $JVMVersion" >> ~/HOBOwareInstall.txt

#    check here if number is 1.6 or higher 

# Parse the JVM version string into MajorJVMVersion, MinorJVMVersion and ReleaseJVMLevel
# We don't worry about ReleaseJVMLevel yet
#
oldifs="$IFS"
IFS=.

let vCount=0
for v in $JVMVersion
do
  let vCount=vCount+1

  # first pass (vCount == 1) gives major version
  if [ $vCount -eq 1 ]
  then
    MajorJVMVersion=$v
  # second pass (vCount == 2) gives minor version
  elif [ $vCount -eq 2 ]
  then
    MinorJVMVersion=$v
  # third pass (vCount == 3) gives release level
  elif [ $vCount -eq 3 ]
  then
    ReleaseJVMLevel=$v
  fi
done
IFS="$oldifs"

# Now check that the three numbers are in range
# Actually, we don't worry about ReleaseJVMLevel yet

if [ $MajorJVMVersion -lt 1 ]
then
	echo "** Java JVM version is too low (should be 1.6 or higher but is $JVMVersion)" >>  ~/HOBOwareInstall.txt
	let retval=113
elif [ $MajorJVMVersion -eq 1 ]
then
	if [ $MinorJVMVersion -lt 6 ]
	then
		echo "** Java JVM version is too low (should be 1.6 or higher but is $JVMVersion)" >>  ~/HOBOwareInstall.txt
		let retval=113
	fi
fi


echo "All available Java Virtual Machines on this computer:" >> ~/HOBOwareInstall.txt
/usr/libexec/java_home --verbose >>~/HOBOwareInstall.txt 2>&1

#
# Check if HOBOWare is already running, notice the second grep which removes the grep process itself from the list
#
if ps -A | grep "HOBOware.app" | grep -v "grep"
then
	echo "** HOBOware is running" >>  ~/HOBOwareInstall.txt
	let retval=114
fi	

exit $retval
