#!/bin/bash
# ==============================================================================
# CONFIGURATION SCRIPT FOR MICRO-ROS AGENT IN DOCKER
# To be executed on the host machine
# ==============================================================================

# Stops the script if any error is detected
set -e

echo "======================================================="
echo "1/4: Setting up the shared workspace for the micro-ROS agent..."
echo "======================================================="
WS_PATH="$PWD/docker_ros2_ws"
mkdir -p "$WS_PATH/src"

echo "======================================================="
echo "2/4: Creating the internal automation script..."
echo "======================================================="
# We create a temporary script that Docker will execute as soon as it starts, 
# this script will handle all the installation and configuration inside the container.
cat << 'EOF' > "$WS_PATH/inside_setup.sh"
#!/bin/bash
set -e

echo "--- [DOCKER] System update and installation of core tools ---"
apt update && apt install -y python3-pip git vim tmux

echo "--- [DOCKER] Cloning micro_ros_setup (Branch: humble) ---"
cd /ros2_ws
if [ ! -d "src/micro_ros_setup" ]; then
   git clone -b humble https://github.com/micro-ROS/micro_ros_setup.git src/micro_ros_setup
else
   echo "Repository already exists, skipping step."
fi

echo "--- [DOCKER] Initializing and updating rosdep ---"
rosdep update
rosdep install --from-paths src --ignore-src -y

echo "--- [DOCKER] Building the workspace base ---"
source /opt/ros/humble/setup.bash
colcon build

echo "--- [DOCKER] Downloading Agent source files ---"
source install/local_setup.bash
ros2 run micro_ros_setup create_agent_ws.sh

echo "--- [DOCKER] Final compilation of the actual Agent (This step takes time) ---"
ros2 run micro_ros_setup build_agent.sh

echo "--- [DOCKER] Saving configuration to container's bashrc ---"
if ! grep -q "source /ros2_ws/install/local_setup.bash" ~/.bashrc; then
   echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
   echo "source /ros2_ws/install/local_setup.bash" >> ~/.bashrc
fi

echo "======================================================="
echo " COMPILATION COMPLETED SUCCESSFULLY INSIDE DOCKER!"
echo "======================================================="
echo "To launch the agent now, run:"
echo "ros2 run micro_ros_agent micro_ros_agent serial --dev /dev/ttyACM0"
echo "======================================================="

# Let the terminal open in Docker so the user can run commands after the setup is done
bash
EOF

chmod +x "$WS_PATH/inside_setup.sh"

echo "======================================================="
echo "3/4: Deploying Docker container (ros:humble)..."
echo "======================================================="
# We launch the container with full access to USB devices (/dev) and the network (--net=host)
# It automatically runs the inside_setup.sh script we just created
docker run -it \
--net=host \
--privileged \
-v /dev:/dev \
-v "$WS_PATH:/ros2_ws" \
--name uros_agent_container \
ros:humble \
/ros2_ws/inside_setup.sh
