#!/bin/sh
# uninstall.command — Licenseware Collector macOS uninstaller wrapper
#
# Double-click this file (or run it from Terminal) to remove the collector.
# The runtime binary's own "uninstall --force" command:
#   - Stops the launchd daemon (launchctl unload)
#   - Removes the launchd plist from /Library/LaunchDaemons/
#   - Wipes /usr/local/libexec/licenseware-collector/ (runtime dir)
#   - Removes /usr/local/etc/licenseware-collector (env file)
#
# The pkg seed directory (/usr/local/licenseware-collector/seed/) can be
# removed separately via: sudo pkgutil --forget io.licenseware.collector
# and manual deletion, since macOS does not ship an "uninstall" pkg concept.

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 service (you may be prompted for your password)..."
sudo LICENSEWARE_UNINSTALL_FROM_PACKAGE=1 "${RUNTIME_BIN}" uninstall --force

echo "Removing seed directory..."
sudo rm -rf /usr/local/licenseware-collector

echo "Forgetting pkg receipt..."
sudo pkgutil --forget io.licenseware.collector 2>/dev/null || true

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