# Contributor: Josip <bpisoj@gmail.com>

_realname=PyICU
pkgbase=mingw-w64-python-icu
pkgname=("${MINGW_PACKAGE_PREFIX}-python-icu")
provides=("${MINGW_PACKAGE_PREFIX}-python3-icu")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-icu")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-icu")
pkgver=2.14
pkgrel=1
pkgdesc="Python extension wrapping the ICU C++ API (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gitlab.pyicu.org/main/pyicu"
msys2_references=(
  'archlinux: python-pyicu'
  'pypi: PyICU'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-icu")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://pypi.io/packages/source/P/${_realname}/${_realname}-${pkgver}.tar.gz"
        001-icu-config.patch
        002-fix-mingw-build.patch)
sha256sums=('acc7eb92bd5c554ed577249c6978450a4feda0aa6f01470152b3a7b382a02132'
            '3e0e8155c2301c53b6e78a86a5de14ee3a958533c819bac435719eeb554216a0'
            '49cd497bf1e6f63128f801d5af12e94231c2f7a67154539f1319409068ef8872')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i ${srcdir}/001-icu-config.patch
  patch -p1 -i ${srcdir}/002-fix-mingw-build.patch
}

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

  export PYICU_LFLAGS="-L${MINGW_PREFIX}/lib"
  export PYICU_INCLUDES="${MINGW_PREFIX}/include"
  export PYICU_LIBRARIES="icuin;icuuc;icudt"

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

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-icu/COPYING"
}
