FROM ros:humble-ros-base-jammy

# Prevent interactive prompts during apt installation
ENV DEBIAN_FRONTEND=noninteractive

# Install Turtlesim, TF2 ROS packages, Xvfb, VNC and noVNC for GUI access via web browser
RUN apt-get update && apt-get install -y \
    ros-humble-turtlesim \
    ros-humble-tf2-ros \
    ros-humble-tf2-geometry-msgs \
    ros-humble-teleop-twist-keyboard \
    xvfb \
    x11vnc \
    fluxbox \
    novnc \
    websockify \
    supervisor \
    python3-pip \
    python3-colcon-common-extensions \
    git \
    && rm -rf /var/lib/apt/lists/*

# Set up work directory and environment variables
ENV DISPLAY=:1
ENV RESOLUTION=1280x800x24
WORKDIR /ros2_ws

# Setup bash environment for interactive sessions
RUN echo "source /opt/ros/humble/setup.bash" >> /root/.bashrc

# Copy supervisord configuration and entrypoint script
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY entrypoint.sh /entrypoint.sh
# Strip Windows line endings (Git on Windows may check out CRLF), otherwise the shebang fails
RUN sed -i 's/\r$//' /entrypoint.sh && chmod +x /entrypoint.sh

# Expose noVNC web port (8080) and VNC port (5900)
EXPOSE 8080 5900

ENTRYPOINT ["/entrypoint.sh"]
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
