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


ROOT="/workspace"

sleep 30

set +u
source /opt/ros/humble/setup.bash
source "${ROOT}/install/setup.bash"
set -u

LOG_DIR="${ROOT}/autorun_logs/autorun_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$LOG_DIR"

kill_port_listeners() {
  local port="$1"
  local pids
  pids="$(ss -ltnp 2>/dev/null | grep ":${port} " | sed -E 's/.*pid=([0-9]+).*/\1/' | sort -u || true)"
  if [[ -n "${pids}" ]]; then
    echo "[run_all] port ${port} already in use, killing: ${pids}"
    kill ${pids} 2>/dev/null || true
    sleep 1
  fi
}

kill_port_listeners 9002

export TURTLEBOT3_MODEL=burger
ros2 launch turtlebot3_gazebo turtlebot3_office.launch.py > "${LOG_DIR}/tb3_gzsim.log" 2>&1 &
GZSIM_PID=$!
# gz sim -v 4 -s -r empty.sdf > "${LOG_DIR}/empty_gzsim.log" 2>&1 &
# GZSIM_PID=$!

echo "[autorun] tb3_gzsim pid=${GZSIM_PID}, log=${LOG_DIR}/tb3_gzsim.log"

sleep 60

if ps -p "$GZSIM_PID" > /dev/null; then
  echo "[autorun] tb3_gzsim is still running"
else
  echo "[autorun] tb3_gzsim exited early. Log:"
  cat "${LOG_DIR}/tb3_gzsim.log"
fi

echo "[autorun] starting websocket ..."

gz launch "${ROOT}/src/websocket.gzlaunch" > "${LOG_DIR}/websocket.log" 2>&1 &
WEBSOCKET_PID=$!

echo "[autorun] websocket pid=${WEBSOCKET_PID}, log=${LOG_DIR}/websocket.log"

sleep 10

if ps -p "$WEBSOCKET_PID" > /dev/null; then
  echo "[autorun] websocket is still running"
else
  echo "[autorun] websocket exited early. Log:"
  cat "${LOG_DIR}/websocket.log"
  exit 1
fi

# ===== Step 3: SLAM Toolbox (建图) =====
# echo "[autorun] starting slam_toolbox ..."

# ros2 launch slam_toolbox online_async_launch.py use_sim_time:=True > "${LOG_DIR}/slam.log" 2>&1 &
# SLAM_PID=$!

# echo "[autorun] slam pid=${SLAM_PID}, log=${LOG_DIR}/slam.log"

# sleep 15

# if ps -p "$SLAM_PID" > /dev/null; then
#   echo "[autorun] slam_toolbox is still running"
# else
#   echo "[autorun] slam_toolbox exited early. Log:"
#   cat "${LOG_DIR}/slam.log"
# fi

# ===== Step 4: Nav2 Navigation (导航) =====
# echo "[autorun] starting nav2_navigation ..."

# ros2 launch nav2_bringup navigation_launch.py use_sim_time:=True headless:=False > "${LOG_DIR}/nav2.log" 2>&1 &
# NAV2_PID=$!

# echo "[autorun] nav2 pid=${NAV2_PID}, log=${LOG_DIR}/nav2.log"

# sleep 15

# if ps -p "$NAV2_PID" > /dev/null; then
#   echo "[autorun] nav2 is still running"
# else
#   echo "[autorun] nav2 exited early. Log:"
#   cat "${LOG_DIR}/nav2.log"
# fi

# ===== Step 5: RViz2 (可视化) =====
# echo "[autorun] starting rviz2 ..."

# ros2 run rviz2 rviz2 -d /opt/ros/humble/share/nav2_bringup/rviz/nav2_default_view.rviz > "${LOG_DIR}/rviz2.log" 2>&1 &
# RVIZ_PID=$!

# echo "[autorun] rviz2 pid=${RVIZ_PID}, log=${LOG_DIR}/rviz2.log"

# sleep 5

# if ps -p "$RVIZ_PID" > /dev/null; then
#   echo "[autorun] rviz2 is still running"
# else
#   echo "[autorun] rviz2 exited early. Log:"
#   cat "${LOG_DIR}/rviz2.log"
# fi

echo "[autorun] all services started."
echo "[autorun] tb3_gzsim pid=${GZSIM_PID}"
echo "[autorun] websocket pid=${WEBSOCKET_PID}"
# echo "[autorun] slam pid=${SLAM_PID}"
# echo "[autorun] nav2 pid=${NAV2_PID}"
# echo "[autorun] rviz2 pid=${RVIZ_PID}"

wait
