# 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>

pkgbase=fwupd
pkgname=(
  fwupd
  fwupd-docs
)
pkgver=2.1.6
pkgrel=1
pkgdesc="Simple daemon to allow session software to update firmware"
arch=(x86_64 aarch64)
url='https://github.com/fwupd/fwupd'
license=(LGPL-2.1-or-later)
depends=(
  bash
  bluez
  curl
  fwupd-efi
  glib2
  glibc
  gnutls
  hicolor-icon-theme
  json-glib
  libarchive
  libcbor
  libdrm
  libgcc
  libjcat
  libmbim
  libmm-glib
  libmnl
  libqmi
  libusb
  libxmlb
  passim
  polkit
  protobuf-c
  python
  shared-mime-info
  sqlite
  systemd-libs
  tpm2-tss
  xz
  zlib
)
makedepends=(
  bash-completion
  gi-docgen
  git
  gnu-efi-libs
  gobject-introspection
  meson
  noto-fonts
  noto-fonts-cjk
  pandoc
  python
  python-cairo
  python-dbus
  python-dbusmock
  python-gobject
  python-pillow
  umockdev
  vala
  valgrind
)
source=(
  "git+https://github.com/fwupd/fwupd.git#tag=${pkgver}?signed"
  fwupd.sysusers
  fwupd-refresh.rules
)
sha512sums=('3f15c566a80c79e1fcbbdda79d6abd98c7cee02a823cfd44ab5b4dcee67a189ae4fccc986a857957506f8ced505b76d965b4191690c9359eac9d80e2e43db1d8'
            'f9a99c60786a2b98e0de439a9288af61b2c0716f8339a4c93e2df7108d2a7f9ef8128967dcaa1e12022ffa647945bf5eb3749e38cac83e00a28cbc5b015fbee9'
            '21e04acbae83d8ca548d6e7f9b2344e7b4dc8e1ab1c0a7799ded3160c99c54c2116b158a559d2b3243e960a6e4c72a3391599d68327209fa79a291622de3c1b8')
b2sums=('690489847163f58e6b5d02816fdd4e8f63b8aab5cd891d37c7ea40c1471444c2640d9e8fbfd4ecc85dafe220353fc74150ad4956a72524dad800a4152f1f7fe0'
        'c294c6dd324c0ad0d752affdc459d427d34f4bf865def099ac0e25db6b5dfbfcf645ca325e4e7732d1256e75c624ff27094d5c814726909c7e348128a9dd93f9'
        'b9e1c1c66452adb29378cbb42bd18152a2760750696644ee8dd4c09c6f23116d3e66fd405fc69c4694a5ea0f47b75d5959356a12da56ab1cc8345fe926098b3b')
validpgpkeys=(163EB50119225DB3DF8F49EA17ACBA8DFA970E17) # Richard Hughes <richard@hughsie.com>

build() {
  local meson_options=(
    -D docs=enabled
    -D efi_binary=false
    -D supported_build=enabled
    -D systemd_unit_user=fwupd
  )

  # HSI (Host Security ID) is x86-only in fwupd's meson.build;
  # arch-meson uses --auto-features=enabled which turns it on and then
  # the require(cpu in ['x86','x86_64']) check fails on other arches.
  [[ "$CARCH" != x86_64 && "$CARCH" != i686 ]] && meson_options+=(-D hsi=disabled)

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

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
    libblkid.so
    libcbor.so
    libcurl.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 -r "${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 -f "${pkgdir}"/usr/lib/modules-load.d/fwupd-msr.conf
  # Compile Python scripts
  python -m compileall -d /usr/share "${pkgdir}"/usr/share
  python -O -m compileall -d /usr/share "${pkgdir}"/usr/share

  install -Dm644 -t "${pkgdir}"/usr/share/polkit-1/rules.d fwupd-refresh.rules

  _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}"
}
