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

_realname=pywin32
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
pkgver=310
pkgrel=1
pkgdesc='Python for Windows Extensions (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/mhammond/pywin32'
msys2_references=(
  'purl: pkg:pypi/pywin32'
)
license=('spdx:PSF-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             $([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "binutils")
             "${MINGW_PACKAGE_PREFIX}-cc")
source=(https://github.com/mhammond/pywin32/archive/b${pkgver}/${_realname}-${pkgver}.tar.gz
        001-compile-and-setup-fixes.patch
        002-com-extensions-fix.patch
        003-win32-extensions-fix.patch
        004-isapi-fix.patch
        005-swig-fix.patch
        006-mapi-headers-fix.patch)
sha256sums=('b3e07c8c1383c0d6e75a360ed5d0d85edce30e6d7b67e9a070f0056156e1c5a3'
            '7e7a8f53a8440d877bade76be3bc9ee15516dfdffc30d4795bae8e595bbb5fd7'
            'dbb2204aa69ab07fbd3cfe9afe0262c1d59f815af4dfc4b7a6a7f6f242144260'
            '5317c09132110f710cca730e321b313f2726ae35f8a22b76af4329f552f32026'
            '0d303188a4a34759e4be1c2e11d68ba33f40edaf3056656618490c983d932dff'
            'cfe20ab0034b7f872d96363ea85261430f6a64ae403831978bd433c20415566c'
            'c8ba874b0b7b960b6bc4aba251e4a55edf39d92982ab8e038804975f57eb305c')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}-b${pkgver}"

  apply_patch_with_msg \
    001-compile-and-setup-fixes.patch \
    002-com-extensions-fix.patch \
    003-win32-extensions-fix.patch \
    004-isapi-fix.patch \
    005-swig-fix.patch \
    006-mapi-headers-fix.patch
}

build() {
  cp -r "${_realname}-b${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 "win32/License.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE.txt"
}
