#!/bin/sh

# Attempt to load Wacom LaunchAgent for all running users
# By https://github.com/MagerValp/Scripted-Mac-Package-Creation/blob/master/scripts/postinstall
for pid_uid in $(/bin/ps -axo pid,uid,args | /usr/bin/grep -i "[l]oginwindow.app" | /usr/bin/awk '{print $1 "," $2}'); do
	pid=$(echo $pid_uid | /usr/bin/cut -d, -f1)
	uid=$(echo $pid_uid | /usr/bin/cut -d, -f2)

  # Quit the service if it's already running
	/bin/launchctl bootout gui/$uid /Library/LaunchAgents/com.wacom.wacomtablet.plist

  # Re-enable the service if it had previously been marked Disabled (e.g. by CleanMyMac or "launchctl unload -w")
	/bin/launchctl enable gui/$uid/com.wacom.wacomtablet
	
	/bin/launchctl bootstrap gui/$uid /Library/LaunchAgents/com.wacom.wacomtablet.plist
done
