# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=pivy
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.6.8
pkgrel=4
pkgdesc='Python bindings to Coin3D (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url='https://github.com/coin3d/pivy'
msys2_references=(
  'archlinux: python-pivy'
)
license=('spdx:ISC')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         $([[ ${CARCH} != i686 ]] && echo \
           "${MINGW_PACKAGE_PREFIX}-soqt-qt6" || echo \
           "${MINGW_PACKAGE_PREFIX}-soqt-qt5"))
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-wheel"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${pkgver}.tar.gz"::"${url}/archive/${pkgver}.tar.gz"
        "001-pivy-0.6.5-setup-py-msystem-env.patch"
        "002-pivy-0.6.8-fake-header-cmath.patch"
        "003-find-qt6.patch"
        "004-fix-compile-flags.patch"
        "005-fix-install-destination.patch")
sha256sums=('c443dd7dd724b0bfa06427478b9d24d31e0c3b5138ac5741a2917a443b28f346'
            '0a37cf9f25ed3b0aca4547b484f448c840cf6a4d1f604ceed7094f8970a9c5b5'
            'b0132c7cf459c15ab8d35dda1e418140932968b351b325fcccb22e8c05362147'
            '653d16af80433e4541e7a5814247b9eccafa2156ce5e10b1fe0fc8c4b4e64a18'
            'e48a9c48999fa6fe01a913357407f13242727cfc350b963b14818c7ed0b348a6'
            'eb9fc0057ace20864aba5faaa68a2feaffef1a6abb948fc21958040cb9824e92')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

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

  apply_patch_with_msg \
    001-pivy-0.6.5-setup-py-msystem-env.patch \
    002-pivy-0.6.8-fake-header-cmath.patch \
    004-fix-compile-flags.patch \
    005-fix-install-destination.patch

  if [[ ${CARCH} != i686 ]]; then
    apply_patch_with_msg \
      003-find-qt6.patch
  fi
}

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

  if [[ "${MSYSTEM}" == "MINGW32" ]]; then
    # Works around gcc OOM on 32bit somehow
    CFLAGS+=" -Os -flarge-source-files"
  fi

  GENERATOR="Ninja" \
  ${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-${_realname}/LICENSE"
}

# vim: ts=2 sw=2 et:
