#!/usr/bin/env bash
# platform-apt-host/remove — drop the sudoers file and the managed authorized_keys block.
# NEVER touches dataDir, the aptly config or the gpg keyring (the signing key is irreplaceable).
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
. "$HERE/../lib/authkeys.sh"
P=$(cat)
ETC="${SSR_ETC:-/etc}"; HOMES="${SSR_HOME_ROOT:-/home}"
USR=$(jq -r '.publishUser // empty' <<<"$P")
if [ -z "$USR" ] && [ -f "$ETC/sudoers.d/silverlabs-aptly" ]; then USR=$(awk '{print $1; exit}' "$ETC/sudoers.d/silverlabs-aptly"); fi
rm -f "$ETC/sudoers.d/silverlabs-aptly" "$ETC/silverlined/platform-apt-host.conf"
if [[ "$USR" =~ ^[a-z_][a-z0-9_-]{0,31}$ ]]; then authkeys_remove_block "$HOMES/$USR/.ssh/authorized_keys" platform-apt-host; fi
echo '{}' > "$SSR_OUTPUTS"
