#!/bin/bash
set -e

# Re-exec from /tmp so package removal doesn't kill this script mid-run
if [[ "$0" != "/tmp/uninstall-probe-basic" ]]; then
    cp "$0" /tmp/uninstall-probe-basic
    chmod +x /tmp/uninstall-probe-basic
    exec bash /tmp/uninstall-probe-basic "$@"
fi

echo "Removing Probe Basic and QtPyVCP..."
sudo apt purge -y python3-probe-basic python3-qtpyvcp

echo "Removing unused dependencies..."
sudo apt autoremove -y

echo "Removing APT repository and GPG keys..."
sudo rm -f /etc/apt/sources.list.d/kcjengr.list
sudo rm -f /etc/apt/trusted.gpg.d/kcjengr.gpg
gpg --batch --yes --delete-key 2DEC041F290DF85A 2>/dev/null || true

echo "Refreshing APT..."
sudo apt update

echo "Probe Basic has been fully removed. Your LinuxCNC installation is unaffected."
rm -f /tmp/uninstall-probe-basic
