#!/bin/sh
set -u

agent=/usr/lib/laotie-remote-ssh/laotie-remote-ssh-agent
service=laotie-remote-ssh-agent.service

if [ ! -x "$agent" ]; then
    echo "未安装"
    exit 1
fi

if [ -d /run/systemd/system ] && command -v systemctl >/dev/null 2>&1; then
    printf 'init system: systemd\n'
    printf 'service enabled: '
    systemctl is-enabled "$service" 2>/dev/null || true
    printf 'service active: '
    systemctl is-active "$service" 2>/dev/null || true
elif command -v rc-service >/dev/null 2>&1; then
    printf 'init system: openrc\n'
    rc-service laotie-remote-ssh-agent status 2>/dev/null || true
elif command -v sv >/dev/null 2>&1 && [ -d /etc/sv/laotie-remote-ssh-agent ]; then
    printf 'init system: runit\n'
    sv status laotie-remote-ssh-agent 2>/dev/null || true
elif [ -x /etc/init.d/laotie-remote-ssh-agent ]; then
    printf 'init system: sysvinit\n'
    /etc/init.d/laotie-remote-ssh-agent status 2>/dev/null || true
else
    printf 'init system: manual\n'
fi

id=$("$agent" --get-id 2>/dev/null || true)
if [ -n "$id" ]; then
    printf 'device ID: %s\n' "$id"
else
    echo "device ID: unavailable"
fi

if [ -d /run/systemd/system ] && command -v systemctl >/dev/null 2>&1; then
    systemctl status "$service" --no-pager --lines=0 2>/dev/null || true
fi
