#!/bin/bash -p
#
#   If  the shell is started with the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied,
#   no startup files are read, shell functions are not inherited from the environment, the SHELLOPTS variable, if it appears in the environment,
#   is ignored, and the effective user id is set to the real user id.
#   If the -p option is supplied at invocation, the startup behavior is the same, but the effective user id is not reset.
#-----------------------------------------------------------------------------

#  precheck.sh
#  Lili
#
#  Copyright (c) 2012 LogMeIn. All rights reserved.
#
#  Runs as the current user

LOGFILE=$1
exec 2>&1 # >>$LOGFILE   We do not need this anymore, script runners capture script outputs

BUNDLEPATH=$2
ROOTPATH=$3

function Run()
{
    echo "Killing Toolkit process"
    killall -TERM Toolkit
    sleep 2
    killall -KILL Toolkit

    echo "Killing Ignition process"
    killall -TERM LogMeInIgnition
    killall -TERM "LogMeIn Ignition"
    sleep 2
    killall -KILL LogMeInIgnition
    killall -KILL "LogMeIn Ignition"

    echo "Stopping running daemons and agents..."
    # Try the bruteforce first
    launchctl stop /Library/LaunchDaemons/com.logmein.logmeinserver.plist
    launchctl unload -w /Library/LaunchDaemons/com.logmein.logmeinserver.plist
    launchctl unload -w /Library/LaunchDaemons/com.logmein.logmeinblanker.plist
    launchctl unload -w /Library/LaunchDaemons/com.logmein.raupdate.plist
    launchctl unload -w /Library/LaunchAgents/com.logmein.LMILaunchAgentFixer.plist
    launchctl unload -w -S Aqua /Library/LaunchAgents/com.logmein.logmeingui.plist
    launchctl unload -w -S Aqua /Library/LaunchAgents/com.logmein.logmeinguiagent.plist
    launchctl unload -w -S LoginWindow /Library/LaunchAgents/com.logmein.logmeinguiagentatlogin.plist
    # Try the normal way just to be sure
    if [ -f "/Library/Application Support/LogMeIn/bin/LogMeIn.app/Contents/Helpers/logmeinserverctl" ]; then
        "/Library/Application Support/LogMeIn/bin/LogMeIn.app/Contents/Helpers/logmeinserverctl" stop
        sleep 2
        "/Library/Application Support/LogMeIn/bin/LogMeIn.app/Contents/Helpers/logmeinserverctl" disable
        sleep 2
    fi
	#Try to stop legacy logmeinserver
    if [ -f "/Library/Application Support/LogMeIn/bin/LogMeIn.app/Contents/Resources/logmeinserverctl" ]; then
        "/Library/Application Support/LogMeIn/bin/LogMeIn.app/Contents/Resources/logmeinserverctl" stop
        sleep 2
        "/Library/Application Support/LogMeIn/bin/LogMeIn.app/Contents/Resources/logmeinserverctl" disable
        sleep 2
    fi
}

echo "precheck: started with path $ROOTPATH and bundlepath $BUNDLEPATH"
echo "current uid=${UID}, gid=$( id | awk -F= '/gid=/ { print $3 }' | awk -F\(  '{ print $1 }' ), euid=${EUID}, egig=$(id -g), user=${USER}"
echo "id=$(id)"

Run

echo "precheck: finished"

# return 0 in case of success
# return 253 in case of success but signal user logoff required to finalize install
# return 254 in case of success but signal OS restart required to finalize install
# return 255 or a negative value in case of errors

exit 0
