#!/usr/bin/env bash
# systemd_local_uninstall — Detiene, deshabilita y elimina un servicio systemd local.
set -euo pipefail

systemd_local_uninstall() {
    local name="$1"

    if [[ -z "$name" ]]; then
        echo "systemd_local_uninstall: se requiere name" >&2
        return 1
    fi

    local unit_path="/etc/systemd/system/${name}.service"

    # stop (idempotente: no falla si ya parado)
    sudo systemctl stop "${name}.service" 2>/dev/null || true
    sudo systemctl disable "${name}.service" 2>/dev/null || true

    if [[ -f "$unit_path" ]]; then
        sudo rm -f "$unit_path"
    fi

    sudo systemctl daemon-reload
    sudo systemctl reset-failed "${name}.service" 2>/dev/null || true

    printf '{"name":"%s","uninstalled":true}\n' "$name"
}
