# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=postgresql
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=17.2
pkgrel=3
pkgdesc="Libraries for use with PostgreSQL (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.postgresql.org/"
msys2_repository_url="https://git.postgresql.org/gitweb/?p=postgresql.git"
msys2_references=(
  'archlinux: postgresql'
  'cpe: cpe:/a:postgresql:postgresql'
)
license=('spdx:PostgreSQL')
depends=("${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-readline"
         #"${MINGW_PACKAGE_PREFIX}-wineditline"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd"
         "winpty")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             #"${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-perl"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-tcl")
optdepends=("${MINGW_PACKAGE_PREFIX}-libxslt: XML plugin"
            "${MINGW_PACKAGE_PREFIX}-perl: for PL/Perl support"
            "${MINGW_PACKAGE_PREFIX}-python: for PL/Python support"
            "${MINGW_PACKAGE_PREFIX}-tcl: for PL/Tcl support")
source=("https://ftp.postgresql.org/pub/source/v${pkgver}/postgresql-${pkgver}.tar.bz2"
        postgresql-13.1-disable-wsa-invalid-event-static-assert.patch
        postgresql-14.0-use-mingw-setjmp-on-ucrt.patch)
sha256sums=('82ef27c0af3751695d7f64e2d963583005fbb6a0c3df63d0e4b42211d7021164'
            '72c14a78eeafdd3c9a13c3e124b1941b5da090488c7bd73f08b3cd78bacd07d5'
            'fe336eac7892b23988189be0178efc95449d8d08493db83754058f5f37bb8ea9')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  apply_patch_with_msg \
    postgresql-13.1-disable-wsa-invalid-event-static-assert.patch \
    postgresql-14.0-use-mingw-setjmp-on-ucrt.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--buildtype=release")
  else
    _extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson setup \
    --prefix=${MINGW_PREFIX} \
    --default-library=shared \
    -Drpath=false \
    -Dicu=enabled \
    -Dldap=enabled \
    -Dlibxml=enabled \
    -Dlibxslt=enabled \
    -Dllvm=disabled \
    -Dlz4=enabled \
    -Dnls=enabled \
    -Dplperl=enabled \
    -Dplpython=enabled \
    -Dpltcl=enabled \
    -Dreadline=enabled \
    -Dssl=openssl \
    -Dzlib=enabled \
    -Dzstd=enabled \
    "${_extra_config[@]}" \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson compile
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/meson test
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install

  # Remove unneeded import libraries for plugins/modules
  rm -f "${pkgdir}"${MINGW_PREFIX}/lib/postgresql/*.dll.a

  # Use winpty-git script to invoke utilities. Please don't move this into a patch as
  # hopefully one day we won't need this hack.
  for f in clusterdb createdb createuser dropdb dropuser initdb pg_basebackup pg_dump pg_dumpall pg_receivewal pg_restore psql reindexdb vacuumdb; do
    mv "${pkgdir}"${MINGW_PREFIX}/bin/${f}.exe "${pkgdir}"${MINGW_PREFIX}/bin/${f}_exe
    cat > "${pkgdir}${MINGW_PREFIX}/bin/${f}" <<END
#!/usr/bin/env bash

EXE="\$( dirname \${BASH_SOURCE[0]} )/${f}.exe"

if [ -t 0 -a -t 1 -a -x /usr/bin/winpty ]; then
  /usr/bin/winpty \$EXE "\$@"
else
  exec \$EXE "\$@"
fi
END
    chmod +x "${pkgdir}${MINGW_PREFIX}/bin/${f}"
    mv "${pkgdir}"${MINGW_PREFIX}/bin/${f}_exe "${pkgdir}"${MINGW_PREFIX}/bin/${f}.exe
  done
}
