FROM --platform=linux/amd64 golang:1.21.2-bullseye

ENV DEBIAN_FRONTEND=noninteractive

# Get sysbench installed
RUN apt update
RUN apt install -y curl

# Install sysbench deps
RUN apt update -y && apt install -y \
  automake \
  libtool \
  pkg-config \
  libaio-dev \
  default-libmysqlclient-dev \
  libssl-dev

# Build sysbench from source
RUN git clone https://github.com/akopytov/sysbench.git && \
  cd sysbench && \
  git checkout -b 1.0.20 tags/1.0.20 && \
  ./autogen.sh && \
  ./configure && \
  make -j && \
  make install

# Install sqlite3 from source
RUN \
  apt-get install -y \
  build-essential \
  tcl \
  lsb-release \
  && wget \
    -O sqlite.tar.gz \
    https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release \
  && tar xvfz sqlite.tar.gz \
  # Configure and make SQLite3 binary
  && ./sqlite/configure --prefix=/usr \
  && make \
  && make install \
  # Smoke test
  && sqlite3 --version

WORKDIR /
COPY ./go /dolt/go
COPY ./config.json /config.json
COPY ./tpcc-config.json /tpcc-config.json
COPY ./sysbench-runner-tests-entrypoint.sh /entrypoint.sh
RUN git clone https://github.com/dolthub/sysbench-lua-scripts.git
RUN git clone https://github.com/Percona-Lab/sysbench-tpcc.git

WORKDIR /mysql
RUN curl -L -O https://dev.mysql.com/get/mysql-apt-config_0.8.22-1_all.deb
RUN dpkg -i mysql-apt-config_0.8.22-1_all.deb
# On 2023-12-14, the GPG key this repository uses to publish these packages expired. We make it insecure and install it anyway for now.
# See https://bugs.mysql.com/bug.php?id=113427
# Hopefully we can remove this soon.
RUN sed -i.bak \
  -e 's|^deb |deb [allow-insecure=true allow-weak=true allow-downgrade-to-insecure=true] |' \
  -e 's|^deb-src |deb-src [allow-insecure=true allow-weak=true allow-downgrade-to-insecure=true] |' \
  /etc/apt/sources.list.d/mysql.list 
RUN apt-get update && apt-get install -y --allow-unauthenticated mysql-server
RUN mysql --version

# Install dolt
WORKDIR /dolt/go/cmd/dolt
RUN go build -o /usr/local/bin/dolt .

WORKDIR /dolt/go/performance/utils/sysbench_runner/cmd
ENTRYPOINT ["/entrypoint.sh"]
