FROM postgres:16

ENV POSTGRES_PASSWORD=password
ENV POSTGRES_USER=postgres

ENV GO_VERSION=1.23.3

# Install Go
RUN apt-get update && \
    apt-get install -y wget sudo git build-essential libicu-dev && \
    wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz && \
    tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz && \
    rm go${GO_VERSION}.linux-amd64.tar.gz

# Install bats
RUN git clone https://github.com/bats-core/bats-core.git
WORKDIR bats-core
RUN ./install.sh $HOME
ENV PATH="/root/bin/:${PATH}"

# Set Go environment variables
ENV PATH="/usr/local/go/bin:${PATH}"

# Doltgres source
WORKDIR /root/building
COPY ./ ./doltgresql

# Get rid of the generated tests, since we aren't going to run them and they don't build without more work
RUN rm -rf ./doltgresql/testing/generation/

# Build the parser
WORKDIR /root/building/doltgresql/postgres/parser
RUN bash ./build.sh

# Build the doltgres binary, which we will need for bats, and put it on PATH
WORKDIR /root/building/doltgresql/cmd/doltgres
RUN go build .
RUN cp ./doltgres /root/bin

WORKDIR /root/building/doltgresql/

# This env var is required to run the bats replication tests
ENV RUN_DOLTGRES_REPLICATION_TESTS=true
# This should be set in the github env, but set it here for local testing as well
ENV GITHUB_ACTION=true

# Run the test script, which starts postgres and then runs the Go script
ENTRYPOINT ["testing/replication-test-entrypoint.sh"]

