# Maintainer: Manuel Naranjo <naranjo.manuel@gmail.com>
# Contributor: dragon-archer <dragon-archer@outlook.com>

_pyname=psutil
_realname=${_pyname}
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=7.2.2
pkgrel=1
pkgdesc="A cross-platform process and system utilities module for Python (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/giampaolo/psutil"
msys2_references=(
  'purl: pkg:pypi/psutil'
  "cpe: cpe:2.3:a:psutil_project:psutil"
)
license=('spdx:BSD-3-Clause')
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")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest"
              "${MINGW_PACKAGE_PREFIX}-python-pytest-xdist"
              "${MINGW_PACKAGE_PREFIX}-python-pywin32"
              "${MINGW_PACKAGE_PREFIX}-python-psleak"
              "${MINGW_PACKAGE_PREFIX}-python-wmi")
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "0001-mingw-fixes-without-loadlib.patch"
        "0002-pyparsepid.patch"
        "0003-avoid-symbol-errors.patch")
sha256sums=('0746f5f8d406af344fd547f1c8daa5f5c33dbc293bb8d6a16d80b4bb88f59372'
            '6fc837c1d6d3a0480b3b1902082fbad62b1ff1db56731f013573fe387691e7f8'
            '3b8593dbd2f41ac7a558d547c88b6479072d499f3dc474ff49b4225ce35bf432'
            '50bf8bb37353c9ea6f27c66ad42165636b7df0a6a5129c27812573777993de18')

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

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  apply_patch_with_msg \
    0001-mingw-fixes-without-loadlib.patch

  # https://github.com/msys2/MINGW-packages/issues/18460#issuecomment-1717951357
  # CPython defines it as int, so we do as well
  apply_patch_with_msg \
    0002-pyparsepid.patch

  apply_patch_with_msg \
    0003-avoid-symbol-errors.patch

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

build() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  _platform=$(python -c "import sysconfig; print(sysconfig.get_platform())")
  _soabi=$(python -c "import sysconfig; print(sysconfig.get_config_var('py_version_nodot'))")
  cp "build/lib.${_platform}-cpython-${_soabi}/psutil/_psutil_windows.pyd" psutil
  ${MINGW_PREFIX}/bin/python -m pytest || warning "Tests failed"
}

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 "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"
}
