# Contributor: Patrycja Rosa <alpine@ptrcnull.me>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=headscale
pkgver=0.23.0
pkgrel=5
pkgdesc="Open source, self-hosted implementation of the Tailscale control server"
url="https://github.com/juanfont/headscale"
# ppc64le, riscv64, loongarch64: go build fails
# armhf, armv7: tests fail on sqlite read ("database disk image is malformed")
arch="all !ppc64le !riscv64 !loongarch64 !armhf !armv7"
license="BSD-3-Clause"
pkgusers="headscale"
pkggroups="headscale"
makedepends="go"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-openrc
	$pkgname-zsh-completion
	"
install="$pkgname.pre-install"
source="https://github.com/juanfont/headscale/archive/v$pkgver/headscale-$pkgver.tar.gz
	headscale.initd
	"
options="net" # fetch dependencies

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

prepare() {
	default_prepare

	# move socket to a subdirectory to allow running as non-root
	sed -i 's|/var/run/headscale.sock|/run/headscale/headscale.sock|' config-example.yaml
}

build() {
	go build -ldflags "-X github.com/juanfont/headscale/cmd/headscale/cli.Version=v$pkgver" ./cmd/headscale
	mkdir completions
	./headscale completion bash > completions/bash
	./headscale completion fish > completions/fish
	./headscale completion zsh > completions/zsh
}

check() {
	# headscale runs integration tests if /.dockerenv exists, but those
	# tests fail in gitlab CI because they rely on a certain environment
	# (see test_integration target in headscale's Makefile). so, use -short
	# to disable integration tests when packaging.
	go test -short ./...
}

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

	install -Dm755 "$srcdir"/headscale.initd "$pkgdir"/etc/init.d/headscale
	install -Dm644 config-example.yaml "$pkgdir"/etc/headscale/config.yaml

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

sha512sums="
6ade2452f5cc4a4f1abe61d0501ef3053fa0e361b0dea0058dd3fa4ec56678e6da1c88ce3edc07fa82eb9cfea3d835039fa1f003c55c65614c93048cbefefc92  headscale-0.23.0.tar.gz
0800829bfc087af283afc117406324a0129b30b587c8cc5df85e147ac09fc879d726fc2d0b62ed545fb0190ed887641f07256745da9dea56932dd2d90aa41625  headscale.initd
"
