# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=starship
pkgver=1.20.1
pkgrel=0
pkgdesc="Minimal, blazing-fast and infinitely customizable prompt for any shell"
url="https://starship.rs"
# s390x: fails to build nix crate
arch="all !s390x"
license="ISC"
makedepends="
	cargo
	cargo-auditable
	dbus-dev
	openssl-dev>3
	zlib-ng-dev
	"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	$pkgname-zsh-plugin:_zsh_plugin:noarch
	"
source="https://github.com/starship/starship/archive/v$pkgver/starship-$pkgver.tar.gz
	use-libdbus.patch
	starship.plugin.zsh
	"
options="net" # fetching dependencies

# test process exits with SIGTRAP on the riscv64 CI
[ "$CARCH" = "riscv64" ] && options="$options !check"

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

	default_prepare

	# Rust target triple.
	local target=$(rustc -vV | sed -n 's/host: //p')

	# Build against system-provided libz-ng.
	mkdir -p .cargo
	cat >> .cargo/config.toml <<-EOF
		[target.$target]
		z-ng = { rustc-link-lib = ["z-ng"], rustc-cfg = ["zng"] }
	EOF

	# Update after patching Cargo.toml.
	cargo fetch --target="$CTARGET"
}

build() {
	cargo auditable build --frozen --release

	local i; for i in bash fish zsh; do
		./target/release/starship completions $i > target/starship.$i
	done
}

check() {
	# tests fail with this
	unset GIT_CEILING_DIRECTORIES
	# Some tests sporadically fail, try to repeat 3 times before failing.
	local i; for i in $(seq 0 3); do
		[ $i -eq 0 ] || msg "Retrying ($i/3)..."
		cargo test --frozen && return 0
		sleep 1
	done
	return 1
}

package() {
	install -D -m755 target/release/starship -t "$pkgdir"/usr/bin/
	install -D -m644 target/starship.bash "$pkgdir"/usr/share/bash-completion/completions/starship
	install -D -m644 target/starship.fish "$pkgdir"/usr/share/fish/vendor_completions.d/starship.fish
	install -D -m644 target/starship.zsh "$pkgdir"/usr/share/zsh/site-functions/_starship
}

_zsh_plugin() {
	pkgdesc="$pkgdesc (Zsh plugin)"
	depends="$pkgname=$pkgver-r$pkgrel"

	install -D -m644 "$srcdir"/starship.plugin.zsh \
		-t "$subpkgdir"/usr/share/zsh/plugins/starship/
}

sha512sums="
a72ff68a182340aaebaf60f96b7c815c1ee31d8e513aca08eaf81f76427b047cad83bf7931ee64abf70e75ad8f08e1cb03bcea00441125a5a14a6fa17a6cd129  starship-1.20.1.tar.gz
8d28559f37b1794e95035e007612428c023be0c659f668b77d65ba2fd27a47940ab98d946b1671120cb530aca4c1c6628683ebc0bb3f99cdad8b6f454d246539  use-libdbus.patch
384a3b84b103005d347ef76c9a61f8c5e21cc97f17f195cff157f71f42dbd76fd6f54f93715cbebbee2eab70a275e39917334721fa21d130bd715a7b6334418b  starship.plugin.zsh
"
