# Contributor: Josip <bpisoj@gmail.com>

_realname=pyicu
pkgbase=mingw-w64-python-icu
pkgname=("${MINGW_PACKAGE_PREFIX}-python-icu")
pkgver=2.15.3
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'
  'purl: pkg:pypi/pyicu'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-cc-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=('f32e78e1cb64d0aeb14f027e037a8944861d3114548818a6adf0081ef51aefc3'
            '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"
}
