# Maintainer: Patrick Stewart <patstew@gmail.com>

_realname=pyzmq
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=19.0.2
pkgrel=1
pkgdesc="Python bindings for zeromq, written in Cython (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.zeromq.org/languages/python"
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-zeromq")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cython")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/zeromq/${_realname}/archive/v${pkgver}.tar.gz"
        "001-mingw-python.patch"
        "002-mingw-win-compat.patch")
sha256sums=('ed67b5ffe8a87c88c5a19bf6f2d64ca7e3c325a2f6cc30f989f5f57d2205bada'
            '3106e8f70443154aa978b3fe1928774624f1ec21574dd7df9aa4699199afd43a'
            'c893ceafb7e940452689b644c8372281e900e48dc1fc7f8fff14f89a65277b64')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/001-mingw-python.patch
  patch -p1 -i ${srcdir}/002-mingw-win-compat.patch

  cd "${srcdir}"
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build --zmq=${MINGW_PREFIX}
}

package() {
  cd "${srcdir}/python-build-${CARCH}"

  sed -i 's|except socket.error, e:|except socket.error as e:|' zmq/eventloop/ioloop.py
  sed -i 's|except gevent.Timeout, t:|except gevent.Timeout as t:|' zmq/green/core.py

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install \
    --prefix=${MINGW_PREFIX} --root=${pkgdir} --optimize=1 --skip-build

  find "${pkgdir}/" -name "*.pyc" -delete
  find "${pkgdir}/" -type d -empty -delete

  rm -rf build

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix absolute paths in config files
  find "${pkgdir}${MINGW_PREFIX}" -name "*.json" -exec sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i {} \;
}
