# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=py2exe
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
pkgver=0.13.0.2
pkgrel=1
pkgdesc='A distutils extension to create standalone Windows programs from Python code (mingw-w64)'
arch=('any')
# py2exe is currently incompatible to Python 3.12 or newer:
# https://github.com/py2exe/py2exe/issues/191
# Re-enable if this is fixed upstream.
#mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
mingw_arch=()
msys2_references=(
  'purl: pkg:pypi/py2exe'
)
license=('spdx:MIT')
url='https://github.com/py2exe/py2exe'
depends=(
    "${MINGW_PACKAGE_PREFIX}-python"
    "${MINGW_PACKAGE_PREFIX}-python-cachetools"
    "${MINGW_PACKAGE_PREFIX}-python-pefile"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-python-build"
  "${MINGW_PACKAGE_PREFIX}-python-installer"
  "${MINGW_PACKAGE_PREFIX}-python-setuptools"
  "${MINGW_PACKAGE_PREFIX}-cc")
source=(https://github.com/py2exe/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        001-setup-fix.patch
        002-mingw-fix.patch)
sha256sums=('ef2907f5b82ec8b2259bea41a2d7d798ecd67aa4a92a74a06550b9123dadd8a4'
            'dabe972096c34f5e9ffd0023794807cc665a8870cd6bef6985f9d7197ec4d717'
            '3ac8b13f12ca901a07f3ca87b494f5c97ea31192715b6f1fb36563b458462a0f')

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

build() {
  # build separate zipextimporter wheel for _memimporter pyd (#20382)
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}-zipextimporter"
  pushd "${srcdir}/python-build-${MSYSTEM}-zipextimporter"
  mv setup_zipextimporter.py setup.py
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
  popd

  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}-zipextimporter"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  local _pythonpath=`python -c "import sysconfig; print(sysconfig.get_path('platlib'))"`
  local _site_packages="${pkgdir}$(cygpath ${_pythonpath})"
  rm -rf "${_site_packages}"/zipextimporter*

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

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