# Maintainer: Marko Ivankovic <marko@ivankovic.me>

pkgname=omnidiff
pkgver=0.4.0
pkgrel=1
pkgdesc="Fast, robust, accurate content-aware diffing"
arch=('x86_64' 'aarch64')
url="https://github.com/ivankovic/omnidiff"
license=('AGPL-3.0-only')
depends=('gcc-libs' 'glibc')
makedepends=('cargo')
# Sourced from the GitHub tag rather than the crates.io tarball: the published crate carries
# neither tests/, src/bin/ nor the fixture corpus (see Cargo.toml's `include`), so `check()` would
# have nothing to run. The tradeoff is that this download also carries research/ and the test corpus.
source=("$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz")
# sha256 of the v0.3.0 tag tarball. Until `updpkgsums` runs after the next tag exists, this is
# the previous tag's hash, and the package does not build. Re-run `updpkgsums` in this
# directory after every tag; do not guess it, and do not read it from the release's
# SHA256SUMS.txt - that asset covers the *release assets* (the prebuilt binaries, the .deb, the
# completions tarball), and GitHub's auto-generated source tarball is not one of them.
sha256sums=('82046f16077ecbb8122818bd9ea6ee1a0f01c0ca4650b037afbf610e155f33e5')

prepare() {
	cd "$pkgname-$pkgver"
	# Network is permitted in prepare() but not in build(), so every crate is fetched here and
	# build() then runs --frozen. --locked keeps the resolution identical to the committed
	# Cargo.lock, which is what upstream's CI actually tested.
	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
	# Default features only (`tui`) - the product binary. `stats`/`test-fixtures` gate
	# dataset-analysis dev tools that pull in git2 and a bundled SQLite and are not shipped.
	#
	# Note: upstream's release profile sets `lto = "fat"` and `codegen-units = 1`, and every
	# tree-sitter grammar is compiled from C. This build takes minutes, not seconds.
	cargo build --frozen --release --bin omnidiff
}

check() {
	cd "$pkgname-$pkgver"
	export RUSTUP_TOOLCHAIN=stable
	# --lib only: the fixture-corpus tests are upstream's accuracy benchmark, needing the
	# `test-fixtures` feature plus substantial time and memory. Not a packaging sanity check.
	cargo test --frozen --release --lib
}

package() {
	cd "$pkgname-$pkgver"
	install -Dm0755 "target/release/$pkgname" "$pkgdir/usr/bin/$pkgname"

	# Generated by the binary just built, from the same clap definition as --help. Native build
	# only; a cross-compiled package would need these produced on the host instead.
	local bin="target/release/$pkgname"
	"$bin" util man | install -Dm0644 /dev/stdin "$pkgdir/usr/share/man/man1/$pkgname.1"
	"$bin" util completions bash | install -Dm0644 /dev/stdin \
		"$pkgdir/usr/share/bash-completion/completions/$pkgname"
	"$bin" util completions zsh | install -Dm0644 /dev/stdin \
		"$pkgdir/usr/share/zsh/site-functions/_$pkgname"
	"$bin" util completions fish | install -Dm0644 /dev/stdin \
		"$pkgdir/usr/share/fish/vendor_completions.d/$pkgname.fish"

	install -Dm0644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
	install -Dm0644 README.md "$pkgdir/usr/share/doc/$pkgname/README.md"
}
