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

pkgbase=wusel
pkgname=('wusel' 'wusel-nautilus')
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')
# A split package: `wusel` needs fuse3 and nothing else; the Nautilus extension
# is `wusel-nautilus`, so its libraries reach only machines that want it.
# Both are built unconditionally (nautilus and gtk4 are makedepends, for their
# headers).
makedepends=('rust' 'git' 'pkgconf' 'nautilus' 'gtk4')
source=("$pkgbase-$pkgver.tar.gz::https://github.com/itbh-at/wusel/archive/refs/tags/v$pkgver.tar.gz")
sha256sums=('3dfb8473be5d08b24cad70e9c435ca9c66f7bad38b88f2afd37d1492c109eac8')

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

package_wusel() {
  depends=('fuse3')
  optdepends=('wusel-nautilus: emblems and the Wusel context menu in GNOME Files'
              'gnome-shell: search provider over Nextcloud Unified Search')
  # Removes runtime-written sidebar registrations on uninstall; see the file.
  install=wusel.install

  cd "$pkgbase-$pkgver"

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

  install -Dm644 integration/icons/at.itbh.Wusel.svg \
    "$pkgdir/usr/share/icons/hicolor/scalable/apps/at.itbh.Wusel.svg"
  install -Dm644 integration/icons/at.itbh.Wusel-symbolic.svg \
    "$pkgdir/usr/share/icons/hicolor/symbolic/apps/at.itbh.Wusel-symbolic.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/wusel/LICENSE"
  install -Dm644 README.md "$pkgdir/usr/share/doc/wusel/README.md"
}

package_wusel-nautilus() {
  pkgdesc="GNOME Files (Nautilus) integration for Wusel — emblems and context menu"
  depends=('wusel' 'libnautilus-extension' 'glib2')

  cd "$pkgbase-$pkgver"
  # 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 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
