# Copyright (c) 2021-2024, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
#
# Docker file to build on x86_64
# https://docs.nvidia.com/deeplearning/frameworks/user-guide/index.html
# https://docs.nvidia.com/deeplearning/frameworks/support-matrix/index.html
ARG BASE_IMAGE=nvcr.io/nvidia/tritonserver:23.10-py3
FROM ${BASE_IMAGE}

# Store list of packages (must be first)
RUN mkdir -p /opt/nvidia/isaac_ros_dev_base && dpkg-query -W | sort > /opt/nvidia/isaac_ros_dev_base/x86_64-start-packages.csv

# disable terminal interaction for apt
ENV DEBIAN_FRONTEND=noninteractive
ENV SHELL /bin/bash
SHELL ["/bin/bash", "-c"]

# Ensure we have universe
RUN --mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get install -y \
        software-properties-common \
&& add-apt-repository universe \
&& apt-get update

# Add Isaac apt repository
RUN --mount=type=cache,target=/var/cache/apt \
    wget -qO - https://isaac.download.nvidia.com/isaac-ros/repos.key | apt-key add - && \
    grep -qxF "deb https://isaac.download.nvidia.com/isaac-ros/release-3 $(lsb_release -cs) release-3.0" /etc/apt/sources.list || \
    echo "deb https://isaac.download.nvidia.com/isaac-ros/release-3 $(lsb_release -cs) release-3.0" | tee -a /etc/apt/sources.list \
    && apt-get update

# Fundamentals
RUN --mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get install -y \
        apt-transport-https \
        bash-completion \
        build-essential \
        ca-certificates \
        clang-format \
        cmake \
        curl \
        git \
        git-lfs \
        gnupg2 \
        iputils-ping \
        locales \
        lsb-release \
        rsync \
        software-properties-common \
        wget \
        vim \
        unzip \
        mlocate \
	libgoogle-glog-dev

# Python basics
RUN --mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get install -y \
        python3-pip \
        python3-pybind11 \
        python3-pytest \
        python3-pytest-repeat \
        python3-pytest-rerunfailures \
        python3-pytest-cov

# Set Python3 as default
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 1

# Core dev libraries
RUN --mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get install -y \
        ffmpeg \
        libasio-dev \
        libbullet-dev \
        libtinyxml2-dev \
        libcunit1-dev \
        libmnl0 \
        libmnl-dev \
        libv4l-dev \
        libyaml-cpp-dev \
        libopencv-dev \
        libpython3.10 \
        libx264-dev \
        kmod \
        patchelf \
        python3-opencv \
        nlohmann-json3-dev

# Python3 (PIP)
RUN python3 -m pip install -U \
        argcomplete \
        autopep8 \
        flake8==4.0.1 \
        flake8-blind-except \
        flake8-builtins \
        flake8-class-newline \
        flake8-comprehensions \
        flake8-deprecated \
        flake8-docstrings \
        flake8-import-order \
        flake8-quotes \
        onnx \
        pydocstyle \
        scikit-learn \
        ninja \
        networkx \
        numpy \
        numpy-quaternion \
        pyyaml \
        setuptools_scm>=6.2 \
        trimesh \
        yourdfpy>=0.0.53 \
        warp-lang>=0.9.0 \
        scipy>=1.7.0 \
        tqdm \
        importlib_resources

# Install nvv4l2 for GXF Multimedia h264 codec
RUN --mount=type=cache,target=/var/cache/apt \
apt-get update && apt-get install -y \
        nvv4l2 \
&& ln -s /usr/lib/x86_64-linux-gnu/libnvcuvid.so.1 /usr/lib/x86_64-linux-gnu/libnvcuvid.so \
&& ln -s /usr/lib/x86_64-linux-gnu/libnvidia-encode.so.1 /usr/lib/x86_64-linux-gnu/libnvidia-encode.so

# Install VPI packages
ARG HAS_GPU="true"
RUN --mount=type=cache,target=/var/cache/apt \
    if [ "$HAS_GPU" = "true" ]; then \
	set -e ; \
        apt-key adv --fetch-key https://repo.download.nvidia.com/jetson/jetson-ota-public.asc ; \
        add-apt-repository "deb http://repo.download.nvidia.com/jetson/x86_64/$(lsb_release -cs) r36.3 main" ; \
        apt-get update ; \
        apt-get install libnvvpi3 vpi3-dev ; \
    fi

# Pytorch
RUN python3 -m pip install -U --extra-index-url https://download.pytorch.org/whl/cu121 \
        torch \
        torchvision \
        torchaudio

# Install Tao converter
RUN mkdir -p /opt/nvidia/tao && cd /opt/nvidia/tao && \
    wget --content-disposition 'https://api.ngc.nvidia.com/v2/resources/org/nvidia/team/tao/tao-converter/v5.1.0_8.6.3.1_x86/files?redirect=true&path=tao-converter' -O tao-converter && \
    chmod 755 tao-converter

ENV PATH="${PATH}:/opt/nvidia/tao"
ENV TRT_LIB_PATH="/usr/lib/x86_64-linux-gnu"
ENV TRT_INC_PATH="/usr/include/x86_64-linux-gnu"

# Update environment
ENV LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/opt/tritonserver/lib"

# Install CV-CUDA
RUN --mount=type=cache,target=/var/cache/apt \
    cd /tmp && \
    wget https://github.com/CVCUDA/CV-CUDA/releases/download/v0.5.0-beta/nvcv-lib-0.5.0_beta-cuda12-x86_64-linux.deb && \
    dpkg -i nvcv-lib-0.5.0_beta-cuda12-x86_64-linux.deb && \
    wget https://github.com/CVCUDA/CV-CUDA/releases/download/v0.5.0-beta/nvcv-dev-0.5.0_beta-cuda12-x86_64-linux.deb && \
    dpkg -i nvcv-dev-0.5.0_beta-cuda12-x86_64-linux.deb

# Add MQTT binaries and libraries
RUN --mount=type=cache,target=/var/cache/apt \
apt-add-repository ppa:mosquitto-dev/mosquitto-ppa \
&& apt-get update && apt-get install -y \
        mosquitto \
        mosquitto-clients

# Store list of packages (must be last)
RUN mkdir -p /opt/nvidia/isaac_ros_dev_base && dpkg-query -W | sort > /opt/nvidia/isaac_ros_dev_base/x86_64-end-packages.csv