# Set the base image to Ubuntu Jammy. Jammy includes the correct dependency versions except for LLVM.
FROM ubuntu:jammy

LABEL org.opencontainers.image.authors="Christian Dietrich <stettberger@dokucode.de>"

# TODO: Can shrink the image size down by a LOT:
#       - A lot of dependencies are unnecessary, I just need to figure out which
#       - Separate build-time / runtime dependencies, then do 2 stages
#       - Combine multiple RUNs into a single one
#       - Use apt-get --no-install-recommends during package install and apt-get clean afterwards

# Install basic packages
RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive TZ=Europe/Berlin apt-get install -y --no-install-recommends \
    build-essential \
    ca-certificates \
    cmake \
    cmake-curses-gui \
    wget \
    git \
    doxygen \
    screen \
    openssh-server \
    neovim \
    ranger

# NOTE: Only required if the base image is not Jammy
# Install Boost 1.74 (focal includes 1.71)
# Jammy includes the correct version but its llvm is too old
# RUN wget https://archives.boost.io/release/1.74.0/source/boost_1_74_0.tar.gz \
#  && tar xvzf boost_1_74_0.tar.gz && cd boost_1_74_0 \
#  && chmod +x ./bootstrap.sh \
#  && ./bootstrap.sh --prefix=/usr/local \
#  && ./b2 && ./b2 install
#  && cd / && rm -rf boost_1_74_0.tar.gz && rm -rf boost_1_74_0
# ENV BOOST_ROOT=/usr/local

# NOTE: Only required if the base image is not Focal
# Install LLVM 6.0 from source
RUN git clone https://github.com/llvm/llvm-project.git && cd llvm-project \
 && git checkout llvmorg-6.0.0 \
 && mkdir build && cd build \
 && cmake -G "Unix Makefiles" ../llvm \
    -DCMAKE_BUILD_TYPE=Release \
    -DLLVM_ENABLE_TERMINFO=OFF \
    -DLLVM_ENABLE_CURSES=OFF \
    -DLLVM_ENABLE_PROJECTS="clang;lld" \
 && make -j$(nproc) && make install \
 && cd / && rm -rf ./llvm-project

# Install packages required to build FAIL*
RUN DEBIAN_FRONTEND=noninteractive TZ=Europe/Berlin apt-get install -y --no-install-recommends \
    # binutils-dev \
    libmysqlclient-dev \
    libprotobuf-dev \
    libtinfo-dev \
    libpcl1-dev \
    libdwarf-dev \
    libelf-dev \
    libiberty-dev \
    libboost-thread-dev \
    libboost-system-dev \
    libboost-regex-dev \
    libboost-coroutine-dev \
    libboost-context-dev \
    libfontconfig1-dev \
    zlib1g-dev \
    libz3-dev \
    libsdl1.2-dev \
    # libsdl2-dev \
    libgtk2.0-dev \
    # libgtk-3-dev \
    # libwxbase3.0-dev \
    libwxgtk3.0-gtk3-dev \
    libncurses-dev \
    # libncurses5-dev \
    libx11-dev \
    xorg-dev \
    libasound2-dev

# Install packages required to run FAIL*
RUN DEBIAN_FRONTEND=noninteractive TZ=Europe/Berlin apt-get install -y --no-install-recommends \
    protobuf-compiler \
    libtinfo6 \
    # libtinfo5 \
    libmariadb3 \
    libprotobuf23 \
    libncurses6 \
    && apt-get clean

# NOTE: Only required if we install llvm from apt
# Symlink clang++/llvm-config to match docs
# RUN ln -sf /usr/bin/clang++-6.0 /usr/bin/clang++ \
#  && ln -sf /usr/bin/llvm-config-6.0 /usr/bin/llvm-config

# Add a user for compiling FAIL*
RUN useradd fail \
 && mkdir /home/fail && chown fail /home/fail \
 && echo 'fail:fail' | chpasswd && chsh fail --shell /bin/bash \
 && adduser fail sudo

# SSH login fix. Otherwise user is kicked off after login
RUN mkdir /var/run/sshd \
 && sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE="in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

USER fail
ENV HOME=/home/fail
WORKDIR /home/fail

# Get AspectC++ (originally v1.2) for 64 Bit
ARG acversion="2.5"
RUN wget http://www.aspectc.org/releases/"$acversion"/ac-bin-linux-x86-64bit-"$acversion".tar.gz \
 && tar xvzf ac-bin-linux-x86-64bit-"$acversion".tar.gz \
 && mkdir bin && mv aspectc++/ac++ aspectc++/ag++ bin/ \
 && rm -rf aspectc++ && rm -rf ac-bin-linux-x86-64bit-"$acversion".tar.gz
ENV PATH=/home/fail/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# Clone FAIL*
# Break docker layer cache
ARG CACHE_DATE=1970-01-01
# RUN git clone https://github.com/danceos/fail.git
RUN git clone https://gitea.vps.chriphost.de/christoph/fail
WORKDIR fail

USER root

# Accept SSH connections
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
