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

_realname=postgresql
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=16.2
pkgrel=1
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'
)
license=('spdx:PostgreSQL')
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")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${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")
optdepends=("${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-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)
sha256sums=('446e88294dbc2c9085ab4b7061a646fa604b4bec03521d5ea671c2e5ad9b2952'
            '54958a6116e401d13b2394c7b428a039d8a185bf2aaedf7b89e3ee203dc8f324'
            '99e6c8b9d58a9615c7bb1e7471b8e9c074add7bffc7d2cf742bafa38e9654e1f'
            'ced0ccf978eefc1ad0641803c9759b652e588eb552ec9179cd9dfc9d94ecf499'
            'b12c4e493845b3ead8950bbc26b1cc7bd4fb0dc3313fea4f7b281866d18c0402'
            'cd52d56833c60afdce98d91eece73c11978762671e36be7bd9c31a1ea0a0282d'
            '72c14a78eeafdd3c9a13c3e124b1941b5da090488c7bd73f08b3cd78bacd07d5'
            '5941c74c6ab66d42d6ac6e7d5da358d44e479fd519bd4e025f08eba06bf30752'
            '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}/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
  #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 \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --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() {
  cd "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/"{bin,include,lib}

  make DESTDIR="${pkgdir}" install
  # install libs

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

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

  # Rename the .a files to .dll.a as they're actually import libraries and not static libraries
  #for implib in "${pkgdir}${MINGW_PREFIX}/lib/"*.a; do
  #  mv $implib ${implib/.a/.dll.a}
  #done

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

  # these headers are needed by the public headers of the interfaces
  #  install -m644 pg_config.h "${pkgdir}${MINGW_PREFIX}/include/"
  #  install -m644 pg_config_os.h "${pkgdir}${MINGW_PREFIX}/include/"
  #  install -m644 pg_config_ext.h "${pkgdir}${MINGW_PREFIX}/include/"
  #  cd "${srcdir}/postgresql-$pkgver/src/include"
  #  install -m644 postgres_ext.h "${pkgdir}${MINGW_PREFIX}/include/"
  #  install -m644 libpq/libpq-fs.h "${pkgdir}${MINGW_PREFIX}/include/libpq/"
  #  install -m644 pg_config_manual.h "${pkgdir}${MINGW_PREFIX}/include/"

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