# Contributor: Patrycja Rosa <alpine@ptrcnull.me>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=headscale
pkgver=0.26.1
pkgrel=0
pkgdesc="Open source, self-hosted implementation of the Tailscale control server"
url="https://github.com/juanfont/headscale"
# ppc64le, riscv64: go build fails
# armhf, armv7: tests fail on sqlite read ("database disk image is malformed")
arch="all !ppc64le !riscv64 !armhf !armv7"
license="BSD-3-Clause"
pkgusers="headscale"
pkggroups="headscale"
makedepends="go"
checkdepends="postgresql"
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="
b388ec44f4df3eaea2014673ac9f9fa9a70db9f5f9267b57b65f27180f6a54fc2039f4a5ffbb9b4a4c630eec10083d8e1c6b3dd67d44fb0c4c4c15f95a4db6bb  headscale-0.26.1.tar.gz
0800829bfc087af283afc117406324a0129b30b587c8cc5df85e147ac09fc879d726fc2d0b62ed545fb0190ed887641f07256745da9dea56932dd2d90aa41625  headscale.initd
"
