#!/usr/bin/env sh set -eu INSTALL_DIR="${PLATFORM_AGENT_DIR:-/opt/platform-agent}" SERVICE_NAME="${PLATFORM_AGENT_SERVICE:-platform-agent}" SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" log() { printf '%s\n' "$*" } need_cmd() { command -v "$1" >/dev/null 2>&1 } as_root() { if [ "$(id -u)" -eq 0 ]; then "$@" elif need_cmd sudo; then sudo "$@" else log "Root privileges are required: $*" exit 1 fi } prompt_yes_no() { label="$1" default="${2:-no}" suffix="[y/N]" [ "$default" = "yes" ] && suffix="[Y/n]" printf '%s %s ' "$label" "$suffix" IFS= read -r answer || answer="" answer="$(printf '%s' "$answer" | tr '[:upper:]' '[:lower:]')" if [ -z "$answer" ]; then [ "$default" = "yes" ] return fi [ "$answer" = "y" ] || [ "$answer" = "yes" ] } read_config_value() { key_path="$1" config_path="${INSTALL_DIR}/agent.config.json" if [ ! -f "$config_path" ] || ! need_cmd python3; then return fi python3 - "$config_path" "$key_path" <<'PY' import json import sys with open(sys.argv[1], "r", encoding="utf-8") as fp: data = json.load(fp) value = data for key in sys.argv[2].split("."): if not isinstance(value, dict): value = None break value = value.get(key) if isinstance(value, str): print(value) PY } expand_path_for_user() { path="$1" run_user="$2" case "$path" in "~") if [ "$run_user" = "root" ]; then printf '/root' else printf '/home/%s' "$run_user" fi ;; "~/"*) if [ "$run_user" = "root" ]; then printf '/root/%s' "${path#~/}" else printf '/home/%s/%s' "$run_user" "${path#~/}" fi ;; *) printf '%s' "$path" ;; esac } service_user() { if [ -f "$SERVICE_FILE" ]; then user_line="$(sed -n 's/^User=//p' "$SERVICE_FILE" | head -n 1)" [ -n "$user_line" ] && { printf '%s' "$user_line" return } fi id -un } remove_packages() { packages="$1" if [ -z "$packages" ]; then return fi if need_cmd apt-get; then as_root apt-get remove -y $packages || true as_root apt-get autoremove -y || true elif need_cmd dnf; then as_root dnf remove -y $packages || true elif need_cmd yum; then as_root yum remove -y $packages || true elif need_cmd pacman; then as_root pacman -Rns --noconfirm $packages || true elif need_cmd zypper; then as_root zypper --non-interactive remove $packages || true else log "No supported package manager found. Skipping package removal." fi } clear log "Autonomy Studio Platform Agent Uninstall" log "========================================" log "Service : ${SERVICE_NAME}" log "Install dir : ${INSTALL_DIR}" log "" RUN_USER="$(service_user)" WORKSPACE_ROOT="$(read_config_value workspace.root || true)" [ -n "${WORKSPACE_ROOT:-}" ] || WORKSPACE_ROOT="~/autonomy-studio/workspaces" WORKSPACE_PATH="$(expand_path_for_user "$WORKSPACE_ROOT" "$RUN_USER")" log "Detected user : ${RUN_USER}" log "Workspace root : ${WORKSPACE_ROOT}" log "Resolved workspace : ${WORKSPACE_PATH}" log "" if ! prompt_yes_no "Uninstall the platform agent now?" "no"; then log "Canceled." exit 0 fi if need_cmd systemctl; then log "Stopping systemd service." as_root systemctl stop "$SERVICE_NAME" 2>/dev/null || true as_root systemctl disable "$SERVICE_NAME" 2>/dev/null || true if [ -f "$SERVICE_FILE" ]; then as_root rm -f "$SERVICE_FILE" as_root systemctl daemon-reload fi elif need_cmd service; then as_root service "$SERVICE_NAME" stop 2>/dev/null || true fi if prompt_yes_no "Remove install directory ${INSTALL_DIR}?" "yes"; then case "$INSTALL_DIR" in "/"|"/root"|"/home"|"/usr"|"/opt"|"/var"|"/etc") log "Refusing to remove broad system directory: ${INSTALL_DIR}" ;; *) as_root rm -rf "$INSTALL_DIR" ;; esac fi if prompt_yes_no "Delete workspace ${WORKSPACE_PATH}? This removes cloned modules and user files." "no"; then case "$WORKSPACE_PATH" in "/home/$RUN_USER"|"/home/$RUN_USER/") log "Refusing to remove user home directory: ${WORKSPACE_PATH}" ;; "/"|"/root"|"/home"|"/usr"|"/opt"|"/var"|"/etc"|"/home/"*) as_root rm -rf "$WORKSPACE_PATH" ;; *) as_root rm -rf "$WORKSPACE_PATH" ;; esac fi if need_cmd tailscale && prompt_yes_no "Logout this device from Tailscale/Headscale?" "no"; then as_root tailscale logout || true fi if prompt_yes_no "Remove packages installed by install.sh if present? (tailscale openssh-server git)" "no"; then remove_packages "tailscale openssh-server git" fi log "" log "Uninstall complete."