#!/bin/bash
# OnsetPreFunctions for HOBOware Lite installer
#
#
# Notes:  1) When expecting a directory as a parameter, this pattern is used: ${2%/}
#            This looks at the value of parameter 2 and checks if it ends in '/' and
#            if it does, it removes it. This means you MUST make sure a '/' follows
#            if you are appending to that directory.
#
#         2) To get the results of a command that would normally go to stdout into a
#            variable,you can enclose the command in parentheses with a $ preceding
#            the opening parenthesis. For instance, to get the group name from the
#            id command, this command would work: UserGroup=$(id -gn "$USER")
#            Now, UserGroup would have the first group to which the currently logged
#            on user belongs to.
#
#         3) One of the big problems we've had is dealing with spaces in file and
#            folder names. OS X allows spaces in file and folder names while UNIX
#            often balks at them. To deal with this, we need to enclose variables
#            in { } braces when they are used. For instance:
#            Path=/Users/Jim Dodd/Documents"
#            NewPath="${Path}"
#            The Braces allow the "Jim Dodd" to pass through without terminating the
#            string.
#
#         3) Watch out for putting a wildcard character, I've only checked '*', inside
#            double quotes when trying to get a range of file names. For instance,
#            if you want to get all the Jar files in a folder, this will cause a
#            problem:
#                        cp "$FileFolder/*.jar" "$TargetFolder"
#            The problem is that the shell will be looking for a file literally
#            named "*.jar" which is impossible. File names cannot include am asterisk.
#            Instead, use this method to get wildcard expansion:
#                        cp "$FileFolder"/*.jar "$TargetFolder"
#
#
#         4) If this is to be an Upgrade version of the installer, you must call the
#            upgrade check program (which looks for an existing copy of HOBOware Lite)
#            in this script with the $2 argument which points to where HOBOware Lite is
#            supposed to be.
#
#
# -- DEFINE FUNCTIONS USED LATER IN THE SCRIPT
#
#
# Writes debugging information out to HOBOwareInstall.txt file in user's home folder
#
function WriteLog {
  echo "$*"  >> ~/HOBOwareInstall.txt
}

#WriteLog ""
#WriteLog ""
#WriteLog "==================================================="
#date >> ~/HOBOwareInstall.txt
#chmod 666 ~/HOBOwareInstall.txt
WriteLog ""
WriteLog "Running OnsetPreFunctions script"
WriteLog "Parameters:"
let ParamCount=0
for param in "${@}"
do
	let ParamCount=ParamCount+1
	WriteLog "$ParamCount: ${param}"
	# first parameter is name of installer program (and path, basename removes path)
	if [ $ParamCount -eq 1 ]
	then
		InstallerName=$(basename "${param}")
	fi
done
WriteLog ""

#
# --- FIRST, LIST WHICH VERSION OF HOBOWARE LITE WE ARE INSTALLING AND
#     WHICH VERSION OF THE INSTALLER WE ARE USING
WriteLog ""
ProgramVersion=2.5.0_20-RC5
InstallerSuffix=c
InstallerVersion="$ProgramVersion$InstallerSuffix"
WriteLog "Installing HOBOware Lite version $ProgramVersion"
if [ ${InstallerName} = "HOBOwareLite.pkg" ]
then
	WriteLog "Installer version $InstallerVersion"
else
	WriteLog "Installer (updater) version $InstallerVersion"
fi

#
# --- CHECK THAT THE NAME OF THE INSTALLER FILE IS CORRECT
#     IT MUST BE HOBOwareLite.pkg OR HOBOwareLite_Updater.pkg
#
if [ ${InstallerName} = "HOBOwareLite.pkg" ]
then
	WriteLog "This is an installer"
elif [ ${InstallerName} = "HOBOwareLite_Updater.pkg" ]
then
	WriteLog "This is an updater"
else
	WriteLog "The installer name, '${InstallerName}', is incorrect"
	let retval=96
	exit $retval
fi
WriteLog ""

# -- CREATE A VARIABLE THAT WILL BE RETURNED AS NON-ZERO IS AN ERROR OCCURS
#    IT IS INITIALLY ZERO FOR NO ERRORS
#
let retval=0

#
#  If this is an updater, the name of this installer will be HOBOwareLite_Updater.pkg
#  and the included program UpgradeCheck will be run.
#
# -- IF THIS IS AN UPDATER, CALL JAVA PROGRAM UpgradeLiteCheck WHICH MAKES SURE HOBOWARE LITE EXISTS ALREADY
if [ ${InstallerName} = "HOBOwareLite_Updater.pkg" ]
then
	if "$1/Contents/Resources/UpgradeLiteCheck.app/Contents/MacOS/JavaApplicationStub" "${2%/}/"
	then
	    WriteLog "Previous HOBOware Lite exists"
	else
	    let retval=96
	    WriteLog "No previous copy of HOBOware Lite found"
	    exit $retval
	fi
fi


#
# -- SECOND, CREATE FULL PATH TO WHERE HOBOWARE LITE WILL BE INSTALLED
AppPath=${2%/}/HOBOwareLite.app

WriteLog "AppPath: ${AppPath}"

#
# --- NEXT, CHECK IF PREVIOUS VERSION OF HOBOWARE LITE EXISTS
#     IF IT DOES, SAVE PLUGINS AND PROCESSORS AND THEN
#     REMOVE THE PREVIOUS HOBOWARE LITE
#
WriteLog "Checking for existing HOBOware Lite application"
if test -e "${AppPath}"
then
  WriteLog "Previous HOBOware Lite exists"
  PreviousVersion=$(defaults read "${AppPath%/}"/Contents/info CFBundleVersion)
  WriteLog "Previous version: $PreviousVersion" 

# --- Finally, attempt to remove the previous copy of HOBOware Lite
   if rm -f -R "${AppPath}"  ; then
      WriteLog "Previous HOBOware Lite removed"
   else
      WriteLog "** Error removing previous copy of HOBOware Lite"
   fi

# this else goes with the check if HOBOwareLite.app exists above
else
   WriteLog "No previous copy of HOBOware Lite found"
fi

WriteLog ""

exit $retval
