# Maintainer: bvandeusen
#
# ThoughtSync desktop (Tauri v2) — native Arch package.
#
# Built from THIS repo checkout, from SOURCE, linked against your SYSTEM libraries.
# That is the whole point: it uses the host's graphics stack (webkit2gtk-4.1 + your
# GPU driver), which avoids the bundled-AppImage "EGL_BAD_PARAMETER" black window.
#
# Usage (from a clone of the thoughtsync repo):
#   cd desktop/packaging/arch
#   makepkg -si
#
pkgname=thoughtsync-desktop
pkgver=0.1.0
pkgrel=1
pkgdesc="ThoughtSync desktop — local-first Keep-style thought capture"
arch=('x86_64')
url="https://git.fabledsword.com/bvandeusen/thoughtsync"
license=('MIT')
depends=(
  'webkit2gtk-4.1'
  'gtk3'
  'cairo'
  'gdk-pixbuf2'
  'glib2'
  'libsoup3'
  'pango'
  'hicolor-icon-theme'
  'desktop-file-utils'
  'librsvg'
)
makedepends=('rust' 'nodejs' 'npm' 'git' 'pkgconf')
# Cargo's release profile already does its own LTO; don't let makepkg inject C-level
# LTO flags into the crates that build native code.
options=('!lto')

# Locate the repo root this PKGBUILD lives inside (desktop/packaging/arch/).
_repo() { git -C "$startdir" rev-parse --show-toplevel; }

pkgver() {
  cd "$startdir"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

build() {
  local repo; repo="$(_repo)"

  # 1) Build the shared Vue frontend — it's embedded into the binary at compile time
  #    (tauri generate_context!), so it must exist before the cargo build.
  cd "$repo/frontend"
  npm ci
  npm run build

  # 2) Compile the native binary against SYSTEM libraries (no bundling, no tauri-cli).
  cd "$repo/desktop/src-tauri"
  cargo build --release
}

package() {
  local repo; repo="$(_repo)"

  install -Dm755 "$repo/desktop/src-tauri/target/release/thoughtsync-desktop" \
    "$pkgdir/usr/bin/thoughtsync"
  install -Dm644 "$repo/desktop/src-tauri/app-icon.png" \
    "$pkgdir/usr/share/pixmaps/thoughtsync.png"
  install -Dm644 "$startdir/thoughtsync.desktop" \
    "$pkgdir/usr/share/applications/thoughtsync.desktop"
}
