FROM ubuntu:bionic

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
 autoconf \
 automake \
 bsdmainutils \
 ccache \
 clang-format \
 clang \
 cmake \
 curl \
 libunwind8 \
 flex \
 bison \
 autopoint \
 gettext \
 g++ \
 gcc \
 gdb \
 git \
 glibc-source \
 htop  \
 iputils-ping \
 libc6-dbg \
 libclang-dev \
 liblttng-ust-dev \
 libncurses5-dev \
 libreadline-dev \
 libtool \
 libz-dev \
 llvm \
 locales \
 lttng-modules-dkms \
 lttng-tools \
 make \
 nano \
 net-tools \
 ninja-build \
 openssh-server \
 pkg-config \
 python-pip \
 screen \
 silversearcher-ag \
 sudo \
 tcl \
 tcsh \
 texinfo \
 tmux \
 tree \
 unzip \
 uuid-dev \
 vim \
 tcpdump \
 valgrind \
 tzdata \
 npm \
 nodejs \
 rsync \
 datamash \
 hwloc \
 strace \
 sshpass \
 libssh2-1-dev \
 # temporary mono dependency until .net core 2.1
 mono-runtime \
 libfsharp-core4.3-cil \
 libmono-system-net-http4.0-cil \
 mono-reference-assemblies-4.0 \
 bash-completion \
 libpq-dev \
 libpqxx-dev \
 && rm -rf /var/lig/apt/lists/*


run pip install pybars3 paramiko mkdocs
run npm install -g npx

# fix locale so we can run tmux
run sed -i 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
ENV LANG='en_US.utf8' LANGUAGE='en_US:en' LC_ALL='en_US.utf8'
run locale-gen

# expand libc sources
run cd /usr/src/glibc ; tar xvf glibc-*.tar.xz

# build cgdb (the version shipped with ubuntu is too old)
copy cgdb-0.7.0.tar.gz /externals/
run cd /externals/ && tar -xvzf cgdb-0.7.0.tar.gz && cd cgdb-0.7.0 && ./configure && make -j$(nproc) && make install

# setup ssh
RUN mkdir /var/run/sshd && echo 'root:root' | chpasswd && \
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config
# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

# vim friendlier defaults
RUN echo 'set background=dark \n\
set showcmd \n\
set showmatch \n\
set ignorecase \n\
set smartcase \n\
set incsearch \n\
set autowrite \n\
set hidden \n\
set mouse= \n\
imap jj <Esc> \n\
' >> /etc/vim/vimrc

#taken from https://hub.docker.com/r/microsoft/dotnet/~/dockerfile/
# region .net core

# Install .NET Core SDK
ENV DOTNET_SDK_VERSION 2.1.500
ENV DOTNET_SDK_DOWNLOAD_URL https://dotnetcli.blob.core.windows.net/dotnet/Sdk/$DOTNET_SDK_VERSION/dotnet-sdk-$DOTNET_SDK_VERSION-linux-x64.tar.gz
ENV DOTNET_SDK_DOWNLOAD_SHA 85055728e2433dfde41d15c85475f2dc6cfdd30242b4b23065b63cb12cc846acb93c09c000b02b722890ceac8ac382b40871c78660716ca2339c71052fe52f4e
ENV NUGET_XMLDOC_MODE skip
RUN curl -SL $DOTNET_SDK_DOWNLOAD_URL --output dotnet.tar.gz \
    && echo "$DOTNET_SDK_DOWNLOAD_SHA dotnet.tar.gz" | sha512sum -c - \
    && mkdir -p /usr/share/dotnet \
    && tar -zxf dotnet.tar.gz -C /usr/share/dotnet \
    && rm dotnet.tar.gz \
    && ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet

run DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1 dotnet tool install --tool-path /usr/local/bin paket 
run chmod +X /usr/local/bin/paket

copy sqlite-version-3.26.0.zip extension-functions.c build_sqlite.sh /
run ./build_sqlite.sh


#git-lfs
copy git-lfs /usr/bin/
run chmod +x /usr/bin/git-lfs && git lfs install

##### Onload Stuff #####

# Onload version and its md5sum
ARG ONLOAD_VERSION="201811-u1"
ARG ONLOAD_MD5SUM="357e64862aa4145e49d218fd04e63407"

# When ONLOAD_WITHZF is non-empty, the build includes Solarflare's TCPDirect library.
# Default is to exclude it.
ARG ONLOAD_WITHZF

# 1) Install OpenOnload build dependencies
# 2) Download and verify OpenOnload from Solarflare's site
# 3) Extract, build, and install onload
# 4) Cleanup

RUN \
    DEBIAN_FRONTEND=noninteractive apt-get update -y \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        ethtool \
        gcc-multilib \
        libc6-dev-i386 \
        libpcap0.8-dev \
        libtool-bin \
    && cd /tmp \
    && curl -fSL https://www.openonload.org/download/openonload-${ONLOAD_VERSION}.tgz -o /tmp/openonload-${ONLOAD_VERSION}.tgz \
    && echo "${ONLOAD_MD5SUM} openonload-${ONLOAD_VERSION}.tgz" | md5sum --check \
    && tar -zxf openonload-${ONLOAD_VERSION}.tgz \
    && cd /tmp/openonload-${ONLOAD_VERSION} \
    && cd scripts \
    && ./onload_build --user \
    && ./onload_install --userfiles --nobuild \
    && cd /tmp \
    && rm -rf openonload-${ONLOAD_VERSION}.tgz openonload-${ONLOAD_VERSION} \
    && if [ -z ${ONLOAD_WITHZF} ] ; then rm -rf /usr/include/zf /usr/bin/zf_stackdump /usr/bin/zf_debug /usr/lib/x86_64-linux-gnu/zf /usr/lib/x86_64-linux-gnu/libonload_zf* ; fi \
    && DEBIAN_FRONTEND=noninteractive apt-get remove -y --purge \
        curl \
        patch \
    && DEBIAN_FRONTEND=noninteractive apt-get autoremove -y

########################
