FROM ubuntu:22.04

ARG DEBIAN_FRONTEND=noninteractive

# Note installations that take a while (more than 10s)

RUN apt-get update
RUN apt-get install -y ca-certificates
RUN apt-get install -y ccache
RUN apt-get install -y curl
RUN apt-get install -y fish
RUN apt-get install -y gdb
# git-all: ~250s
RUN apt-get install -y git-all
RUN apt-get install -y gnupg
# liblttng-ust-dev: ~30s
RUN apt-get install -y liblttng-ust-dev
RUN apt-get install -y libssh2-1-dev
RUN apt-get install -y locales
RUN apt-get install -y ninja-build
# openssh-server: ~40s
RUN apt-get install -y openssh-server
RUN apt-get install -y pkg-config
RUN apt-get install -y software-properties-common
RUN apt-get install -y sudo
RUN apt-get install -y tzdata
RUN apt-get install -y vim
RUN apt-get install -y zip

# Python: ~30s
RUN apt-get install -y \
   python3-pip \
   python3-venv \
   && python3 -m pip install --upgrade pip

# GCC
ARG gcc_major=13
RUN \
   add-apt-repository ppa:ubuntu-toolchain-r/test \
   && apt-get update \
   && apt-get install -y g++-$gcc_major

# Clang
ARG clang_major=17
RUN \
   curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key 2> /dev/null \
   | gpg --dearmor - \
   | tee /usr/share/keyrings/llvm-snapshot.gpg > /dev/null \
   && echo "deb [signed-by=/usr/share/keyrings/llvm-snapshot.gpg] http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-$clang_major main" \
   | tee -a "/etc/apt/sources.list.d/llvm.list" \
   && apt-get update \
   && apt-get install -y \
   clang-$clang_major \
   clang-format-$clang_major \
   clangd-$clang_major

# CMake
RUN \
   curl -fsSL https://apt.kitware.com/keys/kitware-archive-latest.asc 2> /dev/null \
   | gpg --dearmor - \
   | tee /usr/share/keyrings/kitware-archive-keyring.gpg > /dev/null \
   && echo "deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main" \
   | tee /etc/apt/sources.list.d/kitware.list > /dev/null \
   && apt-get update \
   && apt-get install -y cmake

# Dotnet
RUN apt-get update && apt-get install -y dotnet8

# Node
ARG node_major=18
RUN mkdir -p /etc/apt/keyrings \
   && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key \
   | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
   && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$node_major.x nodistro main" \
   | tee /etc/apt/sources.list.d/nodesource.list \
   && apt-get update \
   && apt-get install -y nodejs

# Emumba (deps)
RUN apt-get update && apt-get -y install rapidjson-dev
# Emumba
COPY emumba /emumba
RUN dpkg -i /emumba/emumba-deribit-connector-3.3.1-Linux.LICENSE_DISABLED.deb \
    && rm -r /emumba

# Pcap
RUN apt-get update && apt-get install -y libpcap0.8-dev

# Databento (deps)
RUN apt-get update && apt-get -y install libzstd-dev

# Docker in Docker
RUN apt-get update && apt-get install -y \
    docker.io \
    docker-compose \
    docker-compose-v2

# Set up default executables (update-alternatives)
RUN \
   update-alternatives --install /usr/bin/clang clang /usr/bin/clang-$clang_major 100; \
   update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-$clang_major 100; \
   update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-$clang_major 100; \
   update-alternatives --install /usr/bin/clangd clangd /usr/bin/clangd-$clang_major 100; \
   update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-$gcc_major 100; \
   update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-$gcc_major 100

# Add compiler symlinks for ccache 
RUN \
   ln -s /usr/bin/ccache /usr/local/bin/clang; \
   ln -s /usr/bin/ccache /usr/local/bin/clang++; \
   ln -s /usr/bin/ccache /usr/local/bin/clang-$clang_major; \
   ln -s /usr/bin/ccache /usr/local/bin/clang++-$clang_major; \
   ln -s /usr/bin/ccache /usr/local/bin/gcc; \
   ln -s /usr/bin/ccache /usr/local/bin/g++

RUN locale-gen en_US.UTF-8

ARG docker_user="bts"
ARG docker_uid="1000"
ARG docker_pass="bts"

RUN \
   groupadd -g "$docker_uid" "$docker_user" \
   && useradd \
   -m \
   -u "$docker_uid" \
   -g "$docker_uid" \
   -G sudo \
   --password "$(openssl passwd -1 "$docker_pass")" \
   "$docker_user"
