ARG BASE_IMAGE=nvidia/opengl:1.2-glvnd-devel-ubuntu20.04
FROM ${BASE_IMAGE}

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

ENV TZ "America/New_York"

# Not necessary to mkdir. only serves as a list of dirs we touch.
RUN mkdir -p \
    /opt \
    /opt/parat/build \
    /opt/parat/src \
    /opt/parat/install \
    /opt/scripts \
    /opt/spack \
    /opt/mochi-spackages

WORKDIR /opt
COPY scripts/spack.config.sh scripts/spack.config.sh

# -----------------------------------------------------------------------------
# Build dependencies
# -----------------------------------------------------------------------------
# Installs some needed dependencies with system package manager.
# Freeze versions for packages listed in docker/scripts/spack.config.sh
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 \
        autoconf=2.69-11.1 \
        automake=1:1.16.1-4ubuntu6 \
        build-essential \
        ca-certificates \
        chrpath \
        curl \
        gfortran \
        git \
        libboost-dev=1.71.0.0ubuntu2 \
        libcereal-dev=1.3.0-2 \
        libjson-c-dev=0.13.1+dfsg-7ubuntu0.3 \
        libpkgconf-dev=1.6.3-5 \
        libsigsegv-dev=2.12-2 \
        libtool=2.4.6-14 \
        m4=1.4.18-4 \
        nasm \
        ninja-build \
        openssl \
        perl=5.30.0-9ubuntu0.2 && \
apt-get install -y --no-install-recommends python3-dev python3-pip python3-setuptools && \
update-alternatives --install /usr/bin/python python /usr/bin/python3 1 && \
rm -rf /var/lib/apt/lists/*

# Get cmake
RUN mkdir -p /opt/cmake && cd /opt/cmake && \
    curl -L https://cmake.org/files/v3.17/cmake-3.17.2-Linux-x86_64.tar.gz | tar --strip-components=1 -xzv

ENV PATH="/opt/cmake/bin:/opt/parat/build/bin:/opt/spack/bin:${PATH}"

# -----------------------------------------------------------------------------
# Spack + Thallium
# -----------------------------------------------------------------------------
# pin these versions instead of building off of the respective project's main or dev branch.
ENV spec="mochi-thallium@0.10.1 ^libfabric fabrics=tcp,rxm,sockets ^mochi-margo@0.9.10 ^mercury@2.2.0"
ENV SPACKAGE_CONFIG="parat/spack-config"

RUN sh scripts/spack.config.sh

# Get spack
RUN git clone --depth 1 https://github.com/spack/spack "spack"

# Search for a compiler.
RUN spack compiler find

# Clone mochi package repo.
RUN git clone --depth 1 https://github.com/mochi-hpc/mochi-spack-packages "mochi-spackages"

# Add mochi repo to spack.
RUN spack repo add "mochi-spackages"

# inspect spec
RUN spack -C "${SPACKAGE_CONFIG}" spec ${spec} && \
    spack -C "${SPACKAGE_CONFIG}" install ${spec} && \
    spack -C "${SPACKAGE_CONFIG}" clean

# -----------------------------------------------------------------------------
# Parat Project
# -----------------------------------------------------------------------------
# Gets the project from git repo.
RUN git clone https://gitlab.kitware.com/async/paraview.git "parat/src" && \
  cd parat/src && \
  git submodule update --init --recursive

# Configures the project with cmake.
RUN . spack/share/spack/setup-env.sh && \
  spack load mochi-thallium && \
  cmake -GNinja \
  -S parat/src \
  -B parat/build \
  -D CMAKE_BUILD_TYPE=RelWithDebInfo \
  -D PARAVIEW_BUILD_TESTING=WANT \
  -D PARAVIEW_USE_PYTHON=ON \
  -D PARAVIEW_USE_QT=OFF \
  -D PARAVIEW_USE_VTKM=OFF \
  -D OpenGL_GL_PREFERENCE=GLVND \
  -D VTK_USE_X=False \
  -D VTK_OPENGL_HAS_EGL=True \
  -D VTK_DEBUG_LEAKS=ON

# Builds the project with ninja.
RUN . spack/share/spack/setup-env.sh && \
  spack load mochi-thallium && \
  cmake --build parat/build

# Install the project.
RUN . spack/share/spack/setup-env.sh && \
  spack load mochi-thallium && \
  cmake --install parat/build --prefix parat/install

ENTRYPOINT [ "/bin/bash" ]
