maintainer="Hoang Nguyen <folliekazetani@protonmail.com>"
pkgname=timoni
pkgver=0.23.0
pkgrel=5
pkgdesc="Package manager for Kubernetes powered by CUE"
url="https://timoni.sh"
# blocked by kubernetes
arch="all !armhf"
license="Apache-2.0"
makedepends="go"
checkdepends="etcd kube-apiserver"
subpackages="
	$pkgname-doc
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/stefanprodan/timoni/archive/refs/tags/v$pkgver.tar.gz
	fix-mod-push-test.patch
	"
options="net" # download Go modules

# etcd fails to start for the test process
if [ "$CARCH" = "loongarch64" ]; then
	options="$options !check"
fi

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	go build -v -ldflags "-X main.VERSION=v$pkgver" ./cmd/timoni/

	for shell in bash fish zsh; do
		./timoni completion $shell > timoni.$shell
	done
}

check() {
	case "$CARCH" in
		# etcd defines only these architectures as supported
		# Ref: https://github.com/etcd-io/etcd/blob/ac3d5d77ea5fdbc12ef07a6f6fe1722f06d75b24/server/etcdmain/etcd.go#L264
		x86_64|aarch64|ppc64le|s390x) ;;
		*)
			# shellcheck disable=2155
			export ETCD_UNSUPPORTED_ARCH="$(go env GOARCH)" ;;
	esac

	KUBEBUILDER_ASSETS=/usr/bin go test ./...
}

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

	mkdir -p "$pkgdir"/usr/share/doc/$pkgname
	cp -r examples/ "$pkgdir"/usr/share/doc/$pkgname/

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

sha512sums="
49a9ab7f1612c2432361ed7d908c1be386cb64dd1a4b4cd1fd345c837d683383893e5ac6f446664392126d3ae45714857affb4bfd792c0706dd631c67b8b057d  timoni-0.23.0.tar.gz
a541ddead68c5e3503f935383cbd1cd979f866c60699361aeed9c6d8874b98af33e145513a03dd3c4dedb374bab729e37b7b564f49c5d3b1df946d76023c212a  fix-mod-push-test.patch
"
