#!/usr/bin/env bash
# Quick build + run helper for the Jetson ROS2 stack
# Usage: bash build-and-run.sh [build|up|down|logs|shell]

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
JETSON_DIR="$(dirname "$SCRIPT_DIR")"

cd "$JETSON_DIR"

ACTION="${1:-up}"

case "$ACTION" in
    build)
        echo "[saltybot] Building ROS2 Docker image..."
        docker compose build --no-cache
        ;;
    up)
        echo "[saltybot] Starting ROS2 stack..."
        docker compose up -d
        docker compose logs -f
        ;;
    down)
        echo "[saltybot] Stopping ROS2 stack..."
        docker compose down
        ;;
    logs)
        docker compose logs -f
        ;;
    shell)
        echo "[saltybot] Opening shell in ros2 container..."
        docker compose exec saltybot-ros2 bash
        ;;
    slam)
        echo "[saltybot] Launching SLAM only..."
        docker compose up -d rplidar realsense saltybot-ros2
        docker compose logs -f saltybot-ros2
        ;;
    status)
        docker compose ps
        echo ""
        echo "--- ROS2 topics (requires running container) ---"
        docker compose exec saltybot-ros2 \
            bash -c "source /opt/ros/humble/setup.bash && ros2 topic list" 2>/dev/null || true
        ;;
    *)
        echo "Usage: $0 [build|up|down|logs|shell|slam|status]"
        exit 1
        ;;
esac
