# Maintainer: Smo <smo@xpra.org>

_realname=rencode
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=1.0.8
pkgrel=2
pkgdesc="Python module for fast (basic) object serialization similar to bencode (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/aresch/rencode'
msys2_references=(
  "cpe: cpe:/a:rencode_project:rencode"
  "purl: pkg:pypi/rencode"
)
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-poetry-core"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-cc")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest")
options=('!strip')
source=("https://github.com/aresch/rencode/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        0001-Remove-extra-compile-args.patch
        python-rencode-typecode-dos.patch)
sha256sums=('480aab74948a7f339b749b5c39bdb4caf15429f4b49a998c770d5f371098d351'
            'cb70f0a73164b4e676aee990c0cfb2fe8789b07569aaa161a469ccf828986a0c'
            '72046868ae82f7c9576fb9160bcf686cc893b5f104ee8b126c36466d06f21eb7')

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

  # https://github.com/aresch/rencode/issues/38
  patch -p1 -i ../0001-Remove-extra-compile-args.patch
}

build() {
  # Running in the source dir won't work, as
  # ./build.py will be ran instead of python-build
  python -m build --wheel --skip-dependency-check --no-isolation "python-build-${MSYSTEM}"
}

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

  python -m venv --system-site-packages testenv
  testenv/bin/python -m installer dist/*.whl
  testenv/bin/python -m pytest
}

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

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

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"
}
