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

_realname=postgresql
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-pg_probackup")
pkgbase=mingw-w64-${_realname}
_backupver=2.5.15
_pg_ver=17.5
pkgver=${_pg_ver}
_back_commit='911553be3d5b60e17c7ead9d099aa4e4cb655e25'
pkgrel=4
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"
             "git")
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"
        "git+https://github.com/Alexpux/pg_probackup#commit=$_back_commit"
        postgresql-13.1-disable-wsa-invalid-event-static-assert.patch
        postgresql-14.0-use-mingw-setjmp-on-ucrt.patch
        postgresql-add-pgprobackup.patch)
sha256sums=('fcb7ab38e23b264d1902cb25e6adafb4525a6ebcbd015434aeef9eda80f528d8'
            'bdc0b4f495420955400cfa85aa4c32d1a1853ec5ae1b1f5cc8998087f8270bcc'
            '72c14a78eeafdd3c9a13c3e124b1941b5da090488c7bd73f08b3cd78bacd07d5'
            'fe336eac7892b23988189be0178efc95449d8d08493db83754058f5f37bb8ea9'
            '5cd42ed05d21e309f1d591828bffc16e6acd8623b5ca59629e6cdb30c2a08318')

# 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}
  
  cp -rf "${srcdir}"/pg_probackup "${srcdir}"/${_realname}-${pkgver}/src/bin

  apply_patch_with_msg \
    postgresql-13.1-disable-wsa-invalid-event-static-assert.patch \
    postgresql-14.0-use-mingw-setjmp-on-ucrt.patch \
    postgresql-add-pgprobackup.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_postgresql() {
  pkgdesc="Libraries for use with PostgreSQL (mingw-w64)"
  
  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
  
  # Remove pg_probackup files
  rm -f "${pkgdir}"${MINGW_PREFIX}/bin/pg_probackup.exe
  find "${pkgdir}"${MINGW_PREFIX}/share -iname "pg_probackup*"  -exec rm {} \;

  # 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
}

package_pg_probackup() {
  pkgdesc="Utility to manage backup and recovery of PostgreSQL database clusters (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-postgresql=${_pg_ver}")
  # pkgver=${_backupver}
  url="https://github.com/postgrespro/pg_probackup/"
  
  mkdir -p "${pkgdir}"${MINGW_PREFIX}/{bin,share/locale}

  install -D -m 755 "${srcdir}"/build-${MSYSTEM}/src/bin/pg_probackup/pg_probackup.exe "${pkgdir}/${MINGW_PREFIX}/bin/"
  cp -rf "${srcdir}"/build-${MSYSTEM}/src/bin/pg_probackup/po/. "${pkgdir}/${MINGW_PREFIX}/share/locale/"
  
  install -D -m 644 "${srcdir}"/pg_probackup/LICENSE "${pkgdir}/${MINGW_PREFIX}/share/licenses/pg_probackup/LICENSE"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
