#!/usr/bin/env bash
set -euo pipefail

# Gazebo Sim 8 + gz-launch WebSocket :9002 for gzweb.
# Uses office_gz_dartsim.sdf (mesh collisions stripped; dartsim-safe) so the full office scene renders.

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORLD="${ROOT}/turtlebot3_simulations/turtlebot3_gazebo/worlds/office_gz_dartsim.sdf"
WS_CONFIG="${ROOT}/websocket.sdf"
PHYSICS="${PHYSICS_ENGINE:-gz-physics-dartsim-plugin}"

export GZ_SIM_RESOURCE_PATH="${HOME}/.gazebo/models:${ROOT}/turtlebot3_simulations/turtlebot3_gazebo/models/turtlebot3_office:${ROOT}/turtlebot3_simulations/turtlebot3_gazebo/models"

gz sim -s -r --physics-engine "${PHYSICS}" "${WORLD}" &
SIM_PID=$!

cleanup() {
  kill "${SIM_PID}" 2>/dev/null || true
}
trap cleanup EXIT INT TERM

sleep 2
exec gz launch "${WS_CONFIG}"
