#!/bin/bash
# entrypoint-ci.sh — CI container entrypoint for social-bot integration tests.
set -euo pipefail

source /opt/ros/humble/setup.bash
source /ros2_ws/install/setup.bash

# Start the ROS2 daemon in the background so node discovery works
ros2 daemon start 2>/dev/null || true

# Launch the test stack in the background using the test launch file
# (orchestrator + face + tracking + mock_sensors — no GPU speech/LLM/TTS)
echo "[CI] Launching social-bot test stack..."
ros2 launch saltybot_social_tests social_test.launch.py \
    enable_speech:=false \
    enable_llm:=false \
    enable_tts:=false \
    &
LAUNCH_PID=$!

# Give the stack up to 30s to come up
echo "[CI] Waiting for stack to initialise (max 30s)..."
sleep 10

# Run tests (command passed as args to this script)
echo "[CI] Running tests: $*"
"$@"
TEST_EXIT=$?

# Graceful shutdown
echo "[CI] Shutting down stack (PID $LAUNCH_PID)..."
kill -SIGTERM "$LAUNCH_PID" 2>/dev/null || true
sleep 5
kill -SIGKILL "$LAUNCH_PID" 2>/dev/null || true

exit $TEST_EXIT
