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

# Holo: Variant of fwupd disabling some less common plugins/features to save on disk space/dependencies
pkgname=fwupd-minimal
pkgver=2.1.7
pkgrel=2
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
  fwupd-efi
  glib2
  glibc
  gnutls
  hicolor-icon-theme
  libdrm
  libgcc
  libusb
  libmbim
  libmm-glib
  libmnl
  libxmlb
  polkit
  'python>=3.14'
  'python<3.15'
  shared-mime-info
  sqlite
  systemd-libs
  tpm2-tss
  udisks2
  xz
  zlib
)
makedepends=(
  bash-completion
  git
  gnu-efi-libs
  gobject-introspection
  libdrm
  meson
  noto-fonts
  noto-fonts-cjk
  python-cairo
  python-dbus
  python-dbusmock
  python-gobject
  python-jinja
  umockdev
  vala
  valgrind
)
source=(
  "git+https://github.com/fwupd/fwupd.git#tag=${pkgver}?signed"
  # Holo: Force use of /esp partition for capsules, config, not upstreamable:
  #       https://gitlab.steamos.cloud/holo-team/tasks/-/work_items/2345
  0001-fwupd.conf-Set-ESP-location-to-protect-against-efi-u.patch
  fwupd.sysusers
  fwupd-refresh.rules
)
sha512sums=('560fde8c73f68b2611786255c5bfffc3f30cbcf02b45ab54d9ac79dd93990c9be1421b952c1c7a0437baca1f208ce7f886dd0d9806fbdb04913c0141d6103fff'
            '774115709ad4ef864f1d4c68310be7817c7808b9b92c8c8cdd5ea9beb7071bfd1520ea51a64bda675a93a2d1e2840d6689b0459a0f27437cff9fa6abca903076'
            'f9a99c60786a2b98e0de439a9288af61b2c0716f8339a4c93e2df7108d2a7f9ef8128967dcaa1e12022ffa647945bf5eb3749e38cac83e00a28cbc5b015fbee9'
            '21e04acbae83d8ca548d6e7f9b2344e7b4dc8e1ab1c0a7799ded3160c99c54c2116b158a559d2b3243e960a6e4c72a3391599d68327209fa79a291622de3c1b8')
b2sums=('098893b6ec9e0592b0647740e928fbad2665976f95d24dd232a6ea0ab141fa9ea01f5203430569dc20a2966f5f06e2aa574398805a8ad48de13cabef0fb25d92'
        'fb5b4f56fb95430e90251016231704b23cd3ac5b31f95fe213be7844ee5c84db0eba9b7ffa56c19d3026bd2a20587431c7e881d7c1179a7a0162cdaf769ce6b6'
        'c294c6dd324c0ad0d752affdc459d427d34f4bf865def099ac0e25db6b5dfbfcf645ca325e4e7732d1256e75c624ff27094d5c814726909c7e348128a9dd93f9'
        'b9e1c1c66452adb29378cbb42bd18152a2760750696644ee8dd4c09c6f23116d3e66fd405fc69c4694a5ea0f47b75d5959356a12da56ab1cc8345fe926098b3b')
validpgpkeys=(163EB50119225DB3DF8F49EA17ACBA8DFA970E17) # Richard Hughes <richard@hughsie.com>

prepare() {
  cd fwupd

  local patch
  for patch in "${source[@]}"
  do
    case $patch in
      *.patch)
        echo "# Applying ${patch}"
        patch -p1 -i "${srcdir}"/"${patch}"
        ;;
    esac
  done
}

build() {
  # Holo: Disable some less-common plugins/features for size/dependencies
  local meson_options=(
    -D docs=disabled
    -D efi_binary=false
    -D supported_build=enabled
    -D systemd_unit_user=fwupd
    -D hsi=disabled
    -D introspection=disabled
    -D umockdev_tests=disabled
    -D passim=disabled
    -D plugin_modem_manager=disabled
    -D fish_completion=false
  )

  # 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 fwupd 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() {
  depends+=(
    curl libcurl.so
    libmm-glib libmm-glib.so
    libqmi libqmi-glib.so
  )
  optdepends=(
    'python-dbus: Firmware packaging tools'
    'python-gobject: Firmware packaging tools'
  )
  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
}

