# Contributor: Guy Godfroy <guy.godfroy@gugod.fr>
# Maintainer: Guy Godfroy <guy.godfroy@gugod.fr>
pkgname=otf
pkgver=0.3.23
pkgrel=0
pkgdesc="Open source alternative to Terraform Enterprise"
url="https://docs.otf.ninja/"
arch="x86_64 armhf armv7 aarch64" # terraform and opentofu pre-built binaries available only for those archs
license="MPL-2.0"
depends="otf-server otf-cli"
makedepends="go"
install="otf-server.pre-install otf-server.post-install otf-agent.pre-install"
subpackages="
	$pkgname-cli
	$pkgname-server
	$pkgname-server-openrc:server_openrc
	$pkgname-agent
	$pkgname-agent-openrc:agent_openrc
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/leg100/otf/archive/refs/tags/v$pkgver.tar.gz
	otfd.initd
	otfd.confd
	otf-agent.initd
	otf-agent.confd
	"
options="net"

build() {
	mkdir build
	local LD_FLAGS="-s -w -X github.com/leg100/otf/internal.Version=$pkgver"
	go build -ldflags="$LD_FLAGS" -o build ./...
}

check() {
	# TestExecutor_execute: takes ages and then timeout
	# internal/integration, Test{Pool,DB}: needs docker
	local toskip="TestExecutor_execute|TestPool|TestDB"
	# shellcheck disable=SC2046
	go test -skip "$toskip" $(go list ./... | grep -v internal/integration)
}

package() {
	# Main package is only a metapackage depending of server and cli component.
	# This is because main component should be server, but the binary named "otf" is the cli tool.
	# So to avoid confusion, all components are subpackaged.
	mkdir -p "$pkgdir"
}

cli() {
	unset depends
	pkgdesc="Command line tool for otf"
	install -m755 -D "$builddir"/build/otf \
		"$subpkgdir"/usr/bin/otf
}

server() {
	depends="xxd"
	pkgdesc="Server component for otf"
	install -m755 -D "$builddir"/build/otfd \
		"$subpkgdir"/usr/bin/otfd
}

server_openrc() {
	unset depends
	pkgdesc="Server component for otf (OpenRC init scripts)"
	install_if="$pkgname-server=$pkgver-r$pkgrel openrc"
	install -m755 -D "$srcdir"/otfd.initd \
		"$subpkgdir"/etc/init.d/otfd
	install -m644 -D "$srcdir"/otfd.confd \
		"$subpkgdir"/etc/conf.d/otfd
}

agent() {
	unset depends
	pkgdesc="Agent for otf"
	install -m755 -D "$builddir"/build/otf-agent \
		"$subpkgdir"/usr/bin/otf-agent
}

agent_openrc() {
	unset depends
	install_if="$pkgname-agent=$pkgver-r$pkgrel openrc"
	pkgdesc="Agent for otf (OpenRC init scripts)"
	install -m755 -D "$srcdir"/otf-agent.initd \
		"$subpkgdir"/etc/init.d/otf-agent
	install -m644 -D "$srcdir"/otf-agent.confd \
		"$subpkgdir"/etc/conf.d/otf-agent
}

sha512sums="
1d50e306a62187747fab89c9fe951797c25df7dbfb949df69176b4cf3e5c74e44892a208e5d6ac428d596d1dbc993f036fce23dfd9c492977123e7c0918b35d5  otf-0.3.23.tar.gz
ca8bda80412cd7b26cfff97e277a74de54370c59adb9e3c1881c4663fd9f0b0bb9eb1f8f5516b9bf4bee96a28f8468843f80e14d79f58766cc132fda0d23823f  otfd.initd
40ede6045bdc6f5274b616e217594bee51c0a6fc6f5a612e21a9c611ef419b5b3016ad92eb74bd7fad5da04f031cff08bff8f8c963042cffd19b9ec1a1d5f263  otfd.confd
f1a5bc6cb03f1ed4515b6a3305c1a3918aee97b5b4ae1b1121ba3dcb0f9a02db4a39107b5d1077f9ea49938b6fd62a3d8a1898203950703fd5e0ba87461c3b85  otf-agent.initd
2832082ec2c9e76035d9a228a2ad5e3cc09c55b6bb53cfabf466a07d877bf57321b210fb31b42f3264506ce0dc456d25bf54ceae381aac79a487dc442243c9b5  otf-agent.confd
"
