#!/bin/sh
# reports-mcp start|stop|restart|status — the reports MCP server, without an init system.
PID=/run/reports-mcp.pid
running() { [ -f "$PID" ] && kill -0 "$(cat "$PID")" 2>/dev/null; }
start() {
    running && return 0
    setsid python3 /opt/reports-mcp/reports_mcp.py </dev/null >>/var/log/reports-mcp.log 2>&1 &
    echo $! > "$PID"
    for _ in 1 2 3 4 5 6 7 8 9 10; do
        python3 -c 'import socket; socket.create_connection(("127.0.0.1", 8931), 0.3)' 2>/dev/null && return 0
        sleep 0.2
    done
    echo "reports-mcp: did not start; see /var/log/reports-mcp.log" >&2
    return 1
}
stop() { running && kill "$(cat "$PID")" 2>/dev/null; rm -f "$PID"; sleep 0.3; }
case "$1" in
    start) start ;;
    stop) stop ;;
    restart) stop; start ;;
    status) if running; then echo "running, pid $(cat "$PID")"; else echo "stopped"; exit 3; fi ;;
    *) echo "usage: reports-mcp start|stop|restart|status" >&2; exit 2 ;;
esac
