FROM osrf/ros:noetic-desktop-full

# Set up environment variables
ENV DEBIAN_FRONTEND=noninteractive
ENV CATKIN_WS=/root/limo_cobot_ws
ENV DISPLAY=:0
ENV RESOLUTION=1920x1080

# Install core build dependencies, ROS Noetic tools, and GUI/VNC utilities
RUN apt-get update && apt-get install -y \
    python3-rosdep python3-vcstool python3-colcon-common-extensions \
    ros-noetic-gazebo-ros ros-noetic-robot-state-publisher \
    ros-noetic-joint-state-publisher-gui ros-noetic-xacro \
    ros-noetic-twist-mux ros-noetic-moveit \
    ros-noetic-ros-control ros-noetic-ros-controllers \
    ros-noetic-gazebo-ros-control ros-noetic-joint-state-controller \
    ros-noetic-effort-controllers ros-noetic-position-controllers \
    ros-noetic-joint-trajectory-controller \
    build-essential git curl gnupg2 lsb-release supervisor xvfb x11vnc fluxbox novnc net-tools \
    && rm -rf /var/lib/apt/lists/*

# ROS 2 Foxy and the ROS 1 <-> ROS 2 bridge: the simulation runs on ROS 1 (Noetic), the
# practical code uses ROS 2 (rclpy) like the real LIMO. Python packages for the student code.
RUN curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg \
    && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu focal main" > /etc/apt/sources.list.d/ros2.list \
    && apt-get update && apt-get install -y \
    ros-foxy-ros-base ros-foxy-ros1-bridge \
    python3-numpy python3-opencv python3-pytest \
    && rm -rf /var/lib/apt/lists/*

# Symlink noVNC index file
RUN ln -s /usr/share/novnc/vnc.html /usr/share/novnc/index.html

# Set up ROS workspace and clone LIMO Cobot simulation repository
RUN mkdir -p $CATKIN_WS/src
WORKDIR $CATKIN_WS/src
RUN git clone https://github.com/agilexrobotics/limo_cobot_sim.git

# Initialize rosdep and install workspace dependencies
WORKDIR $CATKIN_WS
RUN rosdep update || true
RUN apt-get update && rosdep install --from-paths src --ignore-src -r -y && rm -rf /var/lib/apt/lists/*

# Build the catkin workspace
RUN /bin/bash -c "source /opt/ros/noetic/setup.bash && catkin_make"

# Setup bash environment for interactive sessions (ROS 1; for ROS 2 use run_in_sim.sh)
RUN echo "source /opt/ros/noetic/setup.bash" >> /root/.bashrc
RUN echo "source $CATKIN_WS/devel/setup.bash" >> /root/.bashrc

# World with the yellow cube on a pedestal
COPY worlds /root/sim_worlds

# Copy supervisor configuration file
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Start Supervisord
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
