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

EAPI=8

# Upstream ships a single archive per release rather than one per Ghidra
# version; this is the version recorded in its extension.properties.
GHIDRA_PV="12.1.2"
GHIDRA_EXT_NAME="GhidraMCP"
MY_PN="${PN%-bin}"

inherit ghidra-extension

DESCRIPTION="Ghidra extension exposing program data over MCP for AI-assisted analysis."
HOMEPAGE="https://github.com/bethington/ghidra-mcp"
SRC_URI="https://github.com/bethington/${MY_PN}/releases/download/v${PV}/${GHIDRA_EXT_NAME}-${PV}.zip
	-> ${P}.zip
	https://github.com/bethington/${MY_PN}/archive/refs/tags/v${PV}.tar.gz
	-> ${MY_PN}-${PV}.tar.gz"
S="${WORKDIR}/${GHIDRA_EXT_NAME}"

LICENSE="Apache-2.0"
KEYWORDS="~amd64"
# Upstream's release archive contains the extension alone. The scripts and the
# documentation live in the repository and are fetched from there.
IUSE="scripts"

src_install() {
	if use scripts; then
		# Ghidra adds a module's own ghidra_scripts directory to the script
		# directories it offers in the Script Manager.
		cp -r "${WORKDIR}/${MY_PN}-${PV}/ghidra_scripts" . || die
	fi

	ghidra-extension_src_install

	# archive/ is superseded material upstream keeps for its own history.
	rm -r "${WORKDIR}/${MY_PN}-${PV}/docs/archive" || die
	dodoc -r "${WORKDIR}/${MY_PN}-${PV}/docs"
}

pkg_postinst() {
	elog "The extension runs an embedded HTTP server inside Ghidra. To drive it"
	elog "from an MCP client you also need upstream's separate Python bridge,"
	elog "which is dev-python/ghidra-mcp-bridge."
}