#!/usr/bin/env bash
# Launcher for the X2 dashboard agent. Sources ROS 2 and the AimDK workspace,
# then runs the agent. Used both interactively and by the systemd --user unit.
#
# Note: no `set -u` here. The ROS 2 setup scripts read variables that are not
# defined on a fresh shell (AMENT_TRACE_SETUP_FILES among them), so nounset
# makes sourcing them fail outright.

set -e

export RCUTILS_LOGGING_SEVERITY="${RCUTILS_LOGGING_SEVERITY:-ERROR}"
export ROS_DOMAIN_ID="${ROS_DOMAIN_ID:-0}"

# shellcheck disable=SC1091
source /opt/ros/humble/setup.bash

if [ -f "$HOME/aimdk/install/setup.bash" ]; then
    # shellcheck disable=SC1091
    source "$HOME/aimdk/install/setup.bash"
fi

# -u keeps the agent's own prints unbuffered so they reach the journal promptly.
# Fast DDS writes a very high volume of discovery chatter to stderr; the systemd
# unit sends stderr to /dev/null so it does not swamp the journal. Run this
# script by hand if you need to see it.
exec python3 -u "$(dirname "$(readlink -f "$0")")/x2_agent.py" "$@"
