# Maintainer: Sarah Ottinger <schalaalexiazeal@gmail.com>

_realname=jsonpatch
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=1.33
pkgrel=2
pkgdesc='An implementation of the JSON Patch format (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'archlinux: python-jsonpatch'
  'pypi: jsonpatch'
)
msys2_documentation_url='https://python-json-patch.readthedocs.io/'
url="https://github.com/stefankoegl/python-json-patch"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-python-jsonpointer")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-coverage")
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz")
sha256sums=('9fcd4009c41e6d12348b4a0ff2563ba56a2923a7dfee731d004e212e1ee5030c')

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/coverage run --source=jsonpatch tests.py || 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

  # Undo path translation
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*; do
    sed -e '1 { s/^#!.*python.*$/#!\/usr\/bin\/env python3/ }' -i ${_f}
  done

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