ARG BASE_IMAGE=nvidia/opengl:1.2-glvnd-runtime-ubuntu20.04
ARG DEV_IMAGE=paraview-async-dev
ARG NV_IMAGE=nvidia/opengl:1.2-glvnd-devel-ubuntu20.04

FROM ${DEV_IMAGE} AS devimage
FROM ${NV_IMAGE} AS nv_image
FROM ${BASE_IMAGE}

LABEL maintainer="Jaswant Panchumarti <jaswant.panchumarti@kitware.com>"

ENV TZ "America/New_York"

# -----------------------------------------------------------------------------
# System package
# -----------------------------------------------------------------------------
RUN apt-get update  && \
  DEBIAN_FRONTEND="noninteractive" apt-get install -y tzdata && \
  ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime && \
  dpkg-reconfigure --frontend noninteractive tzdata && \
  apt-get install -y --no-install-recommends \
    ca-certificates \
    libatomic1 \
    libjson-c-dev=0.13.1+dfsg-7ubuntu0.3 \
    libsigsegv-dev=2.12-2 \
    openssl &&\
  apt-get install -y --no-install-recommends python3-dev python3.8-venv python3-pip python3-setuptools && \
  update-alternatives --install /usr/bin/python python /usr/bin/python3 1 && \
  rm -rf /var/lib/apt/lists/*

# Trame for web app.
RUN python3 -m venv /opt/paraview-async/runtime/.pvenv && \
  . /opt/paraview-async/runtime/.pvenv/bin/activate && \
  pip install -U pip && \
  pip install trame

# Populate paraview async runtime, data.
COPY --from=devimage  /opt/scripts                           /opt/scripts
# Preserve spack directory structure, but do not copy sources, the `rpath` of libvtkServicesThallium contains `/opt/spack/opt/spack` entries.
COPY --from=devimage  /opt/spack/opt/spack                   /opt/spack/opt/spack
COPY --from=devimage  /opt/paraview-async/build/ExternalData /opt/paraview-async/data
COPY --from=devimage  /opt/paraview-async/install            /opt/paraview-async/runtime

# Put them at a place runtime can find.
COPY --from=nv_image /usr/lib/x86_64-linux-gnu/libOpenGL.so.0.0.0  /usr/lib/x86_64-linux-gnu/libOpenGL.so.0.0.0
COPY --from=nv_image /usr/lib/x86_64-linux-gnu/libOpenGL.so.0      /usr/lib/x86_64-linux-gnu/libOpenGL.so.0
COPY --from=nv_image /usr/lib/x86_64-linux-gnu/libOpenGL.so        /usr/lib/x86_64-linux-gnu/libOpenGL.so

# Copy the server script.
COPY docker/scripts/server.sh /opt/scripts/server.sh
# Copy the python examples.
COPY python /opt/paraview-async/apps

ENV PARAVIEW_ASYNC_DATA_DIR="/opt/paraview-async/data"
ENV PARAVIEW_ASYNC_RUNTIME_DIR="/opt/paraview-async/runtime"
ENV PARAVIEW_ASYNC_VENV_DIR="/opt/paraview-async/runtime/.pvenv"
ENV PARAVIEW_ASYNC_WEB_APP_DIR="/opt/paraview-async/apps"

ENTRYPOINT [ "/bin/bash", "/opt/scripts/server.sh" ]
