#!/usr/bin/env bash
# hosting-node/remove — revoke the agent's docker access and the siteproxy bind_ip.
# Hosted package containers and the siteproxy container are LEFT RUNNING: deprovision
# packages through SilverDESK first. Re-apply restores the role.
set -euo pipefail
T=/etc/silvershell-agent/agent.toml
CHANGED=0
NEWT=$(mktemp)
REST=$(awk 'BEGIN{skip=0} /^\[/{skip=($0=="[plugins.siteproxy]")} !skip' "$T")
printf '%s\n' "$REST" > "$NEWT"
if ! cmp -s "$NEWT" "$T"; then install -m 640 -o root -g silvershell-agent "$NEWT" "$T"; CHANGED=1; fi
rm -f "$NEWT"
if id -nG silvershell-agent | tr ' ' '\n' | grep -qx docker; then gpasswd -d silvershell-agent docker >/dev/null; CHANGED=1; fi
if (( CHANGED )); then
  systemd-run --quiet --on-active=20 --unit "ssa-restart-$(date +%s)" systemctl restart silvershell-agent
fi
echo "hosting-node removed (containers left running)"
