#!/usr/bin/env bash
# systemd_local_restart — Reinicia un servicio systemd local.
set -euo pipefail

systemd_local_restart() {
    local name="$1"

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

    if ! sudo systemctl restart "${name}.service" >&2; then
        echo "systemd_local_restart: restart falló para '$name'" >&2
        return 1
    fi

    local pid
    pid=$(systemctl show -p MainPID --value "${name}.service" 2>/dev/null || echo 0)

    printf '{"name":"%s","restarted":true,"pid":%s}\n' "$name" "${pid:-0}"
}
