|
1 | 1 | #!/bin/zsh |
2 | 2 |
|
3 | | -# Script requires root so check for root access |
4 | | -if [ $(id -u) -ne 0 ]; then |
5 | | - echo "Please run this script as root or using sudo" |
6 | | - exit 1 |
| 3 | +set -u # error on unset vars |
| 4 | + |
| 5 | +# --- helpers --------------------------------------------------------------- |
| 6 | +log() { print -- "[uninstall] $*" } |
| 7 | +warn() { print -- "[warn] $*" >&2 } |
| 8 | +err() { print -- "[error] $*" >&2 } |
| 9 | + |
| 10 | +# run a command, ignore if it fails; print message |
| 11 | +_try() { |
| 12 | + local desc="$1"; shift |
| 13 | + if "$@"; then |
| 14 | + log "$desc: ok" |
| 15 | + else |
| 16 | + warn "$desc: skipped/failed" |
| 17 | + fi |
| 18 | +} |
| 19 | + |
| 20 | +# delete a file/dir if it exists |
| 21 | +_rm_if_exists() { |
| 22 | + local path="$1" |
| 23 | + if [ -e "$path" ] || [ -L "$path" ]; then |
| 24 | + /bin/rm -rf -- "$path" && log "removed: $path" || warn "failed to remove: $path" |
| 25 | + else |
| 26 | + log "not present: $path" |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +# get console user + uid |
| 31 | +get_console_user() { |
| 32 | + /usr/sbin/scutil <<< "show State:/Users/ConsoleUser" \ |
| 33 | + | /usr/bin/awk '/Name :/ && $3 != "loginwindow" { print $3 }' |
| 34 | +} |
| 35 | +get_console_uid() { |
| 36 | + /usr/sbin/scutil <<< "show State:/Users/ConsoleUser" \ |
| 37 | + | /usr/bin/awk '/kCGSSessionUserIDKey/ {print $NF; exit}' |
| 38 | +} |
| 39 | + |
| 40 | +# --- preflight ------------------------------------------------------------- |
| 41 | +if [ "$(id -u)" -ne 0 ]; then |
| 42 | + err "Please run this script as root or using sudo" |
| 43 | + exit 1 |
7 | 44 | fi |
8 | | -# Use Apple Recommended Method to detect the user signed in to the desktop |
9 | | -current_user=$(echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ && ! /loginwindow/ { print $3 }') |
10 | | -console_user_uid=$(echo "show State:/Users/ConsoleUser" | scutil | awk '/kCGSSessionUserIDKey/ {print $NF; exit}' ) |
11 | | -# Kill the process |
12 | | -echo "Killing the process..." |
13 | | -pkill -f SupportCompanion |
14 | | -# Remove user defaults |
15 | | -echo "Removing user defaults..." |
16 | | -sudo -u "${current_user}" defaults delete com.github.macadmins.SupportCompanion |
17 | | -# Unload launchctl job |
18 | | -echo "Unloading helepr launchctl job..." |
19 | | -launchctl unload -w /Library/LaunchDaemons/com.github.macadmins.SupportCompanion.helper.plist |
20 | | -# Remove launchctl job |
21 | | -echo "Removing helper launchctl job..." |
22 | | -rm -f /Library/LaunchDaemons/com.github.macadmins.SupportCompanion.helper.plist |
23 | | -# Remove launch agent |
24 | | -if [ -f "/Library/LaunchAgents/com.github.macadmins.SupportCompanion.agent.plist" ]; then |
25 | | - echo "Unloading launch agent..." |
26 | | - /bin/launchctl asuser "${console_user_uid}" /bin/launchctl unload -w /Library/LaunchAgents/com.github.macadmins.SupportCompanion.agent.plist |
27 | | - echo "Removing launch agent..." |
28 | | - rm /Library/LaunchAgents/com.github.macadmins.SupportCompanion.agent.plist |
| 45 | + |
| 46 | +APP_ID="com.github.macadmins.SupportCompanion" |
| 47 | +HELPER_ID="com.github.macadmins.SupportCompanion.helper" |
| 48 | +AGENT_ID="com.github.macadmins.SupportCompanion.agent" |
| 49 | + |
| 50 | +APP_PATH="/Applications/SupportCompanion.app" |
| 51 | +HELPER_PATH="/Library/PrivilegedHelperTools/${HELPER_ID}" |
| 52 | +DAEMON_PLIST="/Library/LaunchDaemons/${HELPER_ID}.plist" |
| 53 | +AGENT_PLIST="/Library/LaunchAgents/${AGENT_ID}.plist" |
| 54 | + |
| 55 | +CONSOLE_USER=$(get_console_user || true) |
| 56 | +CONSOLE_UID=$(get_console_uid || true) |
| 57 | + |
| 58 | +log "Console user: ${CONSOLE_USER:-unknown} (uid ${CONSOLE_UID:-n/a})" |
| 59 | + |
| 60 | +# --- stop processes -------------------------------------------------------- |
| 61 | +log "Stopping application and helper if running" |
| 62 | +_try "kill app" pkill -f SupportCompanion |
| 63 | + |
| 64 | +# --- defaults cleanup (non-fatal) ----------------------------------------- |
| 65 | +if [ -n "${CONSOLE_USER:-}" ]; then |
| 66 | + # Only attempt if domain exists to avoid noisy errors |
| 67 | + if sudo -u "$CONSOLE_USER" /usr/bin/defaults domains | /usr/bin/grep -q -- "$APP_ID"; then |
| 68 | + _try "delete user defaults" sudo -u "$CONSOLE_USER" /usr/bin/defaults delete "$APP_ID" |
| 69 | + else |
| 70 | + log "user defaults domain not present: $APP_ID" |
| 71 | + fi |
| 72 | +else |
| 73 | + warn "No console user detected; skipping user defaults removal" |
29 | 74 | fi |
30 | | -# Remove the app |
31 | | -echo "Removing the app..." |
32 | | -rm -rf /Applications/SupportCompanion.app |
33 | | -# Remove app data |
34 | | -echo "Removing helper..." |
35 | | -rm -rf "/Library/PrivilegedHelperTools/com.github.macadmins.SupportCompanion.helper" |
36 | | -# Forget the package |
37 | | -echo "Forgetting the package..." |
38 | | -pkgutil --forget com.github.macadmins.SupportCompanion > /dev/null 2>&1 |
39 | | -pkgutil --forget com.github.macadmins.SupportCompanion.LaunchAgent > /dev/null 2>&1 |
40 | | -pkgutil --forget com.github.macadmins.SupportCompanion.suite > /dev/null 2>&1 |
| 75 | + |
| 76 | +# --- launchd: daemon (helper) --------------------------------------------- |
| 77 | +if [ -f "$DAEMON_PLIST" ]; then |
| 78 | + log "Unloading helper launch daemon" |
| 79 | + _try "launchctl unload daemon" /bin/launchctl unload -w "$DAEMON_PLIST" |
| 80 | +else |
| 81 | + log "daemon plist not present: $DAEMON_PLIST" |
| 82 | +fi |
| 83 | + |
| 84 | +# Remove daemon plist regardless of unload outcome |
| 85 | +_rm_if_exists "$DAEMON_PLIST" |
| 86 | + |
| 87 | +# --- launchd: agent (per-user) -------------------------------------------- |
| 88 | +if [ -f "$AGENT_PLIST" ]; then |
| 89 | + if [ -n "${CONSOLE_UID:-}" ]; then |
| 90 | + log "Unloading launch agent for uid $CONSOLE_UID" |
| 91 | + _try "launchctl unload agent" /bin/launchctl asuser "$CONSOLE_UID" /bin/launchctl unload -w "$AGENT_PLIST" |
| 92 | + else |
| 93 | + warn "No console uid; unloading agent in current context" |
| 94 | + _try "launchctl unload agent (fallback)" /bin/launchctl unload -w "$AGENT_PLIST" |
| 95 | + fi |
| 96 | +else |
| 97 | + log "agent plist not present: $AGENT_PLIST" |
| 98 | +fi |
| 99 | + |
| 100 | +# Remove agent plist |
| 101 | +_rm_if_exists "$AGENT_PLIST" |
| 102 | + |
| 103 | +# --- remove bits ----------------------------------------------------------- |
| 104 | +log "Removing installed components" |
| 105 | +_rm_if_exists "$APP_PATH" |
| 106 | +_rm_if_exists "$HELPER_PATH" |
| 107 | + |
| 108 | +# --- pkg receipts ------------------------------------------- |
| 109 | +forget_if_present() { |
| 110 | + local package="$1" |
| 111 | + if /usr/sbin/pkgutil --pkgs | /usr/bin/grep -qx -- "$package"; then |
| 112 | + _try "forget $package" /usr/sbin/pkgutil --forget "$package" |
| 113 | + else |
| 114 | + log "receipt not present: $package" |
| 115 | + fi |
| 116 | +} |
| 117 | + |
| 118 | +forget_if_present "${APP_ID}" |
| 119 | +forget_if_present "${APP_ID}.LaunchAgent" |
| 120 | +forget_if_present "${APP_ID}.suite" |
| 121 | + |
| 122 | +log "Uninstall completed" |
| 123 | +exit 0 |
0 commit comments