# Maintainer: Christoph D. Hermann <christoph.hermann@itbh.at>

pkgname=wusel
pkgver=0.2.2
pkgrel=1
pkgdesc="Virtual Nextcloud filesystem — Nextcloud, woven into your desktop"
arch=('x86_64' 'aarch64')
url="https://github.com/itbh-at/wusel"
license=('Apache-2.0')
# Only the mount itself is a hard dependency. The Nautilus extension and
# search provider are optional integrations, built unconditionally (nautilus
# is a makedepend, for its headers) but not required at runtime — pulling in
# the GTK/GNOME stack on a KDE or headless box for a mount that needs fuse3
# and nothing else is exactly the mismatch `optdepends` exists to avoid.
depends=('fuse3')
makedepends=('rust' 'git' 'pkgconf' 'nautilus')
optdepends=('nautilus: file-manager integration — sidebar entry, per-file emblems, pin/unpin menu'
            'gnome-shell: search provider over Nextcloud Unified Search')
source=("$pkgname-$pkgver.tar.gz::https://github.com/itbh-at/wusel/archive/refs/tags/v$pkgver.tar.gz")
sha256sums=('3dfb8473be5d08b24cad70e9c435ca9c66f7bad38b88f2afd37d1492c109eac8')

build() {
  cd "$pkgname-$pkgver"
  export CARGO_TARGET_DIR=target
  cargo build --release --locked --features fuse -p wusel
  make -C integration/nautilus
}

package() {
  cd "$pkgname-$pkgver"

  install -Dm755 target/release/wusel "$pkgdir/usr/bin/wusel"
  install -Dm644 packaging/rpm/wusel@.service "$pkgdir/usr/lib/systemd/user/wusel@.service"

  # Installs the extension .so + emblem icons at the paths pkg-config reports
  # for *this* system — the Makefile computes them, not a value hardcoded here.
  make -C integration/nautilus install DESTDIR="$pkgdir"

  install -Dm644 integration/icons/at.itbh.Wusel.svg \
    "$pkgdir/usr/share/icons/hicolor/scalable/apps/at.itbh.Wusel.svg"

  install -Dm644 integration/gnome-search/at.itbh.Wusel.desktop \
    "$pkgdir/usr/share/applications/at.itbh.Wusel.desktop"
  install -Dm644 integration/gnome-search/wusel-search-provider.desktop \
    "$pkgdir/usr/share/gnome-shell/search-providers/wusel-search-provider.desktop"
  install -Dm644 /dev/stdin \
    "$pkgdir/usr/share/dbus-1/services/at.itbh.Wusel.SearchProvider.service" <<< \
    "$(sed 's#Exec=.*/wusel search-provider#Exec=/usr/bin/wusel search-provider#' \
        integration/gnome-search/at.itbh.Wusel.SearchProvider.service)"

  # Cloud-provider (Nautilus sidebar) registration for the default account —
  # generated by the freshly built binary so it never drifts from the code
  # (same trick the RPM spec uses; see its %install for the rationale).
  install -d "$pkgdir/usr/share/applications"
  "$pkgdir/usr/bin/wusel" desktop install-provider --account default --dir "$pkgdir/usr/share/applications" >/dev/null
  rm -f "$pkgdir/usr/share/applications/mimeinfo.cache"

  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  install -Dm644 README.md "$pkgdir/usr/share/doc/$pkgname/README.md"
}
