FROM debian:latest
MAINTAINER Tristan Salles

# install things
RUN apt-get update -qq && \
    DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
        bash-completion \
        build-essential \
        git \
        python3-minimal \
        python3-dev \
        python3-pip \
        libxml2-dev \
        xorg-dev \
        ssh \
        curl \
        libfreetype6-dev \
        libpng-dev \
        libxft-dev \
        xvfb \
        python3-tk \
        mesa-utils \
        rsync \
        vim \
        less \
        xauth \
        swig \
        gdb-minimal \
        python3-dbg \
        cmake \
        python3-setuptools \
        wget \
        gfortran  && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

ARG MPICH_VERSION="3.4.3"
ARG MPICH_CONFIGURE_OPTIONS="--enable-fast=all,O3 --prefix=/opt/mpich --with-device=ch3"
ARG MPICH_MAKE_OPTIONS="-j4"
WORKDIR /tmp/mpich-build
RUN wget http://www.mpich.org/static/downloads/${MPICH_VERSION}/mpich-${MPICH_VERSION}.tar.gz && \
    tar xvzf mpich-${MPICH_VERSION}.tar.gz && \
    cd mpich-${MPICH_VERSION}              && \
    FFLAGS=-fallow-argument-mismatch ./configure ${MPICH_CONFIGURE_OPTIONS} && \
    make ${MPICH_MAKE_OPTIONS}             && \
    make install                           && \
    ldconfig                               && \
    cd /tmp                                && \
    rm -fr *

ENV MPI_DIR=/opt/mpich
ENV PATH=${MPI_DIR}/bin:$PATH

ENV LANG=C.UTF-8
# Install setuptools and wheel first, needed by plotly
RUN pip3 install -U setuptools  && \
    pip3 install -U wheel       && \
    pip3 install --no-cache-dir packaging \
        appdirs \
        numpy \
        jupyter \
        plotly \
        matplotlib \
        pillow \
        pyvirtualdisplay \
        ipython \
        ipyparallel \
        pint \
        sphinx \
        sphinx_rtd_theme \
        sphinxcontrib-napoleon \
        mock \
        scipy \
        tabulate && \
    env MPICC=${MPI_DIR}/mpicc MPICXX=${MPI_DIR}/mpicxx MPIFC=${MPI_DIR}/mpifort python3 -m pip install --no-cache-dir mpi4py

RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 1

WORKDIR /tmp/petsc-build
ARG PETSC_VERSION="3.16.1"
RUN wget http://ftp.mcs.anl.gov/pub/petsc/release-snapshots/petsc-lite-${PETSC_VERSION}.tar.gz && \
    tar zxf petsc-lite-${PETSC_VERSION}.tar.gz && cd petsc-${PETSC_VERSION}   && \
    ./configure --with-debugging=0 --prefix=/opt/petsc                     \
                --COPTFLAGS="-g -O3" --CXXOPTFLAGS="-g -O3" --FOPTFLAGS="-g -O3" \
                --with-zlib=1                   \
                --download-fblaslapack=1        \
                --download-ctetgen=1            \
                --download-triangle=1           \
                --download-hdf5=1               \
                --download-mumps=1              \
                --download-parmetis=1           \
                --download-eigen=1              \
                --download-metis=1              \
                --download-hypre=1              \
                --download-scalapack=1          \
                --download-pragmatic=1          \
                --useThreads=1                  \
                --with-shared-libraries         \
                --with-cxx-dialect=C++11     && \
    make PETSC_DIR=/tmp/petsc-build/petsc-${PETSC_VERSION} PETSC_ARCH=arch-linux-c-opt all     && \
    make PETSC_DIR=/tmp/petsc-build/petsc-${PETSC_VERSION} PETSC_ARCH=arch-linux-c-opt install && \
    make PETSC_DIR=/opt/petsc PETSC_ARCH="" check && \
    cd /tmp && \
    rm -fr *

ENV PETSC_DIR=/opt/petsc
ENV PATH=${PETSC_DIR}/bin:$PATH

ENV PYTHONPATH $PYTHONPATH:/usr/lib
#RUN CC=h5pcc HDF5_MPI="ON" HDF5_DIR=${PETSC_DIR} python3 -m pip install --no-cache-dir --no-binary=h5py h5py && \
RUN python3 -m pip install --no-cache-dir h5py
#==2.10.0

RUN python3 -m pip install --no-cache-dir petsc4py==3.16.1

# install extras in a new layer
RUN python3 -m pip install --no-cache-dir \
        ipython \
        jupyter

# Install Tini.. this is required because CMD (below) doesn't play nice with notebooks for some reason: https://github.com/ipython/ipython/issues/7062, https://github.com/jupyter/notebook/issues/334
RUN curl -L https://github.com/krallin/tini/releases/download/v0.10.0/tini > tini && \
    echo "1361527f39190a7338a0b434bd8c88ff7233ce7b9a4876f3315c22fce7eca1b0 *tini" | sha256sum -c - && \
    mv tini /usr/local/bin/tini && \
    chmod +x /usr/local/bin/tini

# expose notebook port
EXPOSE 8888

RUN python3 -m pip install --no-cache-dir setuptools wheel && \
    python3 -m pip install --no-cache-dir \
    pathlib \
    meshio>=4.2.2 \
    shapely \
    meshplex>=0.13.3 \
    ruamel.yaml>=0.16.12 \
    numpy>=1.22.2 \
    scipy>=1.5.2 \
    Cython>=0.29.21 \
    h5py>=2.10.0 \
    pandas>=1.1.2  \
    meshplex>=0.15.14 \
    # scikit-fuzzy>=0.4.2 \
    pre-commit>=2.7.1 \
    vtk

# create a volume
RUN mkdir /live && \
    mkdir /live/lib

VOLUME /live/share
WORKDIR /live

EXPOSE 8888
ENV TINI_VERSION v0.18.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/local/bin/tini
RUN chmod +x /usr/local/bin/tini
ENTRYPOINT ["/usr/local/bin/tini", "--"]

EXPOSE 9999
# script for xvfb-run.  all docker commands will effectively run under this via the entrypoint
RUN printf "#\041/bin/sh \n rm -f /tmp/.X99-lock && xvfb-run -s '-screen 0 1600x1200x16' \$@" >> /usr/local/bin/xvfbrun.sh && \
chmod +x /usr/local/bin/xvfbrun.sh

# Add Tini
EXPOSE 8888
ENV TINI_VERSION v0.18.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/local/bin/tini
RUN chmod +x /usr/local/bin/tini
ENTRYPOINT ["/usr/local/bin/tini", "--"]

EXPOSE 9999

# install JIGSAW
RUN git clone https://github.com/dengwirda/jigsaw-python.git  && \
cd jigsaw-python && \
python3 setup.py build_external && \
python3 setup.py install

# note we use xvfb which to mimic the X display
ENTRYPOINT ["/usr/local/bin/xvfbrun.sh"]

# launch notebook
#CMD ["jupyter", "notebook", " --no-browser", "--allow-root", "--ip=0.0.0.0", "--NotebookApp.iopub_data_rate_limit=1.0e10"]

ADD run-jupyter.sh /usr/local/bin/run-jupyter.sh
RUN chmod +x /usr/local/bin/run-jupyter.sh
CMD /usr/local/bin/run-jupyter.sh
