#!/bin/bash

# Lovingly lifted from Per Olofsson
# https://github.com/MagerValp/Scripted-Mac-Package-Creation/blob/master/scripts/postinstall

export PATH=/usr/bin:/bin:/usr/sbin:/sbin

# Execute preinstall actions if we're installing on a live system.
# This is useful for unloading launch daemons and agents.
if [ "$3" == "/" ]; then

    # Run preinstall actions for root.
    launchctl unload /Library/LaunchDaemons/com.googlecode.munki.app_usage_monitor.plist
    launchctl unload /Library/LaunchDaemons/com.googlecode.munki.appusaged.plist

    # Run preinstall actions for all logged in users.
    os_build_version=$(sw_vers -buildVersion | cut -b 1-2)
    for pid_uid in $(ps -axo pid,uid,args | grep -i "[l]oginwindow.app" | awk '{print $1 "," $2}'); do
        pid=$(echo $pid_uid | cut -d, -f1)
        uid=$(echo $pid_uid | cut -d, -f2)
        if [[ $os_build_version -lt 14 ]] ; then
            launchctl bsexec "$pid" chroot -u "$uid" / launchctl unload /Library/LaunchAgents/com.googlecode.munki.app_usage_monitor.plist
        else
            launchctl asuser "$uid" launchctl unload /Library/LaunchAgents/com.googlecode.munki.app_usage_monitor.plist
        fi
    done

fi

exit 0