# Maintainer: Saul Ibarra Corretge <saghul@gmail.com>
# Contributor: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=sip
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=6.15.1
pkgrel=1
pkgdesc="A tool that makes it easy to create Python bindings for C and C++ libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://riverbankcomputing.com/software/sip"
msys2_repository_url="https://github.com/Python-SIP/sip"
msys2_references=(
  'archlinux: sip'
  'purl: pkg:pypi/sip'
)
license=('spdx:BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-packaging"
         "${MINGW_PACKAGE_PREFIX}-python-setuptools")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools-scm")
options=(!strip)
source=(https://pypi.python.org/packages/source/s/sip/sip-${pkgver}.tar.gz
        001-mingw-python.patch
        002-wrappers.patch)
sha256sums=('dc2e58c1798a74e1b31c28e837339822fe8fa55288ae30e8986eb28100ebca5a'
            '15d64d0ff5253096a3fb5d3d4584eab8f93b9e9ffbcfee1c02dce00d695e6027'
            'ba695df87296ad5be7a64d57ce0a8f54db3867a9b69b303b76482f2ac48bb7d6')

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

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

  # Apply patches
  apply_patch_with_msg \
    001-mingw-python.patch \
    002-wrappers.patch

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

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  cd python-build-${MSYSTEM}

  ${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/${_realname}/LICENSE"
}
