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

_realname=postgresql
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=13.1
pkgrel=1
pkgdesc="Libraries for use with PostgreSQL (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.postgresql.org/"
license=('custom:PostgreSQL')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-tcl"
         #"${MINGW_PACKAGE_PREFIX}-readline"
         #"${MINGW_PACKAGE_PREFIX}-wineditline"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "winpty")
options=('staticlibs' 'strip')
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)
sha256sums=('12345c83b89aa29808568977f5200d6da00f88a035517f925293355432ffe61f'
            '607217b422349770d25af20f88e4a7925e68bb934232dff368c2ee59f24249a4'
            '57c1e9b75c042af591b05b9dda60e6327b5c364bb5adc2675da8a48b47e11b81'
            '1afbe207b0fe8c4178cc3f8cb4e3923c7c000207d22ec4f3875d6358b312a1d5'
            'ab9c42374b4e8a01b598810b19b583d9ee7bf5c43c39c019f66b62aacac38926'
            '51c72fbd380d23cf944165405221912a277b9be99e285479772b39cacbbf384f')

prepare() {
  cd ${srcdir}/postgresql-${pkgver}
  patch -p1 -i ${srcdir}/postgresql-12.0-mingw-link.patch
  patch -p1 -i ${srcdir}/postgresql-9.5.1-pl-perl.patch
  patch -p1 -i ${srcdir}/postgresql-9.5.1-pl-python.patch
  patch -p1 -i ${srcdir}/postgresql-9.4.1-pl-tcl.patch
  #patch -p1 -i ${srcdir}/postgresql-9.4.1-mingw-enable-readline.patch

  sed -s "s|2\\.69|2\\.71|g" -i configure.in
  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${CARCH}" ]] && rm -rf "${srcdir}/build-${CARCH}"
  mkdir -p "${srcdir}/build-${CARCH}" && cd "${srcdir}/build-${CARCH}"
  export PYTHON=${MINGW_PREFIX}/bin/python
  ../postgresql-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --with-openssl \
    --enable-thread-safety \
    --enable-integer-datetimes \
    --enable-nls \
    --disable-rpath \
    --with-ldap \
    --with-libxml \
    --with-libxslt \
    --with-python \
    --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
}

package() {
  cd "${srcdir}/build-${CARCH}"
  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
    _exename="${f}"
    echo "#!/usr/bin/env bash" > "${pkgdir}${MINGW_PREFIX}/bin/${_exename}"
    echo '/usr/bin/winpty "$( dirname ${BASH_SOURCE[0]} )/'${_exename}'.exe" "$@"' >> "${pkgdir}${MINGW_PREFIX}/bin/${_exename}"
    mv "${pkgdir}"${MINGW_PREFIX}/bin/${f}_exe "${pkgdir}"${MINGW_PREFIX}/bin/${f}.exe
  done
}
