# ---- BUILD ----
FROM nvidia/opengl:1.0-glvnd-devel-ubuntu20.04 AS builder

# Set environment for packages part and install packages
ENV DEBIAN_FRONTEND="noninteractive" TZ="Europe/Paris"
RUN apt-get update && apt-get install -y \
    cmake \
    freeglut3-dev \
    gcc \
    g++ \
    && rm -rf /var/lib/apt/lists/*

# Copy necessary files from host
ADD arch-Linux-x86_64-gcc9-release/ /OpenInventor/arch-Linux-x86_64-gcc9-release/
ADD examples/ /OpenInventor/examples/
ADD source/ /OpenInventor/source/
ADD include/ /OpenInventor/include/
# Copy your own password.dat in the License folder
ADD License/ /OpenInventor/License/

# Set environment variables
ENV OIVHOME /OpenInventor
ENV OIVARCH "arch-Linux-x86_64-gcc9-release"
ENV LD_LIBRARY_PATH ${OIVHOME}/${OIVARCH}/lib


WORKDIR /OpenInventor/examples
RUN mkdir build && cd build && cmake -D CMAKE_BUILD_TYPE=Release .. && make -j9

# ---- RUN ----
FROM nvidia/opengl:1.0-glvnd-runtime-ubuntu20.04

# The NVIDIA_DRIVER_CAPABILITIES controls which driver libraries/binaries will be mounted inside the container.
# OpenInventor is using OpenGL (graphics).
# If you want to use H.264 hardware encoding, RemoteViz needs CUDA and NVIDIA's Video Codec SDK (compute, video).
# https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/user-guide.html#driver-capabilities
ENV NVIDIA_VISIBLE_DEVICES all
ENV NVIDIA_DRIVER_CAPABILITIES graphics,compute,video,utility

# Set environment for packages part and install packages
ENV DEBIAN_FRONTEND="noninteractive" TZ="Europe/Paris"
RUN apt-get update && apt-get install -y \
    libglu1-mesa \
    libopengl0 \
    libgomp1 \
    pciutils \
    libva-drm2 \
    libicu66 \
    && rm -rf /var/lib/apt/lists/*
# The package libva-drm2 is required for enabling RemoteViz video encoding

# Copy necessary files from the builder image
COPY --from=builder /OpenInventor/arch-Linux-x86_64-gcc9-release/lib /OpenInventor/arch-Linux-x86_64-gcc9-release/lib
COPY --from=builder /OpenInventor/License /OpenInventor/License
COPY --from=builder /OpenInventor/examples/bin/arch-Linux-x86_64-gcc9-release/RemoteViz /apps/RemoteViz

# Set environment variables
ENV OIVHOME /OpenInventor
ENV OIVARCH "arch-Linux-x86_64-gcc9-release"
ENV LD_LIBRARY_PATH ${OIVHOME}/${OIVARCH}/lib
ENV OIV_LICENSE_FILE /OpenInventor/License/password.dat

EXPOSE 8080

CMD ["/apps/RemoteViz/RemoteVizHelloConeH264RenderingService", "0.0.0.0", "8080"]
