# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=starship
pkgver=1.22.1
pkgrel=0
pkgdesc="Minimal, blazing-fast and infinitely customizable prompt for any shell"
url="https://starship.rs"
arch="all"
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="
97377922ae0b4e702ddf36534abc9b2fdfbfac50c5f0d4a483a06135f5efef6585abd1fd335ed28f9b0e43b5a0197b724ab1385d3adeef2c75046f5a29fe4ff6  starship-1.22.1.tar.gz
db26430c587cb204a75ec46c236ca4dc2ff5d75a64fb06d4980c5dc5d162af5bfe1f25d7475d9e1d56638113e535b8f1b49e8dbcaca4e066873c98eb99f98ba0  use-libdbus.patch
384a3b84b103005d347ef76c9a61f8c5e21cc97f17f195cff157f71f42dbd76fd6f54f93715cbebbee2eab70a275e39917334721fa21d130bd715a7b6334418b  starship.plugin.zsh
"
