#!/usr/bin/env bash
# Bring the X2 dashboard up ON THE ROBOT and keep it up.
#
# The dashboard is served by the robot itself, so the link people open is the
# robot's own address - http://<robot-ip>:8770. Nothing depends on any laptop
# being switched on, and when the robot joins a different Wi-Fi the link simply
# follows its new address.
#
# Idempotent by design: it starts only what is not already running, so it is
# safe to call from cron every minute, from @reboot, and by hand.
#
# No `set -u` - the ROS setup scripts read undefined variables.

set -e

DIR="$(dirname "$(readlink -f "$0")")"
LOG_DIR="/home/agi/x2_dashboard_logs"
AGENT_PORT="${X2_AGENT_PORT:-8781}"
WEB_PORT="${X2_WEB_PORT:-8770}"

mkdir -p "$LOG_DIR"

listening() {
    ss -ltn 2>/dev/null | grep -q ":$1 "
}

trim() {   # keep the logs from growing without bound
    [ -f "$1" ] || return 0
    if [ "$(stat -c%s "$1" 2>/dev/null || echo 0)" -gt 2000000 ]; then
        tail -n 300 "$1" > "$1.tmp" && mv "$1.tmp" "$1"
    fi
}

# 1. the ROS bridge -------------------------------------------------------
if ! listening "$AGENT_PORT" && ! pgrep -f "x2_agent.py" >/dev/null 2>&1; then
    trim "$LOG_DIR/agent.log"
    echo "[$(date -Is)] starting agent" >> "$LOG_DIR/agent.log"
    setsid nohup "$DIR/run_agent.sh" >> "$LOG_DIR/agent.log" 2>&1 < /dev/null &
    sleep 3
fi

# 2. the web dashboard ----------------------------------------------------
if ! listening "$WEB_PORT" && ! pgrep -f "x2_dashboard/-m backend|backend.server" >/dev/null 2>&1; then
    trim "$LOG_DIR/dashboard.log"
    echo "[$(date -Is)] starting dashboard" >> "$LOG_DIR/dashboard.log"
    cd /home/agi/x2_dashboard
    # The dashboard talks to the agent over localhost and needs no ROS itself,
    # but sourcing costs nothing and keeps one environment for both.
    setsid nohup env PYTHONUNBUFFERED=1 python3 -m backend \
        >> "$LOG_DIR/dashboard.log" 2>&1 < /dev/null &
fi

exit 0
