c_josie_overlay

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

EAPI=8

inherit desktop systemd unpacker optfeature

DESCRIPTION="Folding@home v8 client (Bastet), a distributed protein-folding client"
HOMEPAGE="https://foldingathome.org/"
# Upstream publishes v8 only as .deb/.rpm/.pkg (plus a binary tarball). The source
# (github.com/FoldingAtHome/fah-client-bastet) builds with SCons but needs cbang,
# which is not packaged in Gentoo, so this repackages the Debian build.
SRC_URI="https://download.foldingathome.org/releases/public/fah-client/debian-10-64bit/release/fah-client_${PV}_amd64.deb"
S="${WORKDIR}"

LICENSE="GPL-3+"
SLOT="0"
KEYWORDS="~amd64"
IUSE="systemd webui rocm-opencl"
RESTRICT="bindist mirror strip"
QA_PREBUILT="usr/bin/fah-client"

DEPEND="dev-util/patchelf"
RDEPEND="
	acct-group/fah-client
	acct-user/fah-client
	app-arch/lz4
	app-misc/ca-certificates
	virtual/zlib
	systemd? ( sys-apps/systemd )
	!systemd? ( sys-auth/elogind )
	webui? ( sci-biology/fah-web-control-bin )
	rocm-opencl? ( dev-libs/rocm-opencl-runtime )
"

src_prepare() {
	default

	# The upstream binary is linked against systemd's libsystemd.so.0. That library
	# is unavailable on OpenRC/elogind systems (sys-auth/elogind blocks
	# sys-apps/systemd), but elogind is a systemd fork and exports the seven
	# sd_bus_* symbols the client uses at the same LIBSYSTEMD_221/222 symbol
	# versions, so point the dependency at it instead.
	if ! use systemd; then
		patchelf --replace-needed libsystemd.so.0 libelogind.so.0 \
			usr/bin/fah-client || die
	fi
}

src_install() {
	dobin usr/bin/fah-client usr/bin/fahctl

	if use systemd; then
		systemd_dounit lib/systemd/system/fah-client.service
	fi
	newinitd "${FILESDIR}"/fah-client.initd fah-client

	insinto /etc/fah-client
	if use webui; then
		# Point the client's built-in web server at the locally installed Web Control.
		cat > "${T}"/config.xml <<-EOF || die
		<config>
		  <web-root v='/usr/share/fah-client/webui'/>
		</config>
		EOF
		newins "${T}"/config.xml config.xml
	else
		doins "${FILESDIR}"/config.xml
	fi

	insinto /usr/share/polkit-1/rules.d
	doins usr/share/polkit-1/rules.d/10-fah-client.rules

	domenu usr/share/applications/fah-client.desktop

	insinto /usr/share/pixmaps
	doins usr/share/pixmaps/fahlogo.png

	keepdir /var/lib/fah-client /var/log/fah-client
	fowners fah-client:fah-client /var/lib/fah-client /var/log/fah-client
	fperms 0750 /var/lib/fah-client

	dodoc usr/share/doc/fah-client/README.md
}

pkg_postinst() {
	optfeature "command-line control with fahctl" dev-python/websocket-client
	optfeature "sleep inhibition through logind" sys-auth/polkit
	if use webui; then
		elog "Local Web Control is installed at http://localhost:7396/"
	fi
	if use rocm-opencl; then
		elog "The ROCm OpenCL runtime is installed; check that it is detected with: clinfo"
	fi
	elog "Control the client from your browser at https://v8-5.foldingathome.org/"
	elog "or with fahctl. Start the service with:"
	elog "(openrc)\trc-service fah-client start"
	elog "(systemd)\tsystemctl start fah-client"
	elog "GPU folding uses HIP/ROCm, OpenCL or CUDA, all detected at runtime."
}