#!/bin/bash
set -e

BASEDIR=$(dirname $(realpath "$0"))
REPOSITORY="ghcr.io/zachasme/h3-pg"

# grab extension version from `h3.control`
EXTVERSION=$(grep default_version h3.control | \
	sed -e "s/default_version[[:space:]]*=[[:space:]]*'\([^']*\)'/\1/")

# i386 being phased out from postgres apt :-(
#ARCHS=(amd64 i386)
ARCHS=(amd64)
UBUNTUS=(jammy focal) # latest and LTS
POSTGRESQLS=(14 13) # two latest

cd $BASEDIR

printhelp () { echo \
"Usage: $0 [-a i386] [-u bionic] [-g 12] -b|-p|-t

Options:
  -a   Architecture (amd64 or i386)
  -u   Ubuntu release (focal, bionic, etc.)
  -g   PostgreSQL version (15, 14, etc.)

Commands
  -b   Build images
  -p   Push images
  -t   Run tests"

  exit 0;
}

while getopts ':hbptiva::u::g::c::' o; do
case "${o}" in
  a)  # set arch
      ARCHS=($OPTARG)
      ;;
  u)  # set release name
      UBUNTUS=($OPTARG)
      ;;
  g)  # set postgresql version
      POSTGRESQLS=($OPTARG)
      ;;

  b)  # build images
      work=build
      for postgresql in "${POSTGRESQLS[@]}"; do
        for ubuntu in "${UBUNTUS[@]}"; do
          for arch in "${ARCHS[@]}"; do
            echo "=============================="
            echo "$postgresql-$ubuntu-$arch"
            docker build \
              --tag $REPOSITORY/test:$postgresql-$ubuntu-$arch \
              --platform linux/$arch \
              --build-arg POSTGRESQL=$postgresql \
              --build-arg UBUNTU=$ubuntu \
              --build-arg ARCH=$arch \
              .
          done
        done
      done
      ;;

  p)  # push images
      work=push
      for postgresql in "${POSTGRESQLS[@]}"; do
        for ubuntu in "${UBUNTUS[@]}"; do
          for arch in "${ARCHS[@]}"; do
            echo "=============================="
            echo "$postgresql-$ubuntu-$arch"
            docker push \
              $REPOSITORY/test:$postgresql-$ubuntu-$arch
          done
        done
      done
      ;;

  t)  # run tests
      work=test
      for postgresql in "${POSTGRESQLS[@]}"; do
        for ubuntu in "${UBUNTUS[@]}"; do
          for arch in "${ARCHS[@]}"; do
            echo "=============================="
            echo "$postgresql-$ubuntu-$arch"
            docker run \
              --rm \
              --platform linux/$arch \
              -v "$PWD"/../..:/github/workspace \
              $REPOSITORY/test:$postgresql-$ubuntu-$arch
          done
        done
      done
      ;;

  i)  # try pgxnclient install
      work=pgxn
      for postgresql in "${POSTGRESQLS[@]}"; do
        for ubuntu in "${UBUNTUS[@]}"; do
          for arch in "${ARCHS[@]}"; do
            echo "=============================="
            echo "$postgresql-$ubuntu-$arch"
            docker run \
              --rm \
              --platform linux/$arch \
              -v "$PWD"/../..:/github/workspace \
              $REPOSITORY/test:$postgresql-$ubuntu-$arch \
              pgxn "./h3-${EXTVERSION}.zip"
          done
        done
      done
      ;;

  v)  # validate upgrade path
      work=pg_validate_extupgrade
      for postgresql in "${POSTGRESQLS[@]}"; do
        for ubuntu in "${UBUNTUS[@]}"; do
          for arch in "${ARCHS[@]}"; do
            echo "=============================="
            echo "$postgresql-$ubuntu-$arch"
            docker run \
              --rm \
              --platform linux/$arch \
              -v "$PWD"/../..:/github/workspace \
              $REPOSITORY/test:$postgresql-$ubuntu-$arch \
              "pg_validate_extupgrade --host /var/run/postgresql --config h3/pg_validate_extupgrade.toml && \
              pg_validate_extupgrade --host /var/run/postgresql --config h3_postgis/pg_validate_extupgrade.toml"
          done
        done
      done
      ;;

  c)  # run cmd
      work=cmd
      CMD=($OPTARG)
      for postgresql in "${POSTGRESQLS[@]}"; do
        for ubuntu in "${UBUNTUS[@]}"; do
          for arch in "${ARCHS[@]}"; do
            echo "=============================="
            echo "$postgresql-$ubuntu-$arch"
            docker run \
              --rm \
              --platform linux/$arch \
              -v "$PWD"/../..:/github/workspace \
              $REPOSITORY/test:$postgresql-$ubuntu-$arch \
              $CMD
          done
        done
      done
      ;;

  *)  # print help
      printhelp
      exit 1;;
  esac
done

shift $((OPTIND-1))

# print help if no actual work was done
if [ -z "${work}" ]; then
  printhelp
fi