# Contributor: Andrei Jiroh Eugenio Halili <ajhalili2006@andreijiroh.eu.org>
# Contributor: Robert Günzler <r@gnzler.io>
# Contributor: Xe <xe@tailscale.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Robert Günzler <r@gnzler.io>
pkgname=tailscale
pkgver=1.84.2
pkgrel=0
pkgdesc="The easiest, most secure way to use WireGuard and 2FA"
url="https://tailscale.com/"
license="BSD-3-Clause"
# s390x: upstream still doesn't have hardware to debug this
arch="all !s390x"
options="chmod-clean"
makedepends="
	go
	linux-headers
	"
checkdepends="
	helm
	iproute2-minimal
	openssh
	"
pkgusers="tailscale"
pkggroups="tailscale"
install="
	$pkgname.pre-install
	$pkgname.pre-upgrade
	$pkgname.post-install
	$pkgname.post-upgrade
	"
subpackages="
	$pkgname-doc
	$pkgname-openrc
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/tailscale/tailscale/archive/v$pkgver.tar.gz
	$pkgname.confd
	$pkgname.initd
	$pkgname.logrotate
	$pkgname.modules-load
	$pkgname.doasd
	resolvconf
	test-bump-conn-max-overhead.patch
	"

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

# secfixes:
#   1.66.1-r0:
#     - CVE-????-????? TS-2024-005
#   1.32.3-r0:
#     - CVE-2022-41924 TS-2022-004
#   0:
#     - CVE-2022-41925 TS-2022-005

prepare() {
	default_prepare

	# this script attempts to download a helm binary
	# use the helm we have in repo instead
	rm -v tool/helm
	ln -sv /usr/bin/helm tool/helm
}

build() {
	local tags="netcgo,ts_omit_aws,ts_omit_kube,ts_include_cli"
	local ldflags="
		-X tailscale.com/version.shortStamp=$pkgver
		-X tailscale.com/version.longStamp=$pkgver-AlpineLinux
		-X tailscale.com/version.gitCommitStamp=AlpineLinux
		"

	go build -v -tags "$tags" -ldflags "$ldflags" ./cmd/tailscaled
	ln -sv ./tailscaled ./tailscale

	mkdir -p completions
	local shell; for shell in bash fish zsh; do
		./tailscale completion $shell > completions/tailscale.$shell
	done
}

check() {
	# flaky test, on multiple archs
	local test_args="-skip TestAllEndpointsAreUpAndReturnExpectedResponse"

	# shellcheck disable=SC2046
	go run ./cmd/testwrapper \
		$(go list ./... | grep -Ev 'util/linuxfw|net/netcheck|wgengine/magicsock|tstest/(archtest|integration|jsdeps|iosdeps)|ssh/tailssh|containerboot|net/connstats|tool/gocross|derp/xdp') \
		$test_args
}

package() {
	install -m755 -D tailscaled -t "$pkgdir"/usr/sbin/
	mkdir -p "$pkgdir"/usr/bin
	ln -sv /usr/sbin/tailscaled "$pkgdir"/usr/bin/tailscale

	install -m644 -D completions/tailscale.bash "$pkgdir"/usr/share/bash-completion/completions/tailscale
	install -m644 -D completions/tailscale.fish "$pkgdir"/usr/share/fish/vendor_completions.d/tailscale.fish
	install -m644 -D completions/tailscale.zsh "$pkgdir"/usr/share/zsh/site-functions/_tailscale

	install -m644 -D README.md -t "$pkgdir"/usr/share/doc/$pkgname/

	install -m755 -D "$srcdir"/tailscale.initd "$pkgdir"/etc/init.d/$pkgname
	install -m644 -D "$srcdir"/tailscale.confd "$pkgdir"/etc/conf.d/$pkgname
	install -m644 -D "$srcdir"/tailscale.logrotate "$pkgdir"/etc/logrotate.d/$pkgname
	install -m644 -D "$srcdir"/tailscale.modules-load "$pkgdir"/usr/lib/modules-load.d/$pkgname.conf

	install -m755 -D "$srcdir"/resolvconf -t "$pkgdir"/usr/libexec/$pkgname/
	install -m640 -D "$srcdir"/$pkgname.doasd "$pkgdir"/etc/doas.d/$pkgname.conf

	install -d -m750 -o tailscale -g tailscale "$pkgdir"/var/lib/$pkgname
}

sha512sums="
f4baaa9070a2ad4f4ddef9bac89fc5333dc1a74db68a976c30a20a7359ebc51d14905ef0098b1046493fb99a829b9b10502e03b12799e94ef543ebffc6018553  tailscale-1.84.2.tar.gz
73387de31f04d2cd1b5c64fdc4306b42dccfdb84a050df7c0f625ce8a29d0f5d278c4235b92844cc673fce4930318efdd17cc2bdb577e30b4c87fd8e63914ebc  tailscale.confd
5a231233a3cbb510fb2c36c059764d97e1eb49f41b826e4e3a955fd81748a0b84a0220dd27b5f3f9cfd60bdc6c867134df1313d52e5609d98922be1a96decee1  tailscale.initd
4f44f59553142bff5ca0914543114e4a6e3e413a96353543551445be6fa3ddc1037492baf0e091c95fe22a63e639b58bc5040285b8b5d246dbb57b70337d5178  tailscale.logrotate
871b650ff982be061532b4c5fe4812f41e6e2c58fc69b24f8a745f9a43389da44e742a69b0467b3c3d9e2b031af0728e20f10fa4584695c4f5ac87768a1fd34e  tailscale.modules-load
2a5b939bef6fc81909e81dc48a4aeb71c9e0bbeeeb2e6ea19f5c9502d1d327435baee9e42528fb9fd4061eab6fa9ecb969416d0e1e1c7a7e4aa8c20e0593fb2b  tailscale.doasd
5b986e8e8bb66a8e8e959958b73d74941b81b45785d339fae203343a584b743f96e1443456ef45a7a4ef6a8c4be0506824891906f4e85532b5cf0760f9485369  resolvconf
8e7cae496f9910c202cdf7216affcddb6f54df3f8fffb35257fe77bf90c4508b5025498b340b4f38d08a42f62d387ffcdaff24b33aeeadfdb4663fd9f7c2c03b  test-bump-conn-max-overhead.patch
"
