# Maintainer: Bruno Pagani <archange@archlinux.org>
# Maintainer: Filipe LaĆ­ns (FFY00) <lains@archlinux.org>
# Maintainer: Frederik Schwan <freswa at archlinux dot org>
# Contributor: Mirco Tischler <mt-ml at gmx dot de>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - remove depends on fwupd-efi, tmp2-tss
#  - remove makedepends on valgrind, gnu-efi-libs, libdrm (amdgpu dependency)
#  - meson with -Dplugin_uefi_capsule=disabled -Dplugin_uefi_pk=disabled -Dplugin_redfish=disabled -Dplugin_nvme=disabled (gnu-efi-libs dependency)
#  - meson with -Dplugin_msr=disabled
#  - meson with -Dplugin_intel_spi=false
#  - meson with -Dplugin_amdgpu=disabled
#  - remove makedepend on pandoc, meson with -Dman=false
#  - disable tests, failing in chroot

pkgbase=fwupd
pkgname=(
  fwupd
  fwupd-docs
)
pkgver=1.9.23
pkgrel=1
pkgdesc="Simple daemon to allow session software to update firmware"
arch=(x86_64)
url='https://github.com/fwupd/fwupd'
license=(LGPL-2.1-or-later)
depends=(
  bash
  bluez
  curl
  flashrom
  gcc-libs
  glib2
  glibc
  gnutls
  hicolor-icon-theme
  json-glib
  libarchive
  libcbor
  libgudev
  libgusb
  libjcat
  libmbim
  libmm-glib
  libqmi
  libxmlb
  passim
  polkit
  protobuf-c
  python
  shared-mime-info
  sqlite
  systemd-libs
  xz
  zlib
)
makedepends=(
  bash-completion
  gi-docgen
  gobject-introspection
  meson
  noto-fonts
  noto-fonts-cjk
  python-cairo
  python-dbus
  python-gobject
  python-pillow
  vala
)
checkdepends=(umockdev)
source=(
  "https://github.com/fwupd/fwupd/releases/download/${pkgver}/${pkgname}-${pkgver}.tar.xz"{,.asc}
  fwupd.sysusers
)
sha512sums=('379adcc79d40795f4895749bc944689ffee6fadc7c3dd1974df98c15120ca47d020ad4efabc68d0371cb36ebd3b549698c05a81bee0a673745a867bb32c4eeb5'
            'SKIP'
            '637203080b55eda74a659f58c853a9a723a2dad5da70915b2b0e036c6145a649468ebec700cc83975d9cb5378b9dced8b3a3b26bdbcc75ddc774837355e75deb')
b2sums=('d084244f99b5edccf4bf6a783846623876e9b4344c05bfe3cd412235bcde63d1f36db5528572e371fd0bde27f72c2bd61ba7b6472504ade4c9a068a00e34ca4e'
        'SKIP'
        'e65ca7da22a20a40882cfc1fe4479643f9a38c90a4f2c3e71e6e5e3de1d6db212a0f17d600097619fe3cdb0a9b860422f8b0b9a9d45441518e51a7eb12a918bb')
validpgpkeys=(163EB50119225DB3DF8F49EA17ACBA8DFA970E17) # Richard Hughes <richard@hughsie.com>

build() {
  local meson_options=(
    -D docs=enabled
    -D efi_binary=false
    -D launchd=disabled
    -D plugin_amdgpu=disabled
    -D plugin_intel_spi=false
    -D supported_build=enabled
    -D systemd_unit_user=fwupd
    -Dplugin_uefi_capsule=disabled -Dplugin_uefi_pk=disabled -Dplugin_redfish=disabled -Dplugin_nvme=disabled -Dplugin_msr=disabled -Dman=false -Dtests=false
  )

  arch-meson ${pkgname}-${pkgver} build "${meson_options[@]}"
}

check() {
  meson test -C build --print-errorlogs
}

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

package_fwupd() {
  depends+=(
    libarchive.so
    libcbor.so
    libcurl.so
    libgudev-1.0.so
    libjson-glib-1.0.so
    libmm-glib.so
    libpassim.so
    libprotobuf-c.so
    libqmi-glib.so
  )
  optdepends=(
    'python-dbus: Firmware packaging tools'
    'python-gobject: Firmware packaging tools'
    'udisks2: UEFI firmware upgrade support'
  )
  provides=(libfwupd.so)
  backup=(
    'etc/fwupd/fwupd.conf'
    'etc/fwupd/remotes.d/lvfs-testing.conf'
    'etc/fwupd/remotes.d/lvfs.conf'
    'etc/fwupd/remotes.d/vendor-directory.conf'
  )

  meson install -C build --destdir "${pkgdir}"
  # Add fwupd user https://bugs.archlinux.org/task/79653
  install -D -m644 fwupd.sysusers "${pkgdir}"/usr/lib/sysusers.d/fwupd.conf
  # Remove the tests
  rm -rf "${pkgdir}"/usr/{lib,share}/installed-tests/
  # Conflicts with the dbxtool package
  mv "${pkgdir}"/usr/bin/{,fwupd-}dbxtool
  #mv "${pkgdir}"/usr/share/man/man1/{,fwupd-}dbxtool.1
  # Remove msr module-load config as it is built-in
  #rm "${pkgdir}"/usr/lib/modules-load.d/fwupd-msr.conf
  #rmdir "${pkgdir}"/usr/lib/modules-load.d

  _pick docs "${pkgdir}"/usr/share/doc/{,fwupd/}{libfwupdplugin,libfwupd}
}

package_fwupd-docs() {
  pkgdesc="Simple daemon to allow session software to update firmware - documentation"
  depends=()
  mv docs/* "${pkgdir}"
}