# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Maintainer: Naveen M K <naven@syrusdark.website>

_realname=distlib
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.4.0
pkgrel=1
pkgdesc="Low-level components of distutils2/packaging (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'purl: pkg:pypi/distlib'
)
url="https://github.com/pypa/distlib"
license=('spdx:PSF-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://files.pythonhosted.org/packages/source/${_realname:0:1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "003-launcher-secure-api-shim.patch"
        "004-launcher-fix-gcc.patch")
sha256sums=('feec40075be03a04501a973d81f633735b4b69f98b05450592310c0f401a4e0d'
            'f90de01cd05cf901e9ac375e5f92727b83cb90ce3847d6ac0a18e504cd3793ca'
            '9381f6c1dbd8afe3d6e7654979bd3f7a68ad8fe4c0a2b3c529d19f9f04eecc19')

prepare() {
  cd "${srcdir}"
  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"

  cd "python-build-${MSYSTEM}"
  patch -Np1 -i "${srcdir}/003-launcher-secure-api-shim.patch"
  patch -Np1 -i "${srcdir}/004-launcher-fix-gcc.patch"
}

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

  rm -f distlib/{w,t}{32,64}{,-arm}.exe
  local _gui_cflags="-DWIN32 -DNDEBUG -D_WINDOWS -mwindows -O2"
  local _cli_cflags="-DWIN32 -DNDEBUG -D_CONSOLE -O2"
  local _gui_lflags="-lshlwapi"
  local _cli_lflags="-lshlwapi"
  if check_option "strip" "y"; then
    _gui_cflags+=" -s"
    _cli_cflags+=" -s"
  fi

  windres --input PC/launcher.rc --output PC/resources.res --output-format=coff

  case "${MINGW_CHOST}" in
  i686-w64-mingw32)
    cc $_cli_cflags -o distlib/t32.exe PC/launcher.c PC/resources.res $_cli_lflags
    cc $_gui_cflags -o distlib/w32.exe PC/launcher.c PC/resources.res $_gui_lflags
    ;;
  x86_64-w64-mingw32)
    cc $_cli_cflags -o distlib/t64.exe PC/launcher.c PC/resources.res $_cli_lflags
    cc $_gui_cflags -o distlib/w64.exe PC/launcher.c PC/resources.res $_gui_lflags
    ;;
  aarch64-w64-mingw32)
    cc $_cli_cflags -o distlib/t64-arm.exe PC/launcher.c PC/resources.res $_cli_lflags
    cc $_gui_cflags -o distlib/w64-arm.exe PC/launcher.c PC/resources.res $_gui_lflags
    ;;
  *)
    echo "Unsupported CHOST ${MINGW_CHOST}" && false
    ;;
  esac

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  msg "Python test for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m pytest
}

package() {
  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"
}
