# Inherit from docker container that has the fail source code
# prepared, including all tools which are needed to build FAIL*. The
# generic-tracing experiment was already built and the binaries are in
# place (~fail/bin/*)

FROM danceos/fail-generic-tracing

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

# Passwort for MySQL Daemon
ADD my.cnf /home/fail/.my.cnf
RUN chown fail /home/fail/.my.cnf

USER fail
WORKDIR /home/fail
RUN echo 'export PATH=$HOME/bin:$PATH' >> ~/.profile \
 && echo 'cd $HOME/fail-targets' >> ~/.profile

WORKDIR fail
RUN mkdir build; cd build
WORKDIR build

RUN cmake \
    -DAGXX=/home/fail/bin/ag++ \
    -DBOOST_THREAD_LIBRARY=/usr/lib/x86_64-linux-gnu/libpthread.so \
    # Enable / Disable features \
    -DBUILD_ARM=OFF \
    -DBUILD_BOCHS=ON \
    -DBUILD_CAPSTONE_DISASSEMBLER=OFF \
    -DBUILD_COMPUTE_HOPS=OFF \
    -DBUILD_CONVERT_TRACE=OFF \
    -DBUILD_DATA_AGGREGATOR=OFF \
    -DBUILD_DUMP_HOPS=OFF \
    -DBUILD_DUMP_TRACE=OFF \
    -DBUILD_FAULTSPACEPLOT=OFF \
    -DBUILD_GEM5=OFF \
    -DBUILD_IMPORT_TRACE=OFF \
    -DBUILD_LLVM_DISASSEMBLER=ON \
    -DBUILD_PANDA=OFF \
    -DBUILD_PRUNE_TRACE=OFF \
    -DBUILD_QEMU=OFF \
    -DBUILD_T32=OFF \
    -DBUILD_X86=ON \
    # \
    -DCLIENT_JOB_INITIAL=1 \
    -DCLIENT_JOB_LIMIT=1000 \
    -DCLIENT_JOB_REQUEST_SEC=30 \
    -DCLIENT_RAND_BACKOFF_TEND=8 \
    -DCLIENT_RAND_BACKOFF_TSTART=3 \
    -DCLIENT_RETRY_COUNT=3 \
    # ;-separated list \
    -DCMAKE_AGPP_FLAGS="-D__NO_MATH_INLINES" \
    # \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr/local \
    -DCONFIG_BOCHS_COMPRESS_STATE=ON \
    -DCONFIG_BOCHS_NON_VERBOSE=OFF \
    -DCONFIG_BOCHS_NO_ABORT=ON \
    -DCONFIG_DISABLE_KEYB_INTERRUPTS=OFF \
    -DCONFIG_EVENT_BREAKPOINTS=ON \
    -DCONFIG_EVENT_BREAKPOINTS_RANGE=ON \
    -DCONFIG_EVENT_GUESTSYS=ON \
    -DCONFIG_EVENT_INTERRUPT=ON \
    -DCONFIG_EVENT_IOPORT=ON \
    -DCONFIG_EVENT_JUMP=OFF \
    -DCONFIG_EVENT_MEMREAD=ON \
    -DCONFIG_EVENT_MEMWRITE=ON \
    -DCONFIG_EVENT_TRAP=ON \
    -DCONFIG_FAST_BREAKPOINTS=ON \
    -DCONFIG_FAST_WATCHPOINTS=ON \
    -DCONFIG_FIRE_INTERRUPTS=ON \
    -DCONFIG_INJECTIONPOINT_HOPS=OFF \
    -DCONFIG_SR_REBOOT=ON \
    -DCONFIG_SR_RESTORE=ON \
    -DCONFIG_SR_SAVE=ON \
    -DCONFIG_SUPPRESS_INTERRUPTS=ON \
    -DENABLE_DATABASE_TESTS=OFF \
    # ;-separated list \
    -DEXPERIMENTS_ACTIVATED="generic-experiment" \
    # \
    -DLibIberty_INCLUDE_DIRS=/usr/include/libiberty \
    -DLibIberty_LIBRARIES=/usr/lib/x86_64-linux-gnu/libiberty.a \
    -DMYSQL_CONFIG=/usr/bin/mysql_config \
    -DMYSQL_CONFIG_PREFER_PATH=/bin \
    # ;-separated list \
    -DPLUGINS_ACTIVATED="serialoutput" \
    # \
    -DSERVER_COMM_HOSTNAME=localhost \
    -DSERVER_COMM_TCP_PORT=1111 \
    -DSERVER_OUT_QUEUE_SIZE=0 \
    -DSERVER_PERFORMANCE_MEASURE=OFF \
    -DSERVER_PERF_LOG_PATH=perf.log \
    -DSERVER_PERF_STEPPING_SEC=1 \
    -DTEST_MYSQL_DATABASE=fail_test \
    -DTEST_MYSQL_HOST=localhost \
    -DTEST_MYSQL_PASSWORD=fail_test \
    -DTEST_MYSQL_PORT=3306 \
    -DTEST_MYSQL_USER=fail_test \
    -DVERBOSE_MAKE=OFF \
    -D_filename=/usr/include/wx-3.0/wx/version.h \
    # ;-separated list \
    -Dbochs_configure_params="--enable-a20-pin;--enable-x86-64;--enable-cpu-level=6;--enable-ne2000;--enable-acpi;--enable-pci;--enable-usb;--enable-trace-cache;--enable-fast-function-calls;--enable-host-specific-asms;--enable-disasm;--enable-readline;--enable-clgd54xx;--enable-fpu;--enable-vmx=2;--enable-monitor-mwait;--enable-cdrom;--enable-sb16=linux;--enable-gdb-stub;--disable-docbook;--with-nogui;--with-x11;--with-wx;--with-sdl" \
    # \
    -Dbochs_install_prefix=/home/fail/fail/simulators/bochs/install \
    ..

# We need to manually build Bochs first to generate the bochs/config.h - the external_project configure step
# (where this file is generated) is run at build time (not configure time), but the build order is not defined correctly.
# RUN cmake --build . --target libfailbochs_external-configure \
#  && cmake --build .
RUN cmake --build . --target libfailbochs_external-configure -- -j$(nproc) \
 && cmake --build . -- -j$(nproc)

RUN ln -s /home/fail/fail/build/bin/fail-client               /home/fail/bin/generic-experiment-client \
 && ln -s /home/fail/fail/build/bin/generic-experiment-server /home/fail/bin/ \
 && ln -s /home/fail/fail/tools/analysis/resultbrowser/run.py /home/fail/bin/resultbrowser.py

USER root

# Install additional packages
RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive TZ=Europe/Berlin apt-get install -y --no-install-recommends \
    grub-common \
    xorriso \
    grub-pc-bin \
    mysql-client \
    python2-minimal \
    python2-dev \
    # python2-flask \
    # python2-mysqldb \
    # python2-yaml \
    unzip \
 && apt-get clean

# Fix old shebangs
RUN ln -sf /usr/bin/python2 /usr/bin/python

# Install python packages
RUN wget https://bootstrap.pypa.io/pip/2.7/get-pip.py \
 && python2 get-pip.py \
 && wget https://raw.githubusercontent.com/paulfitz/mysql-connector-c/master/include/my_config.h -O /usr/include/mysql/my_config.h \
 && pip2 install flask pyyaml MySQL-python

USER fail

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

USER root

# Resultbrowser
EXPOSE 5000
