# Contributor: TBK <alpine@jjtc.eu>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=pijul
pkgver=1.0.0_beta9
_pkgver="1.0.0-beta.${pkgver##*_beta}"
pkgrel=0
pkgdesc="Patch-based distributed version control system"
url="https://pijul.org/"
# s390x: fails to build due to nix crate
arch="all !s390x"
license="GPL-2.0-or-later"
makedepends="
	cargo
	cargo-auditable
	libsodium-dev
	openssl-dev
	zstd-dev
	"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="$pkgname-$pkgver.tar.gz::https://crates.io/api/v1/crates/pijul/$_pkgver/download
	update-cargo-lock.patch
	nix-crate-loongarch64_patch
	"
builddir="$srcdir/$pkgname-$_pkgver"
options="net"

# secfixes:
#   1.0.0_beta8-r1:
#     - CVE-2023-48795

export SODIUM_USE_PKG_CONFIG=1

# taken from main/rust/APKBUILD
_clear_vendor_checksums() {
	sed -i 's/\("files":{\)[^}]*/\1/' vendor/$1/.cargo-checksum.json
}

prepare() {
	default_prepare

	cargo vendor --locked

	_clear_vendor_checksums nix
	patch vendor/nix/src/sys/ioctl/linux.rs \
		"$srcdir"/nix-crate-loongarch64_patch

	mkdir -p .cargo
	cat >> .cargo/config.toml <<-EOF
		[source.crates-io]
		replace-with = "vendored-sources"

		[source.vendored-sources]
		directory = "vendor"
	EOF
}

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

	for shell in bash fish zsh; do
		./target/release/pijul completion $shell > pijul.$shell
	done
}

check() {
	cargo test --frozen
}

package() {
	install -Dvm755 target/release/pijul -t "$pkgdir"/usr/bin/

	install -Dvm644 pijul.bash \
		"$pkgdir"/usr/share/bash-completion/completions/pijul
	install -Dvm644 pijul.fish \
		"$pkgdir"/usr/share/fish/vendor_completions.d/pijul.fish
	install -Dvm644 pijul.zsh \
		"$pkgdir"/usr/share/zsh/site-functions/_pijul
}

sha512sums="
5e4facce9b1620b1ea31d6735ccb5fb0f862f7039cf88b63b4bafb8f5efd7de2a8610cd0cf5b3fcf555a25f6df3fd9d8ea9b34dc439d235b3b87345651402123  pijul-1.0.0_beta9.tar.gz
8223d2e33a9ef9babcafb305bbbc6256fc1831fc0350fed6f555f7e1a46066a171f7b925b54f27e6e8055e82de73f02c756bcb8f419e55f8f07967e79ca564f5  update-cargo-lock.patch
14ffe9ec16139856f428c52d19c3b03fbcc1e10c23aff2bf33679f46552d089f6ee1fae968ceeeb691cff757d55ac631d9d6040780bd8c2a99cdff75df8f3a67  nix-crate-loongarch64_patch
"
