# Jetson Orin Nano Super — ROS2 Humble dev container
# Base: JetPack 6 (L4T R36.2.0) + CUDA 12.x / Ubuntu 22.04
#
# Hardware: Jetson Orin Nano Super 8GB (67 TOPS, 1024-core Ampere)
# Previous: Jetson Nano 4GB (JetPack 4.6 / L4T R32.6.1) — see git history

FROM nvcr.io/nvidia/l4t-jetpack:r36.2.0

LABEL maintainer="sl-perception <saltylab>"
LABEL description="ROS2 Humble + SLAM stack for Jetson Orin Nano Super self-balancing robot"
LABEL jetpack="6.0"
LABEL l4t="r36.2.0"
LABEL ros_distro="humble"

ENV DEBIAN_FRONTEND=noninteractive
ENV ROS_DISTRO=humble
ENV ROS_ROOT=/opt/ros/${ROS_DISTRO}
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

# ── Locale ─────────────────────────────────────────────────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
    locales tzdata \
    && locale-gen en_US.UTF-8 \
    && rm -rf /var/lib/apt/lists/*
ENV LANG=en_US.UTF-8

# ── System deps ─────────────────────────────────────────────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential cmake git wget curl \
    python3-dev python3-pip python3-setuptools python3-wheel \
    i2c-tools libi2c-dev python3-smbus \
    picocom minicom setserial \
    usbutils libusb-1.0-0-dev \
    htop tmux nano \
    net-tools iputils-ping \
    && rm -rf /var/lib/apt/lists/*

# ── ROS2 Humble (Ubuntu 22.04 native — standard apt, no ARM64 workarounds) ─────
RUN curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc \
        | gpg --dearmor -o /usr/share/keyrings/ros-archive-keyring.gpg && \
    echo "deb [arch=arm64 signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] \
        http://packages.ros.org/ros2/ubuntu jammy main" \
        > /etc/apt/sources.list.d/ros2.list && \
    apt-get update && apt-get install -y --no-install-recommends \
        ros-humble-ros-base \
        ros-humble-rmw-cyclonedds-cpp \
        ros-dev-tools \
        python3-colcon-common-extensions \
        python3-rosdep \
    && rosdep init && rosdep update \
    && rm -rf /var/lib/apt/lists/*

# ── SLAM / Nav / Sensor packages ───────────────────────────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
    ros-humble-rtabmap-ros \
    ros-humble-slam-toolbox \
    ros-humble-nav2-bringup \
    ros-humble-robot-localization \
    ros-humble-rplidar-ros \
    ros-humble-realsense2-camera \
    ros-humble-realsense2-description \
    ros-humble-tf2-tools \
    ros-humble-robot-state-publisher \
    ros-humble-rqt \
    ros-humble-rqt-common-plugins \
    ros-humble-rviz2 \
    ros-humble-rosbridge-server \
    ros-humble-image-transport-plugins \
    ros-humble-v4l2-camera \
    && rm -rf /var/lib/apt/lists/*

# ── GPIO / serial Python libs ───────────────────────────────────────────────────
RUN pip3 install --no-cache-dir \
    Jetson.GPIO pyserial smbus2 numpy scipy

# ── RealSense SDK (librealsense2 ARM64 for JetPack 6) ─────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
    libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev \
    && rm -rf /var/lib/apt/lists/*

RUN pip3 install --no-cache-dir pyrealsense2

# ── Workspace setup ────────────────────────────────────────────────────────────
RUN mkdir -p /ros2_ws/src
WORKDIR /ros2_ws

RUN echo "source /opt/ros/${ROS_DISTRO}/setup.bash" >> /root/.bashrc && \
    echo "source /ros2_ws/install/local_setup.bash 2>/dev/null || true" >> /root/.bashrc && \
    echo "export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp" >> /root/.bashrc

# ── Entrypoint ─────────────────────────────────────────────────────────────────
COPY scripts/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
CMD ["bash"]
