# Copyright 2026 Errant Solutions
# Distributed under the terms of the GNU General Public License v2

EAPI=8

DESCRIPTION="High-performance S3/B2-compatible FUSE filesystem (built from source, amd64 + arm64)"
HOMEPAGE="https://github.com/kahing/goofys"
# Upstream v0.24.0 (commit 45b8d78) plus its vendored git submodules, .git dirs stripped. Generated by
# packaging/goofys/make-gentoo-distfile.sh in ctg-infrastructure (upstream has no go.mod: GOPATH mode)
# and published to the Forgejo generic registry.
SRC_URI="https://git.canutethegreat.com/api/packages/CanuteTheGreat/generic/goofys-src/${PV}/${P}-src.tar.xz"
S="${WORKDIR}/${P}"

LICENSE="Apache-2.0"
SLOT="0"
KEYWORDS="~amd64 ~arm64"
RESTRICT="mirror"

# jacobsa/fuse execs `fusermount`; fuse:3 only ships fusermount3
RDEPEND="
	sys-fs/fuse:0
	!net-fs/goofys-bin
"
BDEPEND=">=dev-lang/go-1.21"

# static, already stripped by -ldflags
QA_PREBUILT="usr/bin/goofys"

src_prepare() {
	default
	# the vendored jacobsa/fuse jumps into runtime internals from assembly, which current Go refuses (arm64)
	local b=vendor/github.com/jacobsa/fuse/internal/buffer
	rm -f ${b}/runtime_go1.8.s ${b}/runtime_other.s || die
	cp "${FILESDIR}/runtime.go" ${b}/runtime.go || die
}

src_compile() {
	local gp="${WORKDIR}/gopath"
	mkdir -p "${gp}/src/github.com/kahing" || die
	ln -s "${S}" "${gp}/src/github.com/kahing/goofys" || die
	cd "${gp}/src/github.com/kahing/goofys" || die
	GOPATH="${gp}" GO111MODULE=off CGO_ENABLED=0 GOFLAGS= \
		go build -trimpath -ldflags "-s -w" -o "${T}/goofys" . || die
	"${T}/goofys" --version 2>&1 | grep -q "goofys version ${PV}" || die "version check failed"
}

src_install() {
	dobin "${T}/goofys"
}