#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

ARG OS_IMAGE_NAME
ARG OS_IMAGE_TAG

FROM $OS_IMAGE_NAME:$OS_IMAGE_TAG AS gluten-buildenv
MAINTAINER Hongze Zhang<hongze.zhang@intel.com>

SHELL ["/bin/bash", "-l", "-c"]
ENTRYPOINT ["/bin/bash", "-l", "-c"]
CMD ["/bin/bash"]

# Add script for adding environment variables for login-shell (e.g. a shell via ssh)
COPY scripts/set-login-env.sh /usr/local/sbin/set-login-env

# REQUIRED PROXIES: APT, WGET, GIT, MAVEN (also Maven mirror)
ARG HTTP_PROXY_HOST
ARG HTTP_PROXY_PORT

# Sometimes ENV a=b won't work when the shell is not docker-default, so we
# use both two ways to set the variables
ENV http_proxy=${HTTP_PROXY_HOST:+"http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT"}
ENV https_proxy=${HTTP_PROXY_HOST:+"http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT"}
ENV no_proxy=localhost,127.0.0.1,127.0.0.0/8,172.16.0.0/12,192.168.0.0/16
ENV HTTP_PROXY=${HTTP_PROXY_HOST:+"http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT"}
ENV HTTPS_PROXY=${HTTP_PROXY_HOST:+"http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT"}
ENV NO_PROXY=localhost,127.0.0.1,127.0.0.0/8,172.16.0.0/12,192.168.0.0/16
RUN set-login-env "http_proxy=${HTTP_PROXY_HOST:+http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT}"
RUN set-login-env "https_proxy=${HTTP_PROXY_HOST:+http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT}"
RUN set-login-env "no_proxy=localhost,127.0.0.1,127.0.0.0/8,172.16.0.0/12,192.168.0.0/16"
RUN set-login-env "HTTP_PROXY=${HTTP_PROXY_HOST:+http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT}"
RUN set-login-env "HTTPS_PROXY=${HTTP_PROXY_HOST:+http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT}"
RUN set-login-env "NO_PROXY=localhost,127.0.0.1,127.0.0.0/8,172.16.0.0/12,192.168.0.0/16"

RUN if [ -n "$HTTP_PROXY_HOST" ]; then echo "Acquire::http::Proxy \"http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT\";" >> /etc/apt/apt.conf; fi
RUN if [ -n "$HTTP_PROXY_HOST" ]; then echo "Acquire::https::Proxy \"http://$HTTP_PROXY_HOST:$HTTP_PROXY_PORT\";" >> /etc/apt/apt.conf; fi

ARG MAVEN_MIRROR_URL

RUN if [ -n "$MAVEN_MIRROR_URL" ]; \
    then \
      MAVEN_SETTINGS_TEMPLATE="<settings><mirrors><mirror><id>mavenmirror</id><mirrorOf>central</mirrorOf><name>MavenMirror</name><url>{{MAVEN_MIRROR_URL}}</url></mirror></mirrors><proxies><proxy><id>httpproxy</id><active>{{MAVEN_PROXY_ENABLE}}</active><protocol>http</protocol><host>{{MAVEN_PROXY_HOST}}</host><port>{{MAVEN_PROXY_PORT}}</port></proxy><proxy><id>httpsproxy</id><active>{{MAVEN_PROXY_ENABLE}}</active><protocol>https</protocol><host>{{MAVEN_PROXY_HOST}}</host><port>{{MAVEN_PROXY_PORT}}</port></proxy></proxies></settings>"; \
      MAVEN_SETTINGS_TEMPLATE=$(echo $MAVEN_SETTINGS_TEMPLATE | sed "s@{{MAVEN_MIRROR_URL}}@$MAVEN_MIRROR_URL@g"); \
    else \
      MAVEN_SETTINGS_TEMPLATE="<settings><proxies><proxy><id>httpproxy</id><active>{{MAVEN_PROXY_ENABLE}}</active><protocol>http</protocol><host>{{MAVEN_PROXY_HOST}}</host><port>{{MAVEN_PROXY_PORT}}</port></proxy><proxy><id>httpsproxy</id><active>{{MAVEN_PROXY_ENABLE}}</active><protocol>https</protocol><host>{{MAVEN_PROXY_HOST}}</host><port>{{MAVEN_PROXY_PORT}}</port></proxy></proxies></settings>"; \
    fi \
    && if [ -n "$HTTP_PROXY_HOST" ]; \
    then \
      MAVEN_SETTINGS_TEMPLATE=$(echo $MAVEN_SETTINGS_TEMPLATE | sed "s/{{MAVEN_PROXY_ENABLE}}/true/g"); \
      MAVEN_SETTINGS_TEMPLATE=$(echo $MAVEN_SETTINGS_TEMPLATE | sed "s/{{MAVEN_PROXY_HOST}}/$HTTP_PROXY_HOST/g"); \
      MAVEN_SETTINGS_TEMPLATE=$(echo $MAVEN_SETTINGS_TEMPLATE | sed "s/{{MAVEN_PROXY_PORT}}/$HTTP_PROXY_PORT/g"); \
    else \
      MAVEN_SETTINGS_TEMPLATE=$(echo $MAVEN_SETTINGS_TEMPLATE | sed "s/{{MAVEN_PROXY_ENABLE}}/false/g"); \
      MAVEN_SETTINGS_TEMPLATE=$(echo $MAVEN_SETTINGS_TEMPLATE | sed "s/{{MAVEN_PROXY_HOST}}/localhost/g"); \
      MAVEN_SETTINGS_TEMPLATE=$(echo $MAVEN_SETTINGS_TEMPLATE | sed "s/{{MAVEN_PROXY_PORT}}/8888/g"); \
    fi \
    && MAVEN_SETTINGS=$MAVEN_SETTINGS_TEMPLATE \
    && mkdir -p /root/.m2/ \
    && echo $MAVEN_SETTINGS > /root/.m2/settings.xml

# Display environment information
RUN ulimit -a
RUN env
RUN cat /etc/apt/apt.conf || (echo "Apt proxy not set" && true)
RUN cat /root/.m2/settings.xml

## APT dependencies

# Update, then install essentials
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y sudo locales wget tar tzdata git ccache ninja-build build-essential llvm-11-dev clang-11 libiberty-dev libdwarf-dev libre2-dev libz-dev libssl-dev libboost-all-dev libcurl4-openssl-dev curl zip unzip tar pkg-config autoconf-archive bison flex

# install HBM dependencies
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y autoconf automake g++ libnuma-dev libtool numactl unzip libdaxctl-dev

# Install OpenJDK 8 and Maven
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y openjdk-8-jdk
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y maven

# Setup SSH server
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y openssh-server
RUN systemctl disable ssh
RUN ssh-keygen -A
RUN mkdir -p /run/sshd
RUN echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config.d/override.conf
RUN echo 'X11Forwarding yes' >> /etc/ssh/sshd_config.d/override.conf
RUN echo 'X11UseLocalhost no' >> /etc/ssh/sshd_config.d/override.conf
RUN echo -e "123\n123" | passwd

ARG TIMEZONE
RUN test -n "$TIMEZONE" || (echo "TIMEZONE not set" && false)

RUN TZ=$TIMEZONE \
    && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
    && echo $TZ > /etc/timezone \
    && dpkg-reconfigure -f noninteractive tzdata

# Configure locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen \
    && locale-gen

ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8
RUN set-login-env "LANG=en_US.UTF-8"
RUN set-login-env "LANGUAGE=en_US:en"
RUN set-login-env "LC_ALL=en_US.UTF-8"

# Install CMake
RUN cd /opt && wget https://github.com/Kitware/CMake/releases/download/v3.28.3/cmake-3.28.3-linux-x86_64.sh \
    && mkdir cmake \
    && bash cmake-3.28.3-linux-x86_64.sh --skip-license --prefix=/opt/cmake \
    && ln -s /opt/cmake/bin/cmake /usr/bin/cmake
RUN cmake --version

# Install GCC 11
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y software-properties-common
RUN add-apt-repository ppa:ubuntu-toolchain-r/test
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y gcc-11 g++-11
RUN rm -f /usr/bin/gcc /usr/bin/g++
RUN ln -s /usr/bin/gcc-11 /usr/bin/gcc
RUN ln -s /usr/bin/g++-11 /usr/bin/g++
RUN cc --version
RUN c++ --version

# Spark binaries
WORKDIR /opt
ARG BUILD_SPARK_BINARIES

# Build & install Spark 3.2.2
RUN if [ "$BUILD_SPARK_BINARIES" = "ON" ]; then wget https://archive.apache.org/dist/spark/spark-3.2.2/spark-3.2.2-bin-hadoop3.2.tgz; fi
RUN if [ "$BUILD_SPARK_BINARIES" = "ON" ]; then mkdir spark322 && tar -xvf spark-3.2.2-bin-hadoop3.2.tgz -C spark322 --strip-components=1; fi

# Build & install Spark 3.3.1
RUN if [ "$BUILD_SPARK_BINARIES" = "ON" ]; then wget https://archive.apache.org/dist/spark/spark-3.3.1/spark-3.3.1-bin-hadoop3.tgz; fi
RUN if [ "$BUILD_SPARK_BINARIES" = "ON" ]; then mkdir spark331 && tar -xvf spark-3.3.1-bin-hadoop3.tgz -C spark331 --strip-components=1; fi

# Build & install Spark 3.4.3
RUN if [ "$BUILD_SPARK_BINARIES" = "ON" ]; then https://archive.apache.org/dist/spark/spark-3.4.3/spark-3.4.3-bin-hadoop3.tgz; fi
RUN if [ "$BUILD_SPARK_BINARIES" = "ON" ]; then mkdir spark343 && tar -xvf spark-3.4.3-bin-hadoop3.tgz -C spark343 --strip-components=1; fi

# Build & install Spark 3.5.1
RUN if [ "$BUILD_SPARK_BINARIES" = "ON" ]; then https://archive.apache.org/dist/spark/spark-3.5.1/spark-3.5.1-bin-hadoop3.tgz; fi
RUN if [ "$BUILD_SPARK_BINARIES" = "ON" ]; then mkdir spark351 && tar -xvf spark-3.5.1-bin-hadoop3.tgz -C spark351 --strip-components=1; fi

# Prepare entry command
COPY scripts/cmd.sh /root/.cmd.sh
CMD ["/root/.cmd.sh"]
