FROM ubuntu:24.04

ARG SETUPTOOLS_SCM_PRETEND_VERSION

ENV DEBIAN_FRONTEND=noninteractive
ENV ROS_DISTRO=jazzy

SHELL ["/bin/bash", "-c"]

# Base utilities and locale
RUN apt-get update && apt-get install -y --no-install-recommends \
    locales \
    curl \
    gnupg \
    lsb-release \
    ca-certificates \
    libpcap-dev \
    python3-pip \
    build-essential \
    cmake \
    git \
    && rm -rf /var/lib/apt/lists/*
RUN locale-gen en_US en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LC_ALL=en_US.UTF-8

# ROS 2 apt repo
RUN curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key \
    -o /usr/share/keyrings/ros-archive-keyring.gpg
RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] \
    http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" \
    | tee /etc/apt/sources.list.d/ros2.list

# ROS 2 and colcon tooling
RUN apt-get update && apt-get install -y --no-install-recommends \
    ros-${ROS_DISTRO}-ros-base \
    python3-colcon-common-extensions \
    python3-rosdep \
    python3-argcomplete \
    python3-venv \
    && rm -rf /var/lib/apt/lists/*

# Initialize rosdep (safe to re-run in container build)
RUN rosdep init && rosdep update

# Cyphal regulated data types
RUN mkdir -p /home/user/.cyphal && \
    git clone --depth 1 https://github.com/OpenCyphal/public_regulated_data_types.git /home/user/.cyphal/public_regulated_data_types

# Python requirements
WORKDIR /home/user
RUN python3 -m venv .venv && source ".venv/bin/activate" && python3 -m pip install numpy==2.3.5 empy catkin_pkg lark pycyphal yakut nunavut pytest pytest-dependency

# Environment setup on shell entry (login and non-login)
COPY tools/docker/ros2_cyphal_env.sh /home/user/tools/docker/ros2_cyphal_env.sh
COPY tools/docker/entrypoint.sh /home/user/tools/docker/entrypoint.sh
RUN cp /home/user/tools/docker/ros2_cyphal_env.sh /etc/profile.d/ros2_cyphal.sh
RUN echo "source /home/user/tools/docker/ros2_cyphal_env.sh" >> /root/.bashrc
RUN source /home/user/tools/docker/ros2_cyphal_env.sh
RUN chmod +x /home/user/tools/docker/entrypoint.sh

COPY . /home/user/ros2_cyphal_bridge
RUN source "/home/user/.venv/bin/activate" && \
    SETUPTOOLS_SCM_PRETEND_VERSION="${SETUPTOOLS_SCM_PRETEND_VERSION}" \
    python3 -m pip install -e /home/user/ros2_cyphal_bridge --no-build-isolation
RUN source "/home/user/tools/docker/ros2_cyphal_env.sh" && \
    python3 /home/user/ros2_cyphal_bridge/tools/docker/compile_dsdl.py

WORKDIR /home/user/ros2_cyphal_bridge/tools/docker

ENTRYPOINT ["/home/user/tools/docker/entrypoint.sh"]
CMD ["/bin/bash"]
