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

EAPI=8

inherit unpacker

# upstream tags release candidates as v0.41.0-rc1
MY_PV="${PV/_rc/-rc}"

DESCRIPTION="Ollama - get up and running with large language models - additional AMD (ROCm) package"
HOMEPAGE="https://ollama.com/"
# distfile name kept as before, so existing users don't download the ROCm tarball again
SRC_URI="https://github.com/ollama/ollama/releases/download/v${MY_PV}/ollama-linux-amd64-rocm.tar.zst -> ${P}.gh.tar.zst"
S="${WORKDIR}"

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

# upstream prebuilt binaries
RESTRICT="strip"
QA_PREBUILT="*"

# the ROCm backend has to match the main package version exactly
RDEPEND="~sci-ml/ollama-${PV}"
BDEPEND="
	$(unpacker_src_uri_depends)
	dev-util/patchelf
"

src_unpack() {
	if [[ -n ${A} ]]; then
		echo "Unpacking...."
		unpacker ${A} || die "Unpack failed"
		echo "Unpacking finished."
	fi
}

src_install() {
	dodir /usr
	cp -R "${S}"/. "${ED}"/usr/ || die "install failed"

	# bundled ROCm libs sit next to the backend ($ORIGIN),
	# libggml-base.so comes from sci-ml/ollama in /usr/lib/ollama
	patchelf --set-rpath '$ORIGIN:/usr/lib/ollama' \
		"${ED}"/usr/lib/ollama/rocm/libggml-hip.so || die "patchelf failed"
}