#!/bin/sh
# systemd ExecStart launcher for sigma_agent.service.
#
# Sets up the agent environment (from /etc/sigma/agent.conf), refuses to start
# until configured, then exec's the scheduler in the FOREGROUND with -D (the
# agent's "daemon setup without forking" mode). Because it does not fork, this
# process becomes the unit's main PID and systemd tracks it correctly.
set -e
. /etc/sigma/agent.conf

# Refuse to start with a blank or placeholder (qali01) central-server -- the
# installer never collected one. Fail clearly instead of looping.
cs=`sed -n 's/^[[:space:]]*central-server[[:space:]]*=[[:space:]]*\([^:[:space:]]*\).*/\1/p' "$AVCONF/config" 2>/dev/null | head -1`
if [ -z "$cs" ] || [ "$cs" = "qali01" ]; then
    echo "Sigma agent is not configured (no central server). Run: sudo sigma-agent-setup" >&2
    # Exit 78 (EX_CONFIG): a permanent config problem, not a crash. The unit's
    # RestartPreventExitStatus=78 makes systemd land on 'failed' immediately
    # instead of retry-looping, while real crashes still auto-restart.
    exit 78
fi

exec "$PYTHON" "$AVBIN/scheduler.py" -D
