#!/bin/bash
# Cleanly remove Cybee from this Mac. Safe to run repeatedly. Requires root.
#   curl -sSf https://delete.cybee.dev | sudo sh
# Removes: the daemon, binaries, content, state (state.db + quarantine + tokens), the menu-bar
# app, logs, the control socket, and the pkg receipt. Only touches Cybee-owned paths.
set -u
[ "$(id -u)" = "0" ] || { echo "run with sudo:  sudo $0"; exit 1; }

say() { echo "==> $*"; }

EXT_ID="ai.cybee.CybeeAiAgent.desktop.CybeeAiAgentService"
say "deactivating the system extension"
systemextensionsctl uninstall D7UBTQ5RA2 "$EXT_ID" 2>/dev/null || true

say "stopping the app + legacy daemon"
launchctl bootout system /Library/LaunchDaemons/ai.cybee.agent.plist 2>/dev/null || true
pkill -f 'Cybee.app'  2>/dev/null || true
pkill -f 'cybee-esd'  2>/dev/null || true

say "removing files"
rm -f  /Library/LaunchDaemons/ai.cybee.agent.plist
rm -f  /usr/local/bin/cybee-esd /usr/local/bin/cybee
rm -rf /usr/local/share/cybee
rm -rf /var/db/cybee                 # state.db, quarantine, tokens (enrolment)
rm -f  /var/run/cybee.sock
rm -f  /var/log/cybee.log /var/log/cybee.log.*
rm -rf /Applications/Cybee.app /Applications/CybeeMenuBar.app

say "clearing package receipt"
pkgutil --forget ai.cybee.installer; pkgutil --forget ai.cybee.agent >/dev/null 2>&1 || true

echo
echo "Cybee removed. If the system extension still shows in System Settings, it clears on reboot."
echo "This does NOT delete the organization on the backend; the device simply stops reporting."
