# Copyright 2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# The Gitea Actions runner (formerly act_runner): a Go daemon that
# registers with a Gitea instance and executes CI workflow jobs, normally
# in containers via the Docker daemon. Its fork of the act workflow engine
# is vendored in-tree by upstream, so the Go module cache tarball covers
# every dependency.

EAPI=8

inherit go-module systemd

# The Go dependency tarball lives as a release asset on this repository,
# tagged gitea-runner-v<version>.
GITEA_RUNNER_DISTFILES="https://git.ipnmod.org/packages/local-ai-overlay/releases/download/gitea-runner-v${PV}"

DESCRIPTION="Daemon that connects to a Gitea instance and runs CI action jobs"
HOMEPAGE="https://gitea.com/gitea/runner"
SRC_URI="
	https://gitea.com/gitea/runner/archive/v${PV}.tar.gz -> ${P}.tar.gz
	${GITEA_RUNNER_DISTFILES}/${P}-deps.tar.xz
"
# Gitea archives extract to the bare repository name.
S="${WORKDIR}/runner"

LICENSE="MIT"
SLOT="0"
KEYWORDS="~amd64"

RDEPEND="
	acct-group/gitea-runner
	acct-user/gitea-runner
"
BDEPEND=">=dev-lang/go-1.26.7"

src_unpack() {
	# go-module_src_unpack's `go mod verify` trips over the go >= 1.27
	# directive before src_prepare relaxes it for the go 1.26 fallback;
	# the eclass offers NONFATAL_VERIFY for exactly this. The Manifest
	# already guarantees the deps tarball's integrity.
	if ! has_version -b ">=dev-lang/go-1.27"; then
		local NONFATAL_VERIFY=1
		einfo "The following 'go mod verify' failure is expected with go < 1.27"
		einfo "and handled: src_prepare relaxes the go directive for the"
		einfo "GOEXPERIMENT=jsonv2 fallback build."
	fi
	go-module_src_unpack
}

src_prepare() {
	default

	einfo "Pointing the systemd unit at the installed binary path"
	sed -i 's#/usr/local/bin/gitea-runner#/usr/bin/gitea-runner#' examples/systemd/gitea-runner.service || die
	grep -q '/usr/bin/gitea-runner' examples/systemd/gitea-runner.service || die "unit path fix did not apply"

	# The code uses the json/v2 stdlib packages that go 1.27 stabilized;
	# go 1.26 ships the same packages behind GOEXPERIMENT=jsonv2 (and
	# dev-lang/go-1.27.0 is masked for regressions at the time of
	# writing). With 1.26 the go.mod directive must be relaxed to match.
	if ! has_version -b ">=dev-lang/go-1.27"; then
		einfo "Relaxing go directive for go 1.26 + GOEXPERIMENT=jsonv2"
		sed -i -e 's/^go 1.27$/go 1.26/' -e '/^toolchain /d' go.mod || die
		grep -q '^go 1.26$' go.mod || die "go directive relax did not apply"
	fi
}

src_compile() {
	if ! has_version -b ">=dev-lang/go-1.27"; then
		export GOEXPERIMENT=jsonv2
		# The relaxed go directive makes go want to rewrite go.mod's
		# requirement graph; allow it (last -mod flag wins, and GOPROXY
		# stays off so everything still comes from the module cache).
		export GOFLAGS="${GOFLAGS} -mod=mod"
	fi

	# Version stamped the same way upstream's release builds do it.
	# Upstream's Makefile also adds -s -w; leave stripping to Portage
	# instead (it would flag the binary as pre-stripped otherwise).
	local ldflags=(
		-X "gitea.com/gitea/runner/internal/pkg/ver.version=v${PV}"
	)
	ego build -ldflags "${ldflags[*]}" -o gitea-runner

	# The default configuration file, generated by the freshly built
	# binary so it always matches this version's options.
	./gitea-runner generate-config > config.yaml || die
}

src_install() {
	dobin gitea-runner

	insinto /etc/gitea-runner
	doins config.yaml

	newinitd "${FILESDIR}"/gitea-runner.initd gitea-runner
	newconfd "${FILESDIR}"/gitea-runner.confd gitea-runner
	systemd_dounit examples/systemd/gitea-runner.service

	# The .runner registration file and job workspaces live here.
	keepdir /var/lib/gitea-runner
	fowners gitea-runner:gitea-runner /var/lib/gitea-runner
}

pkg_postinst() {
	elog "Register the runner before first start (as the service user, so"
	elog "the registration lands in its working directory):"
	elog "  su -s /bin/sh gitea-runner -c 'cd /var/lib/gitea-runner && \\"
	elog "    gitea-runner register --config /etc/gitea-runner/config.yaml'"
	elog "Then adjust /etc/gitea-runner/config.yaml and start the service."
}