#!/bin/bash
# ============================================================================
#  Uninstall LetsTalkRemote from this Mac.
#
#  Double-click this file. It stops the background host, removes the app and its
#  system agent, erases this Mac's saved account link, and clears the Screen
#  Recording / Accessibility grants - so the next install starts 100% fresh.
#  You'll get ONE macOS password prompt (to remove the app from /Applications).
# ============================================================================
LABEL="com.letstalkremote.host"
UID_NUM="$(id -u)"

echo "Removing LetsTalkRemote from this Mac..."

# 1. Stop the background host (this is your own launchd session - no password).
launchctl bootout "gui/$UID_NUM/$LABEL" 2>/dev/null
pkill -f 'LetsTalkRemoteHost.app/Contents/MacOS/ltrhost' 2>/dev/null

# 2. Wipe this user's saved registration (fabric.json + device secret) and the
#    per-device permission grants, so a fresh install re-prompts cleanly and
#    shows the QR again instead of thinking this Mac is still registered.
rm -rf "$HOME/Library/Application Support/LetsTalkRemote"
tccutil reset ScreenCapture "$LABEL" 2>/dev/null
tccutil reset Accessibility "$LABEL" 2>/dev/null

# 3. Remove the app, the system LaunchAgent, the helper buttons, and the pkg
#    receipt. These live in root-owned locations, so this is the one step that
#    needs an admin password - macOS shows its normal prompt.
osascript <<'OSA'
do shell script "rm -rf /Applications/LetsTalkRemoteHost.app; rm -f /Library/LaunchAgents/com.letstalkremote.host.plist; rm -f '/Applications/Re-register LetsTalkRemote.command'; rm -f '/Applications/Uninstall LetsTalkRemote.command'; pkgutil --forget com.letstalkremote.host 2>/dev/null || true" with administrator privileges
OSA

echo ""
echo "Done. LetsTalkRemote is fully removed and this Mac is unregistered."
echo "You can reinstall any time from letstalkremote.xyz to test the flow again."
echo ""
read -r -p "Press Return to close this window." _
