FROM ubuntu:18.04

ARG DEBIAN_FRONTEND=noninteractive

ENV TERM linux

RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

RUN apt-get update && \
    apt-get -y --no-install-recommends install gawk && \
    update-alternatives --install /usr/bin/awk awk /usr/bin/gawk 9999 && \
    rm -rf /var/lib/apt/lists/*

RUN apt-get update && \
    apt-get purge openjdk-\* icedtea-\* icedtea6-\* && \
    apt-get remove openjdk-11-jre openjdk-11-jdk openjdk-11-jre-headless openjdk-11-jdk-headless && \
    apt-get purge openjdk-* && \
    apt-get install -y --no-install-recommends openjdk-8-jdk && \
    update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 9999 && \
    rm -rf /var/lib/apt/lists/*

# Install Packages
RUN apt-get update && \
    apt-get install -y --no-install-recommends apt-utils && \
    apt-get install -y --no-install-recommends \
    git  \
    vim \
    wget \
    sudo \
    openssh-server \
    openssh-client && \
    yes yes | ssh-keygen -f /root/.ssh/id_rsa -t rsa -N '' > /dev/null && \
    cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys && \
    git config --global core.compression 9 && \
# =============================================================================
# Dependencies for building COMPSs
# =============================================================================
# Build dependencies
    sudo apt-get install -y --no-install-recommends maven \
# Runtime dependencies
    openjdk-8-jdk graphviz xdg-utils \
# Bindings-common-dependencies
    libtool automake build-essential \
# C-binding dependencies
    libboost-all-dev libxml2-dev csh \
# Extrae dependencies
    libxml2 gfortran libpapi-dev papi-tools \
# Misc. dependencies
    openmpi-bin openmpi-doc libopenmpi-dev uuid-runtime curl bc \
# Python-binding dependencies
    python-dev python3-dev libpython2.7 python-pip python3-pip python-setuptools python3-setuptools && \
    pip2 install wheel && \
    pip3 install wheel && \
    pip2 install wheel numpy==1.15.4 dill guppy decorator mpi4py==1.3.1 && \
    pip3 install wheel numpy==1.15.4 dill decorator mpi4py==3.0.1 && \
# Python-redis dependencies
    pip2 install redis==2.10.6 redis-py-cluster && \
    pip3 install redis==2.10.6 redis-py-cluster && \
# pycompsslib dependencies
    pip2 install scipy==1.0.0 scikit-learn==0.19.1 pandas==0.23.1 && \
    pip3 install scipy==1.0.0 scikit-learn==0.19.1 pandas==0.23.1 && \
# Streaming dependencies
    apt-get install -y --no-install-recommends unzip && \
    wget https://services.gradle.org/distributions/gradle-5.4.1-bin.zip && \
    unzip -d /opt gradle-5.4.1-bin.zip && \
    rm gradle-5.4.1-bin.zip && \
    ln -s /opt/gradle-5.4.1 /opt/gradle && \
    echo "GRADLE_HOME=/opt/gradle" >> /etc/environment && \
# Testing dependencies
    pip3 install enum34 tabulate && \
# Jupyter dependencies
    pip2 install jupyter ipykernel && \
    python2 -m ipykernel install && \
    pip3 install jupyter ipykernel && \
    python3 -m ipykernel install && \
# Configure user environment
# =============================================================================
# System configuration
# =============================================================================
# Add environment variables
    echo "JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/" >> /etc/environment && \
    echo "MPI_HOME=/usr/lib/openmpi" >> /etc/environment && \
    #echo "LD_LIBRARY_PATH=/usr/lib/openmpi/lib" >> /etc/environment && \
    echo "LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/openmpi/include/:${LD_LIBRARY_PATH}" >> /etc/environment && \
    mkdir /run/sshd && \
    rm -rf /var/lib/apt/lists/*

RUN rm -rf ./framework && \
    export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/" && \
    export MPI_HOME="/usr/lib/openmpi" && \
    export LD_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu/openmpi/include/:${LD_LIBRARY_PATH}" && \
    #export LD_LIBRARY_PATH="/usr/lib/openmpi/lib" && \
    #git clone --branch "guidance" https://github.com/bsc-wdc/compss.git framework && \
    git clone --branch "guidance" https://github.com/ramonamela/compss.git framework && \
    #git clone --branch "fifo_lifo_schedulers" http://ramela@compss.bsc.es/gitlab/compss/framework.git framework && \
    cd ./framework && \
    ./submodules_get.sh && \
    ./submodules_patch.sh && \
    echo "${JAVA_HOME}" && \
    sudo /framework/builders/buildlocal -A -M -K /opt/COMPSs && \
    rm -rf /root/.cache && \
    cd .. && \
    rm -r ./framework
  

RUN export DEBIAN_FRONTEND=noninteractive && \
    sudo apt-get update && sudo apt-get install -y --no-install-recommends gnupg2 software-properties-common && \
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9 && \
    sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/' && \
    sudo apt-get update && DEBIAN_FRONTEND=noninteractive sudo apt-get install -y --no-install-recommends apt-utils && \
    sudo sed -i 's/^mesg n$/tty -s \&\& mesg n/g' /root/.profile && \
    DEBIAN_FRONTEND=noninteractive sudo apt-get install -y --no-install-recommends r-base r-base-dev r-base-core libcurl4-openssl-dev \
    jags libpq-dev libmariadb-client-lgpl-dev && \
    rm -rf /var/lib/apt/lists/*


#Copy binaries into the container
RUN mkdir /TOOLS
COPY ./TOOLS/R_scripts /TOOLS/R_scripts
COPY ./TOOLS/deps.R /TOOLS/deps.R
RUN chmod 775 /TOOLS/R_scripts/*

RUN apt-get update && \
    apt-get autoremove openjdk-11-jre openjdk-11-jdk

RUN /usr/bin/Rscript /TOOLS/deps.R

RUN ln -s /usr/lib/jvm/java-8-openjdk-amd64 /usr/lib/jvm/default-java

ENV LC_ALL "C"
ENV RSCRIPTDIR "/TOOLS/R_scripts/"
ENV RSCRIPTBINDIR "/usr/bin/"

RUN echo "LC_ALL=\"C\"" >> /etc/environment
RUN echo "RSCRIPTDIR=\"/TOOLS/R_scripts/\"" >> /etc/environment
RUN echo "RSCRIPTBINDIR=\"/usr/bin/\"" >> /etc/environment

WORKDIR /TOOLS
