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

_realname=postgresql
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=11.1
pkgrel=1
pkgdesc="Libraries for use with PostgreSQL (mingw-w64)"
arch=('any')
url="https://www.postgresql.org/"
license=('custom:PostgreSQL')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-python2")
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}-python2"
         "${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-9.4.1-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=('90815e812874831e9a4bf6e1136bf73bc2c5a0464ef142e2dfea40cda206db08'
            'c740a8f308abc85f460ca170969feb1ec41e99072f0e06ef44a63173b26fd7b5'
            '57c1e9b75c042af591b05b9dda60e6327b5c364bb5adc2675da8a48b47e11b81'
            '1afbe207b0fe8c4178cc3f8cb4e3923c7c000207d22ec4f3875d6358b312a1d5'
            'ab9c42374b4e8a01b598810b19b583d9ee7bf5c43c39c019f66b62aacac38926'
            '51c72fbd380d23cf944165405221912a277b9be99e285479772b39cacbbf384f')

prepare() {
  cd ${srcdir}/postgresql-${pkgver}
  patch -p1 -i ${srcdir}/postgresql-9.4.1-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

  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/python2
  ../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

  # Make DLL definition file visible during each arch build
  ln -s "${srcdir}/postgresql-${pkgver}/src/interfaces/libpq/libpqdll.def" src/interfaces/libpq/
  ln -s "${srcdir}/postgresql-${pkgver}/src/interfaces/ecpg/ecpglib/libecpgdll.def" src/interfaces/ecpg/ecpglib/
  ln -s "${srcdir}/postgresql-${pkgver}/src/interfaces/ecpg/pgtypeslib/libpgtypesdll.def" src/interfaces/ecpg/pgtypeslib/
  ln -s "${srcdir}/postgresql-${pkgver}/src/interfaces/ecpg/compatlib/libecpg_compatdll.def" src/interfaces/ecpg/compatlib/

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