# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=smartypants
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=2.0.2
pkgrel=1
pkgdesc="Python with the SmartyPants (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/leohemsted/smartypants.py'
msys2_references=(
  'anitya: 20572'
  'archlinux: python-smartypants'
  'gentoo: dev-python/smartypants'
  'purl: pkg:pypi/smartypants'
)
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")
source=("https://github.com/leohemsted/${_realname}.py/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        fix-tests.patch)
sha256sums=('560ae7485a27c288f5bd63131f5c3c7b8f0745f50e7c3f85f0bbc65d87799d19'
            '9ad6df81da1683cea53dc4b9e1cb3d129a244ad1453afeb3e518bfb9e14552f0')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"/"${_realname}.py-${pkgver}"
  apply_patch_with_msg \
    fix-tests.patch
}

build() {
  cp -r "${_realname}.py-${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}"
  ${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 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"

  # This entire section should be removed if the package does NOT install
  # anything in the /mingw*/bin directory.
  ### begin section ###
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
  #### end section ####
}
