# Contributor: Anon <danilagdn.2004@gmail.com>
# Maintainer: Lindsay Zhou <i@lin.moe>
maintainer="Lindsay Zhou <i@lin.moe>"
pkgname=sing-box
pkgver=1.11.9
pkgrel=1
pkgdesc="The universal proxy platform"
url="https://sing-box.sagernet.org/"
arch="all"
license="GPL-3.0-or-later with name use or association addition"
makedepends="go"
subpackages="$pkgname-openrc $pkgname-bash-completion $pkgname-fish-completion $pkgname-zsh-completion"
options="net" # go modules
source="$pkgname-$pkgver.tar.gz::https://github.com/SagerNet/sing-box/archive/v$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	"
_tags="with_gvisor,with_quic,with_wireguard,with_utls,with_reality_server,with_clash_api,with_ech,with_dhcp,with_acme"

build() {
	export CGO_CPPFLAGS="$CPPFLAGS"
	export CGO_CFLAGS="$CFLAGS"
	export CGO_CXXFLAGS="$CXXFLAGS"
	export CGO_LDFLAGS="$LDFLAGS"

	go build -v -tags "$_tags" \
	-ldflags "-X \"github.com/sagernet/sing-box/constant.Version=$pkgver\" -s -w
	-buildid= -linkmode=external" ./cmd/sing-box

	install -d completions
	go run ./cmd/sing-box completion bash   > completions/bash
	go run ./cmd/sing-box completion fish   > completions/fish
	go run ./cmd/sing-box completion zsh    > completions/zsh
}

check() {
	go test -v ./...
	# require docker to run test cases in the "test" submodule,
	# so we don't use `make test`.
}

package() {
	install -Dm644 LICENSE                      -t "$pkgdir/usr/share/licenses/$pkgname"
	install -Dm755 "$pkgname"                   -t "$pkgdir/usr/bin"
	install -Dm644 "release/config/config.json" -t "$pkgdir/etc/$pkgname"
	install -Dm755 "$srcdir/$pkgname.initd" 		"$pkgdir/etc/init.d/$pkgname"
	install -Dm755 "$srcdir/$pkgname.confd" 		"$pkgdir/etc/conf.d/$pkgname"

	install -Dm644 completions/bash "$pkgdir/usr/share/bash-completion/completions/$pkgname.bash"
	install -Dm644 completions/fish "$pkgdir/usr/share/fish/vendor_completions.d/$pkgname.fish"
	install -Dm644 completions/zsh  "$pkgdir/usr/share/zsh/site-functions/_$pkgname"

}

sha512sums="
b4161b4b6a17d4f53a4c073e39d1d369b9fef5484fbecaaab14a97dbbcda3a9e6a6b46e2370c71b90226bf06291ddb638d67976e9b33e6fa54a2e8b4025bf30f  sing-box-1.11.9.tar.gz
1fd26edaeefb94d3fbf53a8310efad1776c2a90c6497481def603667e565bbca9f75ef670bb1e3c06488ab8a68ff27943f2f828fdb2b869ee6c2da95425cbccb  sing-box.initd
448184b1ea062f93854c61c86b591681b35767bf475c2643e416e41f84da5b1a793351613470909b17c533ddf964ccbf438aa3454073e8e84d0f4d3ef0385654  sing-box.confd
"
