#!/usr/bin/env bash
# AutoServer uninstall. Does NOT unbind the license key (dashboard only).
set -euo pipefail

if [[ "$(id -u)" -ne 0 ]]; then
  echo "Run as root." >&2
  exit 1
fi

echo "This will remove /opt/autoserver if it exists."
echo "Your license key will NOT be unbound. Unbind the machine at https://autoserver.org"
read -r -p "Type yes to continue: " ans
if [[ "$ans" != "yes" ]]; then
  echo "Cancelled."
  exit 0
fi

systemctl stop autoserver 2>/dev/null || true
systemctl disable autoserver 2>/dev/null || true
rm -f /etc/systemd/system/autoserver.service
systemctl daemon-reload 2>/dev/null || true
rm -rf /opt/autoserver
echo "Done. /var/lib/autoserver was kept so a reinstall on this machine can bind again."
echo "To remove machine identity too: rm -rf /var/lib/autoserver"
