# Maintainer: Jorge Manrubia <jorge@37signals.com>
pkgname=omacharts
pkgver=0.1.2
pkgrel=1
pkgdesc='Fast, beautiful charting software for Omarchy'
arch=('x86_64' 'aarch64')
url='https://github.com/jorgemanrubia/omacharts'
license=('MIT')
depends=('gtk4' 'libadwaita')
makedepends=('cargo')
# SQLite and ring are compiled from C by build scripts. makepkg's default
# link-time optimisation hands the linker GCC's LTO bytecode, which rustc's
# linker does not read, and every one of their symbols comes back undefined.
options=('!lto')
source=("$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz")
sha256sums=('00ba2da44bd2a5ec58e3bc6c933bc35d16f6fe7a9c5270b06c93d01538ad89ad')

prepare() {
  cd "$pkgname-$pkgver"
  # Vendor up front so the build itself needs no network, which is what
  # makepkg expects and what a clean chroot enforces.
  export RUSTUP_TOOLCHAIN=stable
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/^host: //p')"
}

build() {
  cd "$pkgname-$pkgver"
  export RUSTUP_TOOLCHAIN=stable
  export CARGO_TARGET_DIR=target
  cargo build --frozen --release --bin omacharts
}

check() {
  cd "$pkgname-$pkgver"
  export RUSTUP_TOOLCHAIN=stable
  # The engine's tests and the app's own; neither needs a display.
  cargo test --frozen --release --workspace
}

package() {
  cd "$pkgname-$pkgver"
  local id='com.jorgemanrubia.Omacharts'

  install -Dm755 "target/release/$pkgname" "$pkgdir/usr/bin/$pkgname"
  install -Dm644 "packaging/$id.desktop" "$pkgdir/usr/share/applications/$id.desktop"

  for size in 16 24 32 48 64 128 256 512; do
    install -Dm644 "assets/icons/${size}x${size}/$id.png" \
      "$pkgdir/usr/share/icons/hicolor/${size}x${size}/apps/$id.png"
  done
  install -Dm644 "assets/icons/$id.svg" \
    "$pkgdir/usr/share/icons/hicolor/scalable/apps/$id.svg"

  # The agent skill, and the Claude Code plugin manifest that wraps it.
  # Installed so that `omacharts skill install` has one directory to link every
  # agent at, and so the plugin can be added from a path on the machine rather
  # than fetched again. Codex needs no manifest — the skill directory is the
  # whole story there.
  #
  # Putting the files here changes nothing about anybody's agent: no route runs
  # on its own. Installing a charting app must not write into an agent's
  # configuration, so there is no post-install hook here and there is not going
  # to be one — see src/cli/skill.rs.
  local plugin="$pkgdir/usr/share/$pkgname/agents"
  install -Dm644 agents/.claude-plugin/plugin.json "$plugin/.claude-plugin/plugin.json"
  install -Dm644 agents/.claude-plugin/marketplace.json "$plugin/.claude-plugin/marketplace.json"
  install -Dm644 agents/skills/omacharts/SKILL.md "$plugin/skills/omacharts/SKILL.md"

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

  # Generated by running the binary that is about to be installed, so the
  # completions and the man page can never describe a different version of
  # the command surface from the one on PATH. Needs no display: these are
  # answered before anything GTK touches.
  #
  # Without a bus, though. A command is handed to an omacharts that is
  # already running, so building this with the app open would have the
  # running one answer — and ship a man page describing whichever binary
  # that happened to be. A clean chroot has no bus and never noticed.
  local -x DBUS_SESSION_BUS_ADDRESS=
  local bin="target/release/$pkgname"
  "$bin" surface --completions bash |
    install -Dm644 /dev/stdin "$pkgdir/usr/share/bash-completion/completions/$pkgname"
  "$bin" surface --completions zsh |
    install -Dm644 /dev/stdin "$pkgdir/usr/share/zsh/site-functions/_$pkgname"
  "$bin" surface --completions fish |
    install -Dm644 /dev/stdin "$pkgdir/usr/share/fish/vendor_completions.d/$pkgname.fish"
  "$bin" surface --man |
    install -Dm644 /dev/stdin "$pkgdir/usr/share/man/man1/$pkgname.1"
}
