FROM --platform=linux/amd64 ubuntu:20.04

# install python, libmysqlclient-dev, java, bats, git ruby, perl, cpan
ENV DEBIAN_FRONTEND=noninteractive
RUN apt update -y && \
    apt install -y \
        curl \
        gnupg \
        libwxbase3.0-0v5 \
        libwxgtk3.0-gtk3-0v5 \
        libncurses5 \
        libsctp1 \
        software-properties-common && \
    curl -sL https://deb.nodesource.com/setup_14.x | bash - && \
    add-apt-repository ppa:deadsnakes/ppa -y && \
    curl -OL https://binaries2.erlang-solutions.com/ubuntu/pool/contrib/e/esl-erlang/esl-erlang_25.0-1~ubuntu~focal_amd64.deb && \
    dpkg -i esl-erlang_25.0-1~ubuntu~focal_amd64.deb && \
    curl -LO https://binaries2.erlang-solutions.com/ubuntu/pool/contrib/e/elixir/elixir_1.14.3_1_otp_25.3~ubuntu~focal_all.deb && \
    dpkg -i elixir_1.14.3_1_otp_25.3~ubuntu~focal_all.deb
RUN apt update -y && \
    apt install -y \
        python3.8 \
        python3-pip \
        curl \
        wget \
        pkg-config \
        mysql-client \
        libmysqlclient-dev \
        openjdk-17-jdk \
        ant \
        ca-certificates-java \
        bats \
        perl \
        php \
        php-mysqli \
        cpanminus \
        cmake \
        g++ \
        libmysqlcppconn-dev \
        git \
        ruby \
        ruby-dev \
        gem \
        libc6 \
        libgcc1 \
        libgssapi-krb5-2 \
        libicu66 \
        libssl1.1 \
        libstdc++6 \
        zlib1g \
        r-base \
        postgresql \
        postgresql-contrib \
        libpq-dev \
        nodejs \
        lsof \
        postgresql-server-dev-all && \
        update-ca-certificates -f

# install go
WORKDIR /root
ENV GO_VERSION=1.23.3
ENV GOPATH=$HOME/go
ENV PATH=$PATH:$GOPATH/bin
ENV PATH=$PATH:$GOPATH/bin:/usr/local/go/bin
RUN curl -O "https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz" && \
    sha256sum "go${GO_VERSION}.linux-amd64.tar.gz" && \
    tar -xvf "go${GO_VERSION}.linux-amd64.tar.gz" -C /usr/local && \
    chown -R root:root /usr/local/go && \
    mkdir -p $HOME/go/{bin,src} && \
    go version

# install MySQL dependency from source
RUN git clone https://github.com/go-sql-driver/mysql.git
WORKDIR mysql
RUN git checkout tags/v1.6.0 -b v1.6.0
RUN go install .
WORKDIR /

# install dotnet
RUN curl -LO https://download.visualstudio.microsoft.com/download/pr/13b9d84c-a35b-4ffe-8f62-447a01403d64/1f9ae31daa0f7d98513e7551246899f2/dotnet-sdk-5.0.400-linux-x64.tar.gz && \
    tar -C /usr/local/bin -xzf dotnet-sdk-5.0.400-linux-x64.tar.gz && \
    dotnet --version

# install pip for python3.8
RUN curl -LO https://bootstrap.pypa.io/get-pip.py && \
  python3.8 get-pip.py && \
  pip --version

# install mysql connector and pymsql
RUN pip install mysql-connector-python==8.0.32
RUN pip install PyMySQL==1.0.2
RUN pip install sqlalchemy==1.4.46

# Setup JAVA_HOME -- useful for docker commandline
ENV JAVA_HOME /usr/lib/jvm/java-17-openjdk-amd64/
 
# install mysql connector java
RUN mkdir -p /mysql-client-tests/java
RUN curl -L -o /mysql-client-tests/java/mysql-connector-java-8.0.21.jar \
  https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.21/mysql-connector-java-8.0.21.jar

# install node deps
COPY mysql-client-tests/node/package.json /mysql-client-tests/node/
COPY mysql-client-tests/node/package-lock.json /mysql-client-tests/node/
WORKDIR /mysql-client-tests/node
RUN npm install

# install cpan dependencies
RUN cpanm --force DBD::mysql

# install ruby dependencies
COPY mysql-client-tests/ruby/Gemfile /mysql-client-tests/ruby/
COPY mysql-client-tests/ruby/Gemfile.lock /mysql-client-tests/ruby/
WORKDIR /mysql-client-tests/ruby
RUN gem install bundler -v 2.1.4 && bundle install

# install R packages
RUN Rscript -e 'install.packages(c("DBI", "RMySQL", "RMariaDB"), \
                  repos = c(RSPM="https://packagemanager.rstudio.com/cran/__linux__/focal/latest"))'

# install rust
RUN curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain stable -y
ENV PATH="/root/.cargo/bin:${PATH}"

# install postgres and psql
RUN service postgresql start

# install mysql_fdw
WORKDIR /mysql-client-tests/mysql_fdw
RUN git clone https://github.com/EnterpriseDB/mysql_fdw --branch REL-2_9_0
WORKDIR /mysql-client-tests/mysql_fdw/mysql_fdw
RUN make USE_PGXS=1 && \
    make USE_PGXS=1 install

# install dolt from source
WORKDIR /root/building
COPY ./go .
ENV GOFLAGS="-mod=readonly"
RUN go build -o /usr/local/bin/dolt ./cmd/dolt

COPY mysql-client-tests /mysql-client-tests
COPY mysql-client-tests/mysql-client-tests-entrypoint.sh /mysql-client-tests/entrypoint.sh

# install rust dependencies
WORKDIR /mysql-client-tests/rust
RUN cargo build --config net.git-fetch-with-cli=true

WORKDIR /mysql-client-tests
ENTRYPOINT ["/mysql-client-tests/entrypoint.sh"]
