#!/bin/bash
# Uninstall Probe Helper.
#
# Removes the LaunchDaemon plist, the helper binary, and the
# snapshot directory. Probe.app falls back to its sandbox-only
# "Recent apps" view within one polling tick (~2s) once the
# snapshot file disappears.
#
# Usage: sudo bash uninstall.sh
#
# This script is what the future uninstall .pkg's postinstall will
# invoke. Users who installed via the .pkg can also download this
# script standalone from juancarlosh.com/probe.

set -e

LABEL="com.jcherrera.Probe.helper"
PLIST="/Library/LaunchDaemons/${LABEL}.plist"
SUPPORT_DIR="/Library/Application Support/Probe Helper"

if [ "$EUID" -ne 0 ]; then
    echo "uninstall.sh must be run as root: sudo bash uninstall.sh" >&2
    exit 1
fi

# 1. Stop the daemon.
if launchctl print "system/${LABEL}" &>/dev/null; then
    echo "uninstall: bouting ${LABEL}"
    launchctl bootout "system/${LABEL}" || true
fi

# 2. Remove the plist.
if [ -f "${PLIST}" ]; then
    rm -f "${PLIST}"
    echo "uninstall: removed ${PLIST}"
fi

# 3. Remove the helper binary + snapshot file.
if [ -d "${SUPPORT_DIR}" ]; then
    rm -rf "${SUPPORT_DIR}"
    echo "uninstall: removed ${SUPPORT_DIR}"
fi

echo "uninstall: complete"
exit 0
