# Maintainer: Ferdous Bhai <https://github.com/ferdousbhai>

pkgbase=ghost-dev
pkgname=(ghost-runtime-dev ghost-dev)
# Regenerated by pkgver() during every build; the committed value is not
# authoritative and is expected to lag HEAD.
pkgver=0.0.1.r100.g0c04ce7
pkgrel=1
pkgdesc="Owner-local AI persona with a character, tools, and an Omarchy-native desktop shell"
arch=('any')
url="https://github.com/ferdousbhai/ghost"
license=('Apache-2.0')
_runtime_depends=(
  'at-spi2-core>=2.58'
  'bun>=1.3.14'
  'grim>=1.5'
  'hyprland>=0.56'
  'libnotify>=0.8'
  'qrencode'
  'systemd'
  'wl-clipboard'
  'wtype>=0.4'
)
_ui_depends=('qt6-5compat>=6.8' 'quickshell>=0.3' 'xdg-utils>=1.2')
depends=("${_runtime_depends[@]}" "${_ui_depends[@]}")
makedepends=(
  'bun>=1.4.0'
  'desktop-file-utils'
  'git'
  'pnpm>=10.12.2'
)
checkdepends=(
  'jq'
  'nodejs'
  'python-yaml'
)
_runtime_optdepends=(
  'chromium: the browser the relay drives, and the browser-relay smoke test'
  'npm: npx-launched MCP servers'
)
options=('!debug' '!strip')
source=("ghost::git+${GHOST_SOURCE_REPO:-https://github.com/ferdousbhai/ghost.git}#${GHOST_SOURCE_REF:-branch=master}")
sha256sums=('SKIP')

pkgver() {
  cd -- "$srcdir/ghost"

  local version revision commit
  version="$(bun -e 'const p = await Bun.file("package.json").json(); process.stdout.write(p.version)')"
  revision="$(git rev-list --count HEAD)"
  commit="$(git rev-parse --short=7 HEAD)"
  printf '%s.r%s.g%s\n' "$version" "$revision" "$commit"
}

build() {
  cd -- "$srcdir/ghost"

  bash packaging/release/prepare-pnpm-engine.sh .
  pnpm install --frozen-lockfile
  pnpm build
  pnpm --filter @ghost/daemon build:runtime
}

check() {
  cd -- "$srcdir/ghost"

  bash packaging/release/test-release-source.sh
  bash packaging/arch/test-check-dependencies.sh
  pnpm test
  pnpm typecheck
  pnpm lint
}

package_ghost-runtime-dev() {
  pkgdesc="Ghost daemon, API, CLI, and desktop/browser automation without the HUD"
  depends=("${_runtime_depends[@]}")
  optdepends=("${_runtime_optdepends[@]}")
  provides=("ghost-runtime=$pkgver-$pkgrel")
  conflicts=(ghost-runtime)
  install=ghost-runtime-dev.install
  cd -- "$srcdir/ghost"
  bash packaging/release/install-payload.sh \
    . packages/daemon/dist/runtime "$pkgdir" "$pkgname" runtime
}

package_ghost-dev() {
  pkgdesc="Ghost HUD and desktop integration for Omarchy"
  depends=("ghost-runtime-dev=$pkgver-$pkgrel" "${_ui_depends[@]}")
  provides=("ghost=$pkgver-$pkgrel")
  conflicts=(ghost)
  install=ghost-dev.install
  cd -- "$srcdir/ghost"
  bash packaging/release/install-payload.sh \
    . packages/daemon/dist/runtime "$pkgdir" "$pkgname" ui
}
