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

_realname=spice-gtk
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.42
pkgrel=4
pkgdesc="GTK3 widget for SPICE clients (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.spice-space.org/"
msys2_references=(
  "cpe: cpe:/a:spice-gtk_project:spice-gtk"
)
license=("GPL2")
depends=("${MINGW_PACKAGE_PREFIX}-cyrus-sasl"
         "${MINGW_PACKAGE_PREFIX}-dbus-glib"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gstreamer"
         "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-json-glib"
         "${MINGW_PACKAGE_PREFIX}-libcacard"
         "${MINGW_PACKAGE_PREFIX}-libepoxy"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libsoup3"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-opus"
         "${MINGW_PACKAGE_PREFIX}-phodav"
         "${MINGW_PACKAGE_PREFIX}-pixman"
         "${MINGW_PACKAGE_PREFIX}-spice-protocol"
         "${MINGW_PACKAGE_PREFIX}-usbredir"
         "${MINGW_PACKAGE_PREFIX}-vala"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-python-pyparsing"
             "${MINGW_PACKAGE_PREFIX}-python-six")
source=(https://www.spice-space.org/download/gtk/${_realname}-${pkgver}.tar.xz{,.sig}
        002-export-symbols.patch
        003-add-stdcall-attribute.patch)
sha256sums=('9380117f1811ad1faa1812cb6602479b6290d4a0d8cc442d44427f7f6c0e7a58'
            'SKIP'
            '3295af8a5656adad24c2b3f8858d458597f14acb00bb5782412c2a115cacf507'
            '431e6de18661b02a0b6561ed831d5a65d46bad155cd969f794fae27259a76667')
validpgpkeys=('206D3B352F566F3B0E6572E997D9123DE37A484F') # Victor Toso de Carvalho <me@victortoso.com>

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  cp -p src/spice-glib-sym-file src/spice-glib-sym-file.def
  cp -p src/spice-gtk-sym-file src/spice-gtk-sym-file.def
  patch -p1 -i "${srcdir}"/002-export-symbols.patch

  # https://gitlab.freedesktop.org/spice/spice-gtk/-/merge_requests/118
  patch -p1 -i "${srcdir}"/003-add-stdcall-attribute.patch
}

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

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    --prefix="${MINGW_PREFIX}" \
    --buildtype=plain \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    -Dlibcap-ng=disabled \
    -Dpolkit=disabled \
    -Dspice-common:manual=false \
    "../${_realname}-${pkgver}"

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

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/meson.exe test || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install

  # Remove full path reference
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
