#!/usr/bin/env bash
# ros2-launch.sh — Source ROS2 Humble + workspace overlay, then exec ros2 launch
#
# Used by saltybot-*.service units so that ExecStart= does not need to inline
# bash source chains.
#
# Usage (from a service file):
#   ExecStart=/opt/saltybot/scripts/ros2-launch.sh <pkg> <launch_file> [args...]
#
# Env vars respected:
#   ROS_DISTRO        default: humble
#   SALTYBOT_WS       default: /opt/saltybot/jetson/ros2_ws/install
#   ROS_DOMAIN_ID     default: 42

set -euo pipefail

ROS_DISTRO="${ROS_DISTRO:-humble}"
SALTYBOT_WS="${SALTYBOT_WS:-/opt/saltybot/jetson/ros2_ws/install}"

ROS_SETUP="/opt/ros/${ROS_DISTRO}/setup.bash"
WS_SETUP="${SALTYBOT_WS}/setup.bash"

if [[ ! -f "${ROS_SETUP}" ]]; then
    echo "[ros2-launch] ERROR: ROS2 setup not found: ${ROS_SETUP}" >&2
    exit 1
fi

# shellcheck disable=SC1090
source "${ROS_SETUP}"

if [[ -f "${WS_SETUP}" ]]; then
    # shellcheck disable=SC1090
    source "${WS_SETUP}"
fi

export ROS_DOMAIN_ID="${ROS_DOMAIN_ID:-42}"

exec ros2 launch "$@"
