#!/usr/bin/env bash
set -euo pipefail

IMAGE="${IMAGE:-pggraph:pg-matrix}"
PG_VERSIONS="${PG_VERSIONS:-14 15 16 17 18}"
RUN_RUST_TESTS="${RUN_RUST_TESTS:-1}"
RUN_PGRX_SQL="${RUN_PGRX_SQL:-1}"
PGRX_TEST_FILTER="${PGRX_TEST_FILTER:-}"
RUN_GQL_WRITE_MATRIX="${RUN_GQL_WRITE_MATRIX:-1}"
RUN_POSTGRES_SANITIZER="${RUN_POSTGRES_SANITIZER:-0}"
RUN_DURABLE_PROJECTION_MATRIX="${RUN_DURABLE_PROJECTION_MATRIX:-0}"
RUN_PACKAGE_INSTALL_MATRIX="${RUN_PACKAGE_INSTALL_MATRIX:-0}"
RUN_OPEN_TYPE_PACKAGE_SMOKE="${RUN_OPEN_TYPE_PACKAGE_SMOKE:-0}"
RUN_CRASH_MATRIX="${RUN_CRASH_MATRIX:-0}"
RUN_RUNTIME_RESOURCES="${RUN_RUNTIME_RESOURCES:-0}"
RUN_PG_UPGRADE_MATRIX="${RUN_PG_UPGRADE_MATRIX:-0}"
RUN_V1_UPDATE_ARTIFACT_ROLLBACK="${RUN_V1_UPDATE_ARTIFACT_ROLLBACK:-0}"
RUN_V1_2_UPDATE_ARTIFACT_ROLLBACK="${RUN_V1_2_UPDATE_ARTIFACT_ROLLBACK:-0}"
PROFILE_TIMEOUT_SECONDS="${PROFILE_TIMEOUT_SECONDS:-600}"
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
DOCKERFILE="$ROOT_DIR/graph/tests/heavy/Dockerfile.pg-matrix"
V1_SOURCE_ARCHIVE="$ROOT_DIR/.pggraph-v1.0-source.tar.gz"
V1_1_SOURCE_ARCHIVE="$ROOT_DIR/.pggraph-v1.1-source.tar.gz"

cleanup() {
  rm -f "$V1_SOURCE_ARCHIVE" "${V1_SOURCE_ARCHIVE}."*
  rm -f "$V1_1_SOURCE_ARCHIVE" "${V1_1_SOURCE_ARCHIVE}."*
}
trap cleanup EXIT

v1_source_args=()
if [[ "$RUN_V1_UPDATE_ARTIFACT_ROLLBACK" == "1" ]]; then
  git -C "$ROOT_DIR" archive --format=tar.gz --output="$V1_SOURCE_ARCHIVE" v1.0.0 graph
  split -b 400k "$V1_SOURCE_ARCHIVE" "${V1_SOURCE_ARCHIVE}."
  v1_source_args=(
    --secret "id=v1_source_aa,src=${V1_SOURCE_ARCHIVE}.aa"
    --secret "id=v1_source_ab,src=${V1_SOURCE_ARCHIVE}.ab"
  )
fi

v1_1_source_args=()
if [[ "$RUN_V1_2_UPDATE_ARTIFACT_ROLLBACK" == "1" ]]; then
  git -C "$ROOT_DIR" archive --format=tar.gz --output="$V1_1_SOURCE_ARCHIVE" v1.1.0 graph
  split -b 400k "$V1_1_SOURCE_ARCHIVE" "${V1_1_SOURCE_ARCHIVE}."
  v1_1_source_args=(
    --secret "id=v1_1_source_aa,src=${V1_1_SOURCE_ARCHIVE}.aa"
    --secret "id=v1_1_source_ab,src=${V1_1_SOURCE_ARCHIVE}.ab"
    --secret "id=v1_1_source_ac,src=${V1_1_SOURCE_ARCHIVE}.ac"
  )
fi

docker build \
  ${v1_source_args[@]+"${v1_source_args[@]}"} \
  ${v1_1_source_args[@]+"${v1_1_source_args[@]}"} \
  --build-arg "PG_VERSIONS=${PG_VERSIONS}" \
  --build-arg "RUN_RUST_TESTS=${RUN_RUST_TESTS}" \
  --build-arg "RUN_PGRX_SQL=${RUN_PGRX_SQL}" \
  --build-arg "PGRX_TEST_FILTER=${PGRX_TEST_FILTER}" \
  --build-arg "RUN_GQL_WRITE_MATRIX=${RUN_GQL_WRITE_MATRIX}" \
  --build-arg "RUN_POSTGRES_SANITIZER=${RUN_POSTGRES_SANITIZER}" \
  --build-arg "RUN_DURABLE_PROJECTION_MATRIX=${RUN_DURABLE_PROJECTION_MATRIX}" \
  --build-arg "RUN_PACKAGE_INSTALL_MATRIX=${RUN_PACKAGE_INSTALL_MATRIX}" \
  --build-arg "RUN_OPEN_TYPE_PACKAGE_SMOKE=${RUN_OPEN_TYPE_PACKAGE_SMOKE}" \
  --build-arg "RUN_CRASH_MATRIX=${RUN_CRASH_MATRIX}" \
  --build-arg "RUN_RUNTIME_RESOURCES=${RUN_RUNTIME_RESOURCES}" \
  --build-arg "RUN_PG_UPGRADE_MATRIX=${RUN_PG_UPGRADE_MATRIX}" \
  --build-arg "RUN_V1_UPDATE_ARTIFACT_ROLLBACK=${RUN_V1_UPDATE_ARTIFACT_ROLLBACK}" \
  --build-arg "RUN_V1_2_UPDATE_ARTIFACT_ROLLBACK=${RUN_V1_2_UPDATE_ARTIFACT_ROLLBACK}" \
  --build-arg "PROFILE_TIMEOUT_SECONDS=${PROFILE_TIMEOUT_SECONDS}" \
  -f "$DOCKERFILE" \
  -t "$IMAGE" \
  "$ROOT_DIR"

echo "Docker PostgreSQL matrix passed for versions: $PG_VERSIONS"
