#!/bin/bash
set +e
API_URL='https://www.inerd.co.uk/remote_api.php'
BRAND_NAME='iNerd Repair & IT Services'
REVOKED_TOK=''
ELEVATE_UI='1'

if [ "$EUID" -ne 0 ] && [ "$ELEVATE_UI" = "1" ]; then
    /usr/bin/osascript -e "do shell script \"/bin/bash '$0' --as-root\" with administrator privileges" >/dev/null 2>&1
    exit 0
fi

# Read stored token if present before wiping
for CFG in "/Library/Application Support/iNerdRMM/config.json" "$HOME/Library/Application Support/iNerdRMM/config.json"; do
    if [ -z "$REVOKED_TOK" ] && [ -f "$CFG" ]; then
        REVOKED_TOK=$(/usr/bin/osascript -l JavaScript -e "var fs=$.NSString.stringWithContentsOfFileEncodingError('$CFG',$.NSUTF8StringEncoding,null); if(fs){try{JSON.parse(ObjC.unwrap(fs)).agent_token||''}catch(e){''}}" 2>/dev/null)
    fi
done

# 1. Notify server that Mac completed Zero-Trace Uninstall
if [ -n "$REVOKED_TOK" ]; then
    HOST_STR=$(/bin/hostname 2>/dev/null || echo "Mac")
    /usr/bin/curl -fsSL -m 4 -X POST "$API_URL" \
        -H "Content-Type: application/json" \
        -d "{\"action\":\"agent_report_uninstalled\",\"agent_token\":\"$REVOKED_TOK\",\"hostname\":\"$HOST_STR\"}" >/dev/null 2>&1 || true
fi

# 2. Show brief macOS Notification Center confirmation
CONSOLE_UID=$(/usr/bin/stat -f "%u" /dev/console 2>/dev/null || echo "")
if [ -n "$CONSOLE_UID" ] && [ "$CONSOLE_UID" != "0" ]; then
    /bin/launchctl asuser "$CONSOLE_UID" /usr/bin/osascript -e "display notification \"Remote support software & background services have been completely removed from this Mac.\" with title \"$BRAND_NAME — Uninstalled Cleanly\"" >/dev/null 2>&1 || true
else
    /usr/bin/osascript -e "display notification \"Remote support software & background services have been completely removed from this Mac.\" with title \"$BRAND_NAME — Uninstalled Cleanly\"" >/dev/null 2>&1 || true
fi

# 3. Unload & remove LaunchDaemon + LaunchAgent across console user & system
if [ -n "$CONSOLE_UID" ] && [ "$CONSOLE_UID" != "0" ]; then
    /bin/launchctl bootout "gui/$CONSOLE_UID" /Library/LaunchAgents/uk.co.inerd.rmm.agent.plist >/dev/null 2>&1 || true
fi
/bin/launchctl unload -w /Library/LaunchAgents/uk.co.inerd.rmm.agent.plist >/dev/null 2>&1 || true
/bin/launchctl bootout system /Library/LaunchDaemons/uk.co.inerd.rmm.plist >/dev/null 2>&1 || true
/bin/launchctl unload -w /Library/LaunchDaemons/uk.co.inerd.rmm.plist >/dev/null 2>&1 || true

/bin/rm -f /Library/LaunchDaemons/uk.co.inerd.rmm.plist /Library/LaunchAgents/uk.co.inerd.rmm.agent.plist 2>/dev/null
/usr/sbin/pkgutil --forget uk.co.inerd.rmm >/dev/null 2>&1 || true

# 4. Wipe Application Support directories & downloaded launchers across users
/bin/rm -rf "/Library/Application Support/iNerdRMM" 2>/dev/null
for UHOME in /Users/*; do
    if [ -d "$UHOME" ]; then
        /bin/rm -rf "$UHOME/Library/Application Support/iNerdRMM" 2>/dev/null
        /bin/rm -rf "$UHOME/Downloads/iNerd Remote Support.app" "$UHOME/Downloads/Install 24-7 Mac Support.app" 2>/dev/null
        /bin/rm -f "$UHOME/Downloads/"*-Mac-Support*.zip "$UHOME/Downloads/"*-Mac-24-7*.zip "$UHOME/Downloads/"*Clean-Uninstall*.command 2>/dev/null
    fi
done
/bin/rm -f /tmp/inerd_mac_* 2>/dev/null
/usr/bin/pkill -f "inerd_mac_ui_" >/dev/null 2>&1 || true

# Kill any lingering support daemon processes after detached 1s delay
( /bin/sleep 1; /usr/bin/pkill -f "iNerdRMM" >/dev/null 2>&1 || true ) >/dev/null 2>&1 &
exit 0