pkgname=buzz
pkgver=0.5.28
pkgrel=1
pkgdesc='Workspace where humans and AI agents build together, on a Nostr relay you own'
arch=('x86_64' 'aarch64')
url='https://github.com/block/buzz'
license=('Apache-2.0')

depends=(
  'alsa-lib'
  'cairo'
  'gcc-libs'
  'gdk-pixbuf2'
  'glib2'
  'glibc'
  'gst-plugins-good'
  'gtk3'
  'hicolor-icon-theme'
  'libayatana-appindicator'
  'libsoup3'
  'opus'
  'webkit2gtk-4.1'
)
makedepends=('cargo' 'cmake' 'nodejs' 'pnpm' 'pkgconf')
conflicts=('buzz-appimage' 'buzz-bin' 'buzz-desktop-git')
# Arch's GCC LTO objects cannot be consumed when crates compile bundled C
# libraries (secp256k1-sys among them).
options=('!lto')

_srcdir="$pkgname-desktop-v$pkgver"
source=("$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/desktop-v$pkgver.tar.gz"
        'buzz.desktop')
sha256sums=('4ba8c250d6ebb075ba60e6c4d217b77ea605b183161715b77629bd8660b81a43'
            '3c152420e67c64f5046cc0e10a9d948e1c7d3efa512582ad279f073b0900526a')

# The sidecars the desktop app spawns from its own directory, from upstream's
# scripts/bundle-sidecars.sh.
_sidecars=(buzz-acp buzz-agent buzz-backend-kubernetes buzz-dev-mcp git-credential-nostr buzz)

prepare() {
  cd "$_srcdir"

  export npm_config_store_dir="$srcdir/pnpm-store"
  pnpm install --frozen-lockfile

  # The sidecars build in the root workspace; the app has its own lockfile.
  cargo fetch --locked --target "$CARCH-unknown-linux-gnu"
  cargo fetch --locked --target "$CARCH-unknown-linux-gnu" \
    --manifest-path desktop/src-tauri/Cargo.toml
}

build() {
  cd "$_srcdir"

  export npm_config_store_dir="$srcdir/pnpm-store"
  # audiopus_sys's vendored opus fallback declares an old CMake minimum.
  export CMAKE_POLICY_VERSION_MINIMUM=3.5
  # sha2-asm assembles ARMv8 SHA instructions, which Arch Linux ARM's
  # -march=armv8-a leaves out. sha2 still checks for them at runtime.
  [[ $CARCH == aarch64 ]] && CFLAGS=${CFLAGS/-march=armv8-a/-march=armv8-a+crypto}

  cargo build --frozen --release -p buzz-acp -p buzz-agent -p buzz-backend-kubernetes \
    -p buzz-dev-mcp -p git-credential-nostr -p buzz-cli
  ./scripts/bundle-sidecars.sh

  # Upstream's Linux release build, without its bundles. Leaving
  # BUZZ_UPDATER_PUBLIC_KEY and BUZZ_UPDATER_ENDPOINT unset compiles the
  # self-updater out: pacman owns updates.
  cd desktop
  pnpm tauri build --ci --no-bundle --features mesh-llm -- --frozen
}

package() {
  cd "$_srcdir"

  install -Dm755 desktop/src-tauri/target/release/buzz-desktop "$pkgdir/usr/bin/buzz-desktop"
  local bin
  for bin in "${_sidecars[@]}"; do
    install -Dm755 "target/release/$bin" "$pkgdir/usr/bin/$bin"
  done

  install -Dm644 "$srcdir/buzz.desktop" "$pkgdir/usr/share/applications/buzz-desktop.desktop"
  local icons=desktop/src-tauri/icons size
  for size in 32 64 128; do
    install -Dm644 "$icons/${size}x$size.png" \
      "$pkgdir/usr/share/icons/hicolor/${size}x$size/apps/buzz-desktop.png"
  done
  install -Dm644 "$icons/128x128@2x.png" "$pkgdir/usr/share/icons/hicolor/256x256/apps/buzz-desktop.png"
  install -Dm644 "$icons/icon.png" "$pkgdir/usr/share/icons/hicolor/512x512/apps/buzz-desktop.png"

  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
