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

_realname=postgresql
_backupver=2.5.15
_pg_ver=16.9
pkgbase=mingw-w64-${_realname}-${_pg_ver%%.*}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-${_pg_ver%%.*}"
         "${MINGW_PACKAGE_PREFIX}-pg_probackup-${_pg_ver%%.*}")
pkgver=${_pg_ver}
_back_commit='911553be3d5b60e17c7ead9d099aa4e4cb655e25'
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}-autotools"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${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-12.0-mingw-link.patch
        postgresql-9.5.1-pl-perl.patch
        postgresql-9.5.1-pl-python.patch
        postgresql-9.4.1-pl-tcl.patch
        postgresql-9.4.1-mingw-enable-readline.patch
        postgresql-13.1-disable-wsa-invalid-event-static-assert.patch
        postgresql-13.1-pgevent-def.patch
        postgresql-14.0-use-mingw-setjmp-on-ucrt.patch
        postgresql-add-pgprobackup.patch)
sha256sums=('07c00fb824df0a0c295f249f44691b86e3266753b380c96f633c3311e10bd005'
            'bdc0b4f495420955400cfa85aa4c32d1a1853ec5ae1b1f5cc8998087f8270bcc'
            '54958a6116e401d13b2394c7b428a039d8a185bf2aaedf7b89e3ee203dc8f324'
            '99e6c8b9d58a9615c7bb1e7471b8e9c074add7bffc7d2cf742bafa38e9654e1f'
            'ced0ccf978eefc1ad0641803c9759b652e588eb552ec9179cd9dfc9d94ecf499'
            'b12c4e493845b3ead8950bbc26b1cc7bd4fb0dc3313fea4f7b281866d18c0402'
            'cd52d56833c60afdce98d91eece73c11978762671e36be7bd9c31a1ea0a0282d'
            '72c14a78eeafdd3c9a13c3e124b1941b5da090488c7bd73f08b3cd78bacd07d5'
            '5941c74c6ab66d42d6ac6e7d5da358d44e479fd519bd4e025f08eba06bf30752'
            '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}
  cp -rf ${srcdir}/pg_probackup ${srcdir}/postgresql-${pkgver}/src/bin/

  cd ${srcdir}/postgresql-${pkgver}
  apply_patch_with_msg \
    postgresql-12.0-mingw-link.patch \
    postgresql-9.5.1-pl-perl.patch \
    postgresql-9.5.1-pl-python.patch \
    postgresql-9.4.1-pl-tcl.patch \
    postgresql-13.1-disable-wsa-invalid-event-static-assert.patch \
    postgresql-13.1-pgevent-def.patch \
    postgresql-14.0-use-mingw-setjmp-on-ucrt.patch \
    postgresql-add-pgprobackup.patch
  #patch -p1 -i ${srcdir}/postgresql-9.4.1-mingw-enable-readline.patch
  
  autoreconf -fiv
}

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

  export PYTHON=${MINGW_PREFIX}/bin/python

  ../postgresql-${pkgver}/configure \
    --prefix=${MINGW_PREFIX}/opt/pg-${_pg_ver%%.*} \
    --enable-thread-safety \
    --enable-integer-datetimes \
    --enable-nls \
    --disable-rpath \
    --with-ldap \
    --with-libxml \
    --with-libxslt \
    --with-lz4 \
    --with-ssl=openssl \
    --with-python \
    --with-zstd \
    --without-perl \
    --with-tcl \
    --with-tclconfig=${MINGW_PREFIX}/lib
    #--with-libedit-preferred

  #for dir in src/interfaces src/bin/pg_config src/bin/psql src/bin/pg_dump; do
  #  make -C ${dir}
  #done

  make
}

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

package_postgresql-16() {
  cd "${srcdir}/build-${MSYSTEM}"
  local _prefix=${MINGW_PREFIX}/opt/pg-${_pg_ver%%.*}
  mkdir -p "${pkgdir}${_prefix}/"{bin,include,lib}

  make DESTDIR="${pkgdir}" install


  # Move dll's to bin directory
  mv "${pkgdir}${_prefix}/lib/"*.dll "${pkgdir}${_prefix}/bin/"

 # Remove pg_probackup files
  rm -f "${pkgdir}${_prefix}"/bin/pg_probackup.exe
  find "${pkgdir}${_prefix}"/share -iname "pg_probackup*"  -exec rm {} \;

  # these headers are needed by the not-so-public headers of the interfaces
  mkdir -p "${pkgdir}${_prefix}"/include/{libpq,postgresql/internal/libpq}
  install -m644 ${srcdir}/postgresql-${pkgver}/src/include/c.h "${pkgdir}${_prefix}/include/postgresql/internal/"
  install -m644 ${srcdir}/postgresql-${pkgver}/src/include/port.h "${pkgdir}${_prefix}/include/postgresql/internal/"
  install -m644 ${srcdir}/postgresql-${pkgver}/src/include/postgres_fe.h "${pkgdir}${_prefix}/include/postgresql/internal/"
  install -m644 ${srcdir}/postgresql-${pkgver}/src/include/libpq/pqcomm.h "${pkgdir}${_prefix}/include/postgresql/internal/libpq/"

  # 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}${_prefix}"/bin/${f}.exe "${pkgdir}${_prefix}"/bin/${f}_exe
    cat > "${pkgdir}${_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}${_prefix}/bin/${f}"
    mv "${pkgdir}${_prefix}"/bin/${f}_exe "${pkgdir}${_prefix}"/bin/${f}.exe
  done
}

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

  install -D -m 755 "${srcdir}"/build-${MSYSTEM}/src/bin/pg_probackup/pg_probackup.exe "${pkgdir}${_prefix}/bin/"
  cp -rf "${srcdir}"/build-${MSYSTEM}/src/bin/pg_probackup/po/. "${pkgdir}${_prefix}/share/locale/"
  
  install -D -m 644 "${srcdir}"/pg_probackup/LICENSE "${pkgdir}${_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;
