# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=shiboken6
pkgbase=mingw-w64-${_realname}
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname})
pkgver=6.4.0
pkgrel=1
pkgdesc="Generates bindings for C++ libraries using CPython source code (mingw-w64)"
url="https://doc.qt.io/qtforpython/"
license=("LGPL")
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-clang"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-libxslt")
optdepends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-python-wheel"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-numpy")
_pkgfn=pyside-setup-opensource-src-$pkgver
source=(https://download.qt.io/official_releases/QtForPython/pyside6/PySide6-$pkgver-src/$_pkgfn.tar.xz
        001-fix-building-on-mingw.patch)
sha256sums=('dcee166a112a7eb04bb58c1164997e1f720c1e3e2ee199ee8c9a70e82f51e321'
            '836e8f647246777625574b68b3d75baa7beb2dea135e7d8f65665337bdeb558f')

prepare() {
  cd "${srcdir}"/$_pkgfn
  patch -p1 -i "${srcdir}"/001-fix-building-on-mingw.patch
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -DBUILD_TESTS=OFF \
    ../$_pkgfn/sources/shiboken6

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}

  install -d "$pkgdir"${MINGW_PREFIX}/share/licenses/${_realname}
  install -Dm644 $_pkgfn/sources/shiboken6/COPYING -t "$pkgdir"${MINGW_PREFIX}/share/licenses/${_realname}

  # Fix *.pc files
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/shiboken6.pc

  # Fix *.cmake files
  for _f in "${pkgdir}"${MINGW_PREFIX}/lib/cmake/Shiboken6-${pkgver}/*.cmake; do
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  # Install egg-info
  cd "${srcdir}"/$_pkgfn
  ${MINGW_PREFIX}/bin/python setup.py bdist_wheel --qtpaths="$(command -v qtpaths-qt6.exe)" --skip-build --build-type=shiboken6
  _pythonpath=`${MINGW_PREFIX}/bin/python -c "from sysconfig import get_path; print(get_path('platlib'))"`
  cp -r shiboken6.egg-info "$pkgdir"/$(cygpath ${_pythonpath})
}
