## The contents of this file are subject to the Mozilla Public License
## Version 1.1 (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.mozilla.org/MPL/
#
## Software distributed under the License is distributed on an "AS IS"
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
## the License for the specific language governing rights and
## limitations under the License.
#
## The Original Code is RabbitMQ.
#
## The Initial Developer of the Original Code is GoPivotal, Inc.
## Copyright (c) 2018 Pivotal Software, Inc.  All rights reserved.

FROM buildpack-deps:stretch

# We'll install the build dependencies for erlang-odbc along with the erlang
# build process:
ADD erlang-git-master/ /usr/src/erlang/
RUN set -xe \
    && ERLANG_VERSION=master \
    && runtimeDeps='libodbc1 \
            libsctp1 \
            libwxgtk3.0-0v5' \
    && buildDeps='unixodbc-dev \
            libsctp-dev \
            libwxgtk3.0-dev' \
    && apt-get update \
    && apt-get install -y --no-install-recommends $runtimeDeps \
    && apt-get install -y --no-install-recommends $buildDeps \
    && export ERL_TOP="/usr/src/erlang" \
    && cd $ERL_TOP \
    && ./otp_build autoconf \
    && ./configure \
        --enable-dirty-schedulers \
    && make -j$(nproc) \
    && make install \
    && find /usr/local -name examples | xargs rm -rf \
    && apt-get purge -y --auto-remove $buildDeps \
    && rm -rf $ERL_TOP /var/lib/apt/lists/* \
    && erl -version

CMD ["erl"]

# extra useful tools here: rebar & rebar3

RUN set -xe \
    && REBAR_VERSION="2.6.4" \
    && REBAR_DOWNLOAD_URL="https://github.com/rebar/rebar/archive/${REBAR_VERSION}.tar.gz" \
    && REBAR_DOWNLOAD_SHA256="577246bafa2eb2b2c3f1d0c157408650446884555bf87901508ce71d5cc0bd07" \
    && mkdir -p /usr/src/rebar-src \
    && curl -fSL -o rebar-src.tar.gz "$REBAR_DOWNLOAD_URL" \
    && echo "$REBAR_DOWNLOAD_SHA256 rebar-src.tar.gz" | sha256sum -c - \
    && tar -xzf rebar-src.tar.gz -C /usr/src/rebar-src --strip-components=1 \
    && rm rebar-src.tar.gz \
    && cd /usr/src/rebar-src \
    && ./bootstrap \
    && install -v ./rebar /usr/local/bin/ \
    && rm -rf /usr/src/rebar-src

RUN set -xe \
    && REBAR3_VERSION="3.14.2" \
    && REBAR3_DOWNLOAD_URL="https://github.com/erlang/rebar3/archive/${REBAR3_VERSION}.tar.gz" \
    && REBAR3_DOWNLOAD_SHA256="19fcdc73debb90d405864f728e188cbc5b61c3939b911e58c0b59bf1619c4810" \
    && mkdir -p /usr/src/rebar3-src \
    && curl -fSL -o rebar3-src.tar.gz "$REBAR3_DOWNLOAD_URL" \
    && echo "$REBAR3_DOWNLOAD_SHA256 rebar3-src.tar.gz" | sha256sum -c - \
    && tar -xzf rebar3-src.tar.gz -C /usr/src/rebar3-src --strip-components=1 \
    && rm rebar3-src.tar.gz \
    && cd /usr/src/rebar3-src \
    && HOME=$PWD ./bootstrap \
    && install -v ./rebar3 /usr/local/bin/ \
    && rm -rf /usr/src/rebar3-src

ENV LANG='C.UTF-8'

# Enable backports.
RUN echo 'deb http://httpredir.debian.org/debian stretch-backports main' \
    >> /etc/apt/sources.list.d/backports.list

# Prerequisites to mess with packages.
RUN apt-get clean && \
    apt-get update && \
    apt-get install -y -V --no-install-recommends \
      ca-certificates \
      wget \
      debconf-utils

# We need to set an APT preference to make sure $ERLANG_VERSION is
# used for all erlang* packages. Without this, apt-get(1) would try to
# install dependencies using the latest version. This would conflict
# with the strict pinning in all packages, and thus fail.
RUN echo 'Package: erlang*' > /etc/apt/preferences.d/erlang && \
    echo "Pin: version $ERLANG_VERSION" >> /etc/apt/preferences.d/erlang && \
    echo 'Pin-Priority: 1000' >> /etc/apt/preferences.d/erlang

# --------------------------------------------------------------------
# Packages to build RabbitMQ.
# --------------------------------------------------------------------

RUN apt-get clean && \
    apt-get update && \
    apt-get install -y -V --fix-missing --no-install-recommends \
      build-essential \
      curl \
      git \
      libcurl3-gnutls \
      man \
      mandoc \
      openssh-client \
      rsync \
      unzip \
      xmlto \
      xsltproc \
      zip

RUN set -xe \
  && ELIXIR_VERSION="v1.10.4" \
  && ELIXIR_DOWNLOAD_URL="https://github.com/elixir-lang/elixir/releases/download/${ELIXIR_VERSION}/Precompiled.zip" \
  && ELIXIR_DOWNLOAD_SHA256="2ec9891ec75a7cbd22396c6e7874b912b526d5a4bfd3c27206eee2a198b250a5" \
  && curl -fSL -o elixir-precompiled.zip $ELIXIR_DOWNLOAD_URL \
  && echo "$ELIXIR_DOWNLOAD_SHA256 elixir-precompiled.zip" | sha256sum -c - \
  && unzip -d /usr/local elixir-precompiled.zip \
  && rm elixir-precompiled.zip

# Put erl_call(1) in the $PATH.
RUN ln -fs /usr/local/lib/erlang/lib/erl_interface-*/bin/erl_call /usr/local/bin/erl_call

# --------------------------------------------------------------------
# Packages used to test RabbitMQ.
# --------------------------------------------------------------------

RUN echo 'slapd    slapd/internal/generated_adminpw    password   rabbitmq' | debconf-set-selections && \
    echo 'slapd    slapd/internal/adminpw    password   rabbitmq' | debconf-set-selections && \
    echo 'slapd    slapd/password2    password   rabbitmq' | debconf-set-selections && \
    echo 'slapd    slapd/password1    password   rabbitmq' | debconf-set-selections && \
    echo 'slapd    slapd/backend    select    HDB' | debconf-set-selections

RUN apt-get install -y -V --fix-missing --no-install-recommends \
      daemonize \
      ldap-utils \
      netcat \
      python-dev \
      python-simplejson \
      python3 \
      slapd

RUN git clone --depth 1 https://github.com/bats-core/bats-core.git && \
    cd bats-core && \
    ./install.sh /usr && \
    cd .. && \
    rm -rf bats-core

# We take OpenJDK from the backports. We need to be explicit
# on the repository and with ca-certificates-java, otherwise,
# ca-certificates-java pulls OpenJDK 7 in.
RUN apt-get install -y -V --fix-missing --no-install-recommends \
      ca-certificates-java \
      openjdk-8-jre-headless \
      openjdk-8-jdk-headless

# Install Java tools separately to be sure it picks the version of
# OpenJDK installed above.
RUN apt-get install -y -V --fix-missing --no-install-recommends \
      maven

# .NET Core 2.0 requirements (https://www.microsoft.com/net/core#linuxdebian).
RUN apt-get install -y -V --fix-missing --no-install-recommends \
        apt-transport-https

# .NET Core 2.0 (https://www.microsoft.com/net/core#linuxdebian).
RUN curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg && \
    mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg && \
    sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/dotnetdev.list'

# .NET Core 2.0 (https://www.microsoft.com/net/core#linuxdebian).
RUN apt-get update && \
    apt-get install -y -V --fix-missing --no-install-recommends \
    gettext \
    libunwind8 \
    dotnet-sdk-2.0.0

# .NET Core 2.0 warmup
RUN mkdir warmup \
    && cd warmup \
    && dotnet new console \
    && cd .. \
    && rm -rf warmup \
    && rm -rf /tmp/NuGetScratch

# Terraform, used to run some testsuites on AWS.
RUN TERRAFORM_VERSION=0.12.24 && \
    wget -O terraform.zip "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip" && \
    unzip terraform.zip && \
    mv terraform /usr/bin && \
    rm -f terraform.zip && \
    terraform --version
