# Maintainer: Antoine Martin <totaam@xpra.org>

_realname=xpra
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-common")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
          "${MINGW_PACKAGE_PREFIX}-${_realname}-common")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=4.4.5
pkgrel=4
pkgdesc='Remote access client/server software (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
msys2_references=(
  'archlinux: xpra'
)
url='https://xpra.org'
license=('spdx:GPL-2.0-or-later')
depends=(
    ${MINGW_PACKAGE_PREFIX}-ffmpeg
    ${MINGW_PACKAGE_PREFIX}-gtk3
    ${MINGW_PACKAGE_PREFIX}-libjpeg-turbo
    ${MINGW_PACKAGE_PREFIX}-libvpx
    ${MINGW_PACKAGE_PREFIX}-libwebp
    ${MINGW_PACKAGE_PREFIX}-libyuv
    ${MINGW_PACKAGE_PREFIX}-python
    ${MINGW_PACKAGE_PREFIX}-python-comtypes
    ${MINGW_PACKAGE_PREFIX}-python-gobject
    ${MINGW_PACKAGE_PREFIX}-lz4
    ${MINGW_PACKAGE_PREFIX}-python-lz4
    ${MINGW_PACKAGE_PREFIX}-python-pillow
    ${MINGW_PACKAGE_PREFIX}-python-pyopengl
    ${MINGW_PACKAGE_PREFIX}-python-rencode
    ${MINGW_PACKAGE_PREFIX}-python-setproctitle
    ${MINGW_PACKAGE_PREFIX}-libx264)
optdepends=(
    "${MINGW_PACKAGE_PREFIX}-libnotify: notification support"
    "${MINGW_PACKAGE_PREFIX}-gstreamer: audio/video streaming support"
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-bad: extra audio codecs"
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-ugly: extra audio codecs"
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-good: extra audio codecs"
    "${MINGW_PACKAGE_PREFIX}-gst-python: audio support"
    "${MINGW_PACKAGE_PREFIX}-python-paramiko: SSH client and server support"
    "${MINGW_PACKAGE_PREFIX}-python-dnspython: SSHFP support with paramiko"
    "${MINGW_PACKAGE_PREFIX}-python-netifaces: mDNS and network integration"
    "${MINGW_PACKAGE_PREFIX}-python-pyu2f: U2F authentication"
    "${MINGW_PACKAGE_PREFIX}-python-ldap: LDAP authentication via python-ldap"
    "${MINGW_PACKAGE_PREFIX}-python-ldap3: LDAP authentication via python-ldap3"
    "${MINGW_PACKAGE_PREFIX}-python-cryptography: AES packet encryption"
    "${MINGW_PACKAGE_PREFIX}-python-zeroconf: mDNS support"
    "${MINGW_PACKAGE_PREFIX}-python-nvidia-ml: nvidia GPU support"
    "${MINGW_PACKAGE_PREFIX}-python-pyopengl-accelerate: OpenGL-accelerate support")
makedepends=(
    ${MINGW_PACKAGE_PREFIX}-brotli
    ${MINGW_PACKAGE_PREFIX}-cython
    ${MINGW_PACKAGE_PREFIX}-cc
    ${MINGW_PACKAGE_PREFIX}-pkgconf
    ${MINGW_PACKAGE_PREFIX}-python-cairo
    ${MINGW_PACKAGE_PREFIX}-python-setuptools
    ${MINGW_PACKAGE_PREFIX}-pygobject-devel)
source=("https://xpra.org/src/${_realname}-${pkgver}.tar.xz"
        "${_realname}-${pkgver}.tar.xz.asc::https://xpra.org/src/${_realname}-${pkgver}.tar.xz.gpg")
sha512sums=('e2bd091367f27fba56c452ba7d0208f5bfbf8bfa65b87c4318be58e8984d05260a7f1c2b47f5cd8d16cc0da992fb1d357692220284fa5da2d3b1c822e36090e9'
            'SKIP')
validpgpkeys=('C11C0A4DF702EDF6C04F458C18ADB31CF18AD6BB') # Antoine Martin <antoine@nagafix.co.uk>

case ${MSYSTEM} in
  CLANG*)
    CFLAGS="$CFLAGS -Wno-error=unreachable-code -Wno-error=sometimes-uninitialized";;
esac

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

build() {
  msg "Python build for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"

  CFLAGS+=" -Wno-error=address"  # triggers gcc 12 error in some auto generated cython code
  CFLAGS+=" -Wno-error=deprecated-declarations"
  ${MINGW_PREFIX}/bin/python setup.py build \
    --without-cuda_kernels --with-data --without-docs --with-modules
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --without-cuda_kernels --with-data  --without-docs \
    --root="${pkgdir}" --share-xpra="./share/xpra" --optimize=1 --skip-build

  install -Dm644 COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"
  install -Dm644 README.md "${pkgdir}${MINGW_PREFIX}/share/${_realname}/README.md"
  install -Dm644 fs/share/xpra/bell.wav "${pkgdir}${MINGW_PREFIX}/share/${_realname}/bell.wav"

  #python version agnostic launch scripts in /bin:
  for script in xpra xpra_launcher; do
    install -Dm755 fs/bin/${script} "${pkgdir}${MINGW_PREFIX}/bin/${script}"
    #patch path to the python interpreter:
    sed -i -e "s+/usr/bin/python+${MINGW_PREFIX}/bin/python+g" "${pkgdir}${MINGW_PREFIX}/bin/${script}"
  done
}
