# Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>

_realname=virt-viewer
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=11.0
pkgrel=7
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
pkgdesc="Displaying the graphical console of a virtual machine (mingw-w64)"
url="https://gitlab.com/virt-viewer/virt-viewer"
msys2_references=(
  'archlinux: virt-viewer'
)
license=("spdx:GPL-2.0-or-later")
depends=("${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-gtk-vnc"
         "${MINGW_PACKAGE_PREFIX}-libgovirt"
         "${MINGW_PACKAGE_PREFIX}-librest"
         "${MINGW_PACKAGE_PREFIX}-libvirt"
         "${MINGW_PACKAGE_PREFIX}-libvirt-glib"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-opus"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-spice-gtk")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-icoutils"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-shared-mime-info"
             "${MINGW_PACKAGE_PREFIX}-spice-protocol")
source=(https://releases.pagure.org/virt-viewer/virt-viewer-${pkgver}.tar.xz{,.asc}
        001-rc-generation-fixes.patch
        002-check-linker-pie-support.patch
        003-fix-arm64-link.patch
        004-fix-build-with-librest-0.9.patch
        005-fix-build-with-libxml-2.12.patch)
sha256sums=('a43fa2325c4c1c77a5c8c98065ac30ef0511a21ac98e590f22340869bad9abd0'
            'SKIP'
            'ef8d364e7f05c799ffd98c798f45dd125ab90d1a5fc116052e9e8b769182a03e'
            '6a52d3807518536ec605dfd331d8ab0c993ca49d147d84b3e71f0ca0ae9f41dd'
            '0bbacaaa3a002fe609aae9751f0930754b9a2b045c48136a041a707525f5e344'
            '6a3ea23ec2648eaadca386f45d5041346ad1da13d31bc66aa8445059c47c2982'
            '5e9d2d60f96a42873df99910e0545b71935624b7561c839f9765018b4c536c8c')
validpgpkeys=('DAF3A6FDB26B62912D0E8E3FBE86EBB415104FDF') # Daniel P. Berrange

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-rc-generation-fixes.patch \
    002-check-linker-pie-support.patch \
    003-fix-arm64-link.patch \
    004-fix-build-with-librest-0.9.patch \
    005-fix-build-with-libxml-2.12.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--buildtype=release")
  else
    _extra_config+=("--buildtype=debug")
  fi

  CFLAGS+=" -Wno-int-conversion -Wno-implicit-function-declaration" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    --prefix="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    --wrap-mode=nodownload \
    --default-library=both \
    --auto-features=disabled \
    -Dovirt=enabled \
    -Dlibvirt=enabled \
    -Dvnc=enabled \
    -Dspice=enabled \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile
}

package() {
  cd "$srcdir/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
