#!/usr/bin/env bash
# =============================================================================
# Platform entrypoint — starts correlator, Apache, sentinel in order
# =============================================================================
set -eo pipefail

log() { echo "[entrypoint] $(date +%H:%M:%S) $*"; }

CORRELATOR_PID=""
HTTPD_PID=""
SENTINEL_PID=""

cleanup() {
    log "Shutting down..."
    [ -n "$SENTINEL_PID" ] && kill "$SENTINEL_PID" 2>/dev/null || true
    [ -n "$CORRELATOR_PID" ] && kill "$CORRELATOR_PID" 2>/dev/null || true
    httpd -k stop 2>/dev/null || true
    wait 2>/dev/null || true
    log "All processes stopped."
}
trap cleanup EXIT SIGTERM SIGINT

# -- 1. Start correlator (creates Unix sockets) ------------------------------
log "Starting correlator..."
correlator -config /etc/logcorrelator/correlator.yml &
CORRELATOR_PID=$!

# Wait for correlator to create its sockets
for i in $(seq 1 30); do
    if [ -S /var/run/logcorrelator/http.socket ] && [ -S /var/run/logcorrelator/network.socket ]; then
        log "Correlator sockets ready."
        break
    fi
    sleep 0.5
done

if [ ! -S /var/run/logcorrelator/http.socket ]; then
    log "ERROR: correlator sockets not created after 15s"
    exit 1
fi

# -- 2. Start Apache (with mod-reqin-log writing to http.socket) -------------
log "Starting Apache..."
httpd -DFOREGROUND &
HTTPD_PID=$!
sleep 2

# -- 3. Start sentinel (captures network traffic) ----------------------------
log "Starting sentinel..."
sentinel -config /etc/ja4sentinel/config.yml &
SENTINEL_PID=$!

log "All services started. PIDs: correlator=$CORRELATOR_PID httpd=$HTTPD_PID sentinel=$SENTINEL_PID"

# -- Wait for any process to exit (indicates failure) -------------------------
wait -n "$CORRELATOR_PID" "$HTTPD_PID" "$SENTINEL_PID" 2>/dev/null || true
EXIT_CODE=$?
log "A process exited with code $EXIT_CODE — triggering shutdown."
exit $EXIT_CODE
