#!/bin/sh
# uninstall.command — Licenseware Collector macOS uninstaller wrapper
#
# Double-click this file (or run it from Terminal) to remove the collector.
# Everything is done by the runtime binary's own "uninstall --force", which
# runs the shared removal Plan (internal/uninstaller/plan.go):
#   - Stops the launchd daemon (launchctl unload) and removes its plist
#   - Wipes /usr/local/libexec/licenseware-collector/ (runtime dir)
#   - Removes /usr/local/etc/licenseware-collector (env file)
#   - Removes the collected-data directory
#   - Removes /usr/local/licenseware-collector/ (pkg seed dir)
#   - Removes osquery when we own it (/opt/osquery, the osqueryi symlink) and
#     forgets its pkg receipts
#   - Removes /var/log/licenseware-collector.log
#   - Forgets the io.licenseware.collector pkg receipt
#
# macOS ships no "uninstall" pkg concept, so a control-plane-triggered
# uninstall runs the exact same Plan rather than delegating anywhere. Both
# paths leave the machine in an identical state.

set -e

RUNTIME_BIN=/usr/local/libexec/licenseware-collector/licenseware-collector

echo "=== Licenseware Collector Uninstaller ==="
echo ""

if [ ! -f "${RUNTIME_BIN}" ]; then
    echo "ERROR: Runtime binary not found at ${RUNTIME_BIN}" >&2
    echo "The collector may have already been removed or was never installed." >&2
    exit 1
fi

echo "Uninstalling the Licenseware Collector (you may be prompted for your password)..."
sudo LICENSEWARE_UNINSTALL_FROM_PACKAGE=1 "${RUNTIME_BIN}" uninstall --force

echo ""
echo "Uninstallation complete."
echo "The Licenseware Collector service and all associated files have been removed."
