# Contributor: Michał Polański <michal@polanski.me>
# Maintainer: Michał Polański <michal@polanski.me>
pkgname=rustic
# NOTE: do not move out of the testing repository until upstream says it's no longer beta-quality software
pkgver=0.9.3
pkgrel=0
pkgdesc="Fast, encrypted, deduplicated backups"
url="https://github.com/rustic-rs/rustic"
license="MIT OR Apache-2.0"
# loongarch64: blocked by libc crate
arch="all !s390x !loongarch64" # fails to build nix crate
makedepends="
	cargo
	cargo-auditable
	openssl-dev
	zstd-dev
	"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
# NOTE: Keep in sync with rustic_core in Cargo.toml.
_core_ver=0.5.3
source="https://github.com/rustic-rs/rustic/archive/v$pkgver/rustic-$pkgver.tar.gz
	https://github.com/rustic-rs/rustic_core/archive/rustic_core-v$_core_ver/rustic_core-rustic_core-v$_core_ver.tar.gz
	build-with-system-tls.patch
	"
options="net" # fetch dependencies

prepare() {
	mkdir -p vendor
	mv "$srcdir"/rustic_core-rustic_core-v$_core_ver vendor/rustic_core

	default_prepare

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

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

	cargo fetch --target="$CTARGET" --locked
}

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

	target/release/rustic completions bash > $pkgname.bash
	target/release/rustic completions fish > $pkgname.fish
	target/release/rustic completions zsh > $pkgname.zsh
}

check() {
	cargo test --frozen
}

package() {
	install -Dm755 target/release/$pkgname -t "$pkgdir"/usr/bin/

	install -Dm644 $pkgname.bash "$pkgdir"/usr/share/bash-completion/completions/$pkgname
	install -Dm644 $pkgname.fish "$pkgdir"/usr/share/fish/vendor_completions.d/$pkgname.fish
	install -Dm644 $pkgname.zsh "$pkgdir"/usr/share/zsh/site-functions/_$pkgname
}

sha512sums="
78567f61f109e19b307da81db7d666ff42767e4c35e938d796b097faed8e841a340edf95810bcf3e52c7dde793c6d9ea64df25ff4cdafa19d19aaf4398e20b8b  rustic-0.9.3.tar.gz
140d8fd43bbf93a90a4f3847a15913cd01ae88f495beb5810d842328ef47e9eb95c065dd0e95fef91dc761e570a707a4dfdff7f1c2a1829def43654030ae225e  rustic_core-rustic_core-v0.5.3.tar.gz
e7c0efcf605fe1e85f3c6c0a09d58c93412d113dfdf7142058304bef43dd28c75c8083fa65750ba23e5559f1c4ee758db6dc6d9d3ca155357752a92d4a1f2164  build-with-system-tls.patch
"
