#!/bin/bash

export ROS_HOSTNAME=$(hostname)
export ROS_HOME=${ROS_HOME:=$HOME_DIR/.ros}
export ROS_LOG_DIR="/tmp"
source /opt/ros/humble/setup.bash
pkill rm_watch_dog.sh # 杀掉看门狗

PID_FILE="$HOME/yq_2026/.rm_bringup.pid"

kill_process_tree() {
	local pid="$1"
	local child

	for child in $(pgrep -P "$pid" 2>/dev/null); do
		kill_process_tree "$child"
	done

	kill -TERM "$pid" 2>/dev/null || true
}

if [[ -f "$PID_FILE" ]]; then
	launch_pid=$(cat "$PID_FILE")
	if [[ -n "$launch_pid" ]] && kill -0 "$launch_pid" 2>/dev/null; then
		kill_process_tree "$launch_pid"
		sleep 2
		kill -KILL "$launch_pid" 2>/dev/null || true
	fi
	rm -f "$PID_FILE"
fi

ros2 daemon stop
ros2 daemon start
sleep 1
