# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Maintainer: Joan Karadimov <joan.karadimov@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname='pywin32'
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
url='https://sourceforge.net/projects/pywin32'
pkgdesc='Python for Windows Extensions (mingw-w64)'
license=('PSF')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
pkgver=228
pkgrel=1
makedepends=(
    ${MINGW_PACKAGE_PREFIX}-python
    ${MINGW_PACKAGE_PREFIX}-python-setuptools
    rsync)
source=("https://github.com/mhammond/${_realname}/archive/b${pkgver}.zip"
        setup.cfg
        seh.h
        minmax.h
        001-compile-and-setup-fixes.patch
        002-dependency-fixes.patch
        003-seh.patch
        004-disable-modules.patch)
sha256sums=('524a40b85bd0d0bd52a5bcce7ccb236ae46a336975df5452b336d813f4b64a6d'
            '74033ac90498a63b579b4dd8c5948b2ea1a9de63aa57f3d4e4e44bb0f549520b'
            '03f8c0372059bb5b1933d77309101c67fa08504b42a6fce1186ed9a29e0c271f'
            '17eb1454e8955771af57209a49439f7cf26a885767a8e707f6c77e3c3ccd479f'
            'a6dc5c309148b85cd0b2e90f673d998528747e9fceab43d246bbe17ee7a4c8ad'
            'fcc23eaaff99160147db4dd48cf951b6db62adb8287807e7a281c032902c4f98'
            '109a3ece2cfa359b6e68aebbcc74db4b7e80eda0be3d9c6bed88a7d099c70d37'
            'e8c32de54fe2a375b0331fae36efaa4400518386d03ee429da13e1846b4c2e08')

prepare() {
    cd "${srcdir}/${_realname}-b${pkgver}"
    patch -p1 < "${startdir}"/001-compile-and-setup-fixes.patch
    patch -p1 < "${startdir}"/002-dependency-fixes.patch
    patch -p1 < "${startdir}"/003-seh.patch
    patch -p1 < "${startdir}"/004-disable-modules.patch
    cp "${startdir}"/setup.cfg setup.cfg
}

build() {
    msg2 "Synchronizing build directory"
    rsync --recursive --times "${srcdir}/${_realname}-b${pkgver}"/* "${srcdir}/build-python-${CARCH}-${_realname}-${pkgver}"
    cd "${srcdir}/build-python-${CARCH}-${_realname}-${pkgver}"

    CFLAGS="-Wno-narrowing -fno-operator-names -fpermissive -include ../seh.h -include ../minmax.h -DCRYPT_DECRYPT_MESSAGE_PARA_HAS_EXTRA_FIELDS" \
    LIB="${MINGW_PREFIX}/lib:${MINGW_PREFIX}/${CARCH}-w64-mingw32/lib" \
    INCLUDE="${MINGW_PREFIX}/include:${MINGW_PREFIX}/${CARCH}-w64-mingw32/include" \
    ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
    cd "${srcdir}/build-python-${CARCH}-${_realname}-${pkgver}"
    LIB="${MINGW_PREFIX}/lib:${MINGW_PREFIX}/${CARCH}-w64-mingw32/lib" \
    DISTUTILS_DEBUG=1 \
    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install \
        --prefix=${MINGW_PREFIX} \
        --root="${pkgdir}" \
        --optimize=1
}
