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

_realname=pyside6
pkgbase=mingw-w64-${_realname}
pkgname=(${MINGW_PACKAGE_PREFIX}-shiboken6
         ${MINGW_PACKAGE_PREFIX}-${_realname}
         ${MINGW_PACKAGE_PREFIX}-${_realname}-tools)
pkgver=6.10.0
pkgrel=1
pkgdesc="Enables the use of Qt6 APIs in Python applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://doc.qt.io/qtforpython/"
msys2_repository_url="https://code.qt.io/cgit/pyside/pyside-setup.git"
msys2_references=(
  'archlinux: pyside6'
  'purl: pkg:pypi/pyside6'
)
license=("spdx:LGPL-2.1-or-later")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-packaging"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-qt6-3d"
             "${MINGW_PACKAGE_PREFIX}-qt6-activeqt"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-charts"
             "${MINGW_PACKAGE_PREFIX}-qt6-connectivity"
             "${MINGW_PACKAGE_PREFIX}-qt6-datavis3d"
             $([[ ${MSYSTEM} == MINGW64 ]] || echo "${MINGW_PACKAGE_PREFIX}-qt6-graphs")
             "${MINGW_PACKAGE_PREFIX}-qt6-httpserver"
             "${MINGW_PACKAGE_PREFIX}-qt6-location"
             "${MINGW_PACKAGE_PREFIX}-qt6-multimedia"
             "${MINGW_PACKAGE_PREFIX}-qt6-networkauth"
             $([[ ${MSYSTEM} == MINGW64 || ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-qt6-pdf")
             "${MINGW_PACKAGE_PREFIX}-qt6-quick3d"
             "${MINGW_PACKAGE_PREFIX}-qt6-positioning"
             #"${MINGW_PACKAGE_PREFIX}-qt6-remoteobjects"
             "${MINGW_PACKAGE_PREFIX}-qt6-scxml"
             "${MINGW_PACKAGE_PREFIX}-qt6-sensors"
             "${MINGW_PACKAGE_PREFIX}-qt6-serialbus"
             "${MINGW_PACKAGE_PREFIX}-qt6-serialport"
             "${MINGW_PACKAGE_PREFIX}-qt6-speech"
             "${MINGW_PACKAGE_PREFIX}-qt6-svg"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-qt6-webchannel"
             "${MINGW_PACKAGE_PREFIX}-qt6-websockets"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers")
_pkgfn=pyside-setup-everywhere-src-${pkgver}
source=(https://download.qt.io/official_releases/QtForPython/pyside6/PySide6-${pkgver}-src/${_pkgfn}.tar.xz
        001-fix-building-on-mingw.patch
        002-fix-install-header-files.patch
        https://invent.kde.org/qt/pyside/pyside-setup/-/commit/c9d602ab.patch)
sha256sums=('f6e18dc880f59fb6c5c37f9f408971d65642dfc7510a15d794b4a3a8e15fcecc'
            '5f71b618e0ad687e9282fa4b8c3815beaf69b78666516bfce57cb039822f4ac6'
            '3bc87409ea3dc41847f1d5d7612fd97931b67f1b40510b465543a8ef5c9764ff'
            'ac42367f1ed50fa0fb7aa62cf01dc859dcf662643f8ade53b4755c69f66360ee')

prepare() {
  cd "${srcdir}"/${_pkgfn}
  patch -p1 -i "${srcdir}"/001-fix-building-on-mingw.patch
  # https://gitlab.archlinux.org/archlinux/packaging/packages/pyside6/-/blob/34a42032/fix-header-install-dir.patch
  patch -p1 -i "${srcdir}"/002-fix-install-header-files.patch
  patch -p1 -i "${srcdir}"/c9d602ab.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd 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

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  # Skip RemoteObjects Module 
  # https://github.com/msys2/MINGW-packages/pull/23888#issue-2974803968
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -DBUILD_TESTS=OFF \
    -DFORCE_LIMITED_API=OFF \
    -DNO_QT_TOOLS="yes" \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DNUMPY_INCLUDE_DIR=${MINGW_PREFIX}/include/python${_pyver}/numpy/_core/include \
    -DSKIP_MODULES="DBus;RemoteObjects" \
    ../${_pkgfn}

  PYTHONPATH="${srcdir}"/build-${MSYSTEM}/sources \
  ${MINGW_PREFIX}/bin/cmake --build .
}

package_shiboken6() {
  pkgdesc="Generates bindings for C++ libraries using CPython source code (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
           "${MINGW_PACKAGE_PREFIX}-clang-libs"
           "${MINGW_PACKAGE_PREFIX}-qt6-base"
           "${MINGW_PACKAGE_PREFIX}-libxml2"
           "${MINGW_PACKAGE_PREFIX}-libxslt")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/sources/shiboken6

  install -Dm644 ${_pkgfn}/sources/shiboken6/COPYING \
    "$pkgdir"${MINGW_PREFIX}/share/licenses/shiboken6/LICENSE

  # 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/*.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 egg_info --build-type=shiboken6
  ${MINGW_PREFIX}/bin/python setup.py egg_info --build-type=shiboken6-generator
  _pythonpath=`${MINGW_PREFIX}/bin/python -c "from sysconfig import get_path; print(get_path('platlib'))"`
  cp -r shiboken6*.egg-info "$pkgdir"/$(cygpath ${_pythonpath})
}

package_pyside6() {
  pkgdesc="Enables the use of Qt6 APIs in Python applications (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
           "${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-shiboken6"
           "${MINGW_PACKAGE_PREFIX}-qt6-base"
           "${MINGW_PACKAGE_PREFIX}-qt6-declarative")
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-3d"
              "${MINGW_PACKAGE_PREFIX}-qt6-activeqt"
              "${MINGW_PACKAGE_PREFIX}-qt6-charts"
              "${MINGW_PACKAGE_PREFIX}-qt6-connectivity"
              "${MINGW_PACKAGE_PREFIX}-qt6-datavis3d"
              "${MINGW_PACKAGE_PREFIX}-qt6-graphs"
              "${MINGW_PACKAGE_PREFIX}-qt6-httpserver"
              "${MINGW_PACKAGE_PREFIX}-qt6-location"
              "${MINGW_PACKAGE_PREFIX}-qt6-multimedia"
              "${MINGW_PACKAGE_PREFIX}-qt6-networkauth"
              "${MINGW_PACKAGE_PREFIX}-qt6-pdf"
              "${MINGW_PACKAGE_PREFIX}-qt6-quick3d"
              "${MINGW_PACKAGE_PREFIX}-qt6-positioning"
              #"${MINGW_PACKAGE_PREFIX}-qt6-remoteobjects"
              "${MINGW_PACKAGE_PREFIX}-qt6-scxml"
              "${MINGW_PACKAGE_PREFIX}-qt6-sensors"
              "${MINGW_PACKAGE_PREFIX}-qt6-serialbus"
              "${MINGW_PACKAGE_PREFIX}-qt6-serialport"
              "${MINGW_PACKAGE_PREFIX}-qt6-speech"
              "${MINGW_PACKAGE_PREFIX}-qt6-svg"
              "${MINGW_PACKAGE_PREFIX}-qt6-tools"
              "${MINGW_PACKAGE_PREFIX}-qt6-webchannel"
              "${MINGW_PACKAGE_PREFIX}-qt6-websockets")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/sources/pyside6

  install -Dm644 ${_pkgfn}/sources/pyside6/COPYING \
    "$pkgdir"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  _pythonpath=`${MINGW_PREFIX}/bin/python -c "from sysconfig import get_path; print(get_path('platlib'))"`
  # Install egg-info
  cd "${srcdir}"/${_pkgfn}
  ${MINGW_PREFIX}/bin/python setup.py egg_info --build-type=pyside6
  cp -r PySide6.egg-info "$pkgdir"/$(cygpath ${_pythonpath})
  # Install pyi files
  install -Dm644 "${srcdir}"/build-${MSYSTEM}/sources/pyside6/PySide6/*.pyi -t "$pkgdir"/$(cygpath ${_pythonpath})/PySide6
}

package_pyside6-tools() {
  pkgdesc="Tools for PySide6 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-pyside6")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}/sources/pyside-tools
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
