# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libvirt
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
_pkgdev=11.8.0
pkgver=${_pkgdev//-/}
pkgrel=1
pkgdesc="Windows libvirt virtualization library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://libvirt.org/'
msys2_repository_url='https://gitlab.com/libvirt/libvirt/'
msys2_documentation_url='https://libvirt.org/docs.html'
msys2_references=(
  'archlinux: libvirt'
  "cpe: cpe:/a:libvirt:libvirt"
  "cpe: cpe:/a:redhat:libvirt"
  'gentoo: app-emulation/libvirt'
)
license=('spdx:LGPL-2.1-or-later OR GPL-2.0-or-later')
depends=(${MINGW_PACKAGE_PREFIX}-curl
         ${MINGW_PACKAGE_PREFIX}-glib2
         ${MINGW_PACKAGE_PREFIX}-gnutls
         ${MINGW_PACKAGE_PREFIX}-gettext
         ${MINGW_PACKAGE_PREFIX}-libgcrypt
         ${MINGW_PACKAGE_PREFIX}-libgpg-error
         ${MINGW_PACKAGE_PREFIX}-libssh
         ${MINGW_PACKAGE_PREFIX}-libssh2
         ${MINGW_PACKAGE_PREFIX}-libxml2
         ${MINGW_PACKAGE_PREFIX}-portablexdr
         ${MINGW_PACKAGE_PREFIX}-readline
         ${MINGW_PACKAGE_PREFIX}-yajl)
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             ${MINGW_PACKAGE_PREFIX}-meson
             ${MINGW_PACKAGE_PREFIX}-pkgconf
             ${MINGW_PACKAGE_PREFIX}-python
             ${MINGW_PACKAGE_PREFIX}-python-docutils
             ${MINGW_PACKAGE_PREFIX}-python-flake8
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             rpcsvc-proto
             perl)
source=("https://libvirt.org/sources/${_realname}-${_pkgdev}.tar.xz"{,.asc}
        0001-meson-drop-path-for-python.patch)
sha256sums=('f7882fe65302bbcf804b573e0128c4fc6bfc52c9c3f44852a04de2391d858e34'
            'SKIP'
            'ec09fa0104984b669c749fc3da41c1d22d329e28b5176d03e32d7d97cabf7eee')
validpgpkeys=('453B65310595562855471199CA68BE8010084C9C') # Jiří Denemark <jdenemar@redhat.com>
noextract=(${_realname}-${_pkgdev}.tar.xz)

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  tar -C "${srcdir}" --exclude=tests -xJf "${srcdir}"/${_realname}-${_pkgdev}.tar.xz || true

  cd "${_realname}-${_pkgdev%-*}"
  apply_patch_with_msg \
    0001-meson-drop-path-for-python.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 \
    -Drpath=disabled \
    -Ddriver_qemu=disabled \
    -Ddriver_openvz=disabled \
    -Ddriver_lxc=disabled \
    -Ddriver_vbox=disabled \
    -Ddriver_libvirtd=disabled \
    -Ddriver_esx=disabled \
    -Ddriver_hyperv=disabled \
    -Ddriver_vmware=disabled \
    -Dsasl=disabled \
    -Dpolkit=disabled \
    -Dnetcf=disabled \
    -Daudit=disabled \
    -Ddtrace=disabled \
    -Dtests=disabled \
    -Dexpensive_tests=disabled \
    ../${_realname}-${_pkgdev%-*}

  ${MINGW_PREFIX}/bin/meson compile
}

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

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

  install -Dm644 "${srcdir}"/${_realname}-${_pkgdev%-*}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}"/${_realname}-${_pkgdev%-*}/COPYING.LESSER "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER"
}
