# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=helix
pkgver=25.01.1
pkgrel=3
pkgdesc="A post-modern modal text editor"
url="https://helix-editor.com/"
arch="all"
license="MPL-2.0"
makedepends="cargo cargo-auditable"
options="net"
install="$pkgname.post-install $pkgname.post-upgrade"
source="
	https://github.com/helix-editor/helix/archive/$pkgver/helix-$pkgver.tar.gz
	bump-cc-rs.patch
	"
subpackages="
	$pkgname-tree-sitter-vendor:_vendor
	$pkgname-tree-sitter-system:_system:noarch
	"

prepare() {
	default_prepare
	cargo fetch --target="$CTARGET" --locked
}

build() {
	# This path is embedded in the resulting binary so that helix can find
	# system grammars
	# https://github.com/helix-editor/helix/blob/25.01.1/helix-loader/src/lib.rs#L64
	export HELIX_DEFAULT_RUNTIME=/usr/share/helix/runtime
	cargo auditable build --frozen --release
}

check() {
	# This is needed for following tests:
	#	helix-vcs::src::git::test::modified_file
	#	helix-vcs::src::git::test::symlink
	#	helix-vcs::src::git::test::unmodified_file
	unset GIT_CEILING_DIRECTORIES

	cargo test --frozen --workspace
}

package() {
	install -D -m755 target/release/hx -t "$pkgdir"/usr/bin/

	install -D -m644 contrib/Helix.desktop -t "$pkgdir"/usr/share/applications/
	install -D -m644 logo.svg "$pkgdir"/usr/share/icons/hicolor/scalable/apps/Helix.svg

	install -d -m755 "$pkgdir"/usr/share/"$pkgname"

	rm runtime/grammars/.gitkeep
	rm runtime/themes/README.md
	rm -rf runtime/grammars/sources
	cp -r runtime "$pkgdir"/usr/share/"$pkgname"
}

_vendor() {
	provides="helix-tree-sitter=$pkgver-r$pkgrel"
	provider_priority=100

	amove usr/share/helix/runtime/grammars
	amove usr/share/helix/runtime/queries
}

_system() {
	provides="helix-tree-sitter=$pkgver-r$pkgrel"
	provider_priority=50

	mkdir -p  "$subpkgdir"/usr/share/helix/runtime
	ln -Ts ../../../lib/tree-sitter "$subpkgdir"/usr/share/helix/runtime/grammars
	ln -Ts ../../tree-sitter/queries "$subpkgdir"/usr/share/helix/runtime/queries
}

sha512sums="
1f4f63563f68595e69ab65f3204b4a39004133248896b14fe9cfbaf7ed3dce5281618fce35f81a470da54f453ffbf9316fde438544df110f8b4285b70bee767d  helix-25.01.1.tar.gz
93b99ab30b99e68c1bc85245e35a67ca24aae1669862e254e17d4b860a35b8f8bcc2856c937b56aa94e9c2366d042cfd07acc4723ff5755e2a2537ab9a6942ba  bump-cc-rs.patch
"
