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

EAPI=8

GHIDRA_EXT_NAME="GhidraExtensionManager"

inherit ghidra-extension

DESCRIPTION="Ghidra extension that installs and updates other Ghidra extensions."
HOMEPAGE="https://github.com/antoniovazquezblanco/GhidraExtensionManager"
SRC_URI="https://github.com/antoniovazquezblanco/${GHIDRA_EXT_NAME}/archive/refs/tags/v${PV}.tar.gz
	-> ${P}.tar.gz"
S="${WORKDIR}/${GHIDRA_EXT_NAME}-${PV}"

# Upstream ships no licence file and states no terms anywhere in the
# repository. The jars vendored in lib/ are Apache-2.0 (commons-io,
# commons-lang3, jackson-*) except github-api, which is MIT.
LICENSE="all-rights-reserved Apache-2.0 MIT"
KEYWORDS="~amd64"
RESTRICT="bindist mirror"

PATCHES=(
	"${FILESDIR}/${PN}-0.3.2-catalog-in-user-settings.patch"
	"${FILESDIR}/${PN}-0.3.2-no-self-update.patch"
)

src_prepare() {
	eapply "${PATCHES[@]}"
	ghidra-extension_src_prepare

	# Generated by the build.gradle task of the same name, from the git tag.
	cat > src/main/java/extensionmanager/ExtensionManagerVersion.java <<-EOF || die
		package extensionmanager;

		public class ExtensionManagerVersion {
		    public static final String GIT_VERSION = "v${PV}";
		}
	EOF
}

src_compile() {
	# Not part of the extension, and everything left here is installed.
	rm -r doc README.md || die

	ghidra-extension_src_compile
}

pkg_postinst() {
	elog "The catalog installed here is the snapshot upstream committed for this"
	elog "release, and it lists nothing for the newest Ghidra versions. Use"
	elog "\"Update catalog\" in the dialog to fetch the current one; it is kept"
	elog "in ~/.config/ghidra."
	elog ""
	elog "Extensions installed from the extension manager go into Ghidra's"
	elog "per-user directory, ~/.config/ghidra, not into this installation, and"
	elog "are not known to Portage. Prefer the packaged extensions in this"
	elog "overlay where one exists, since two copies of the same extension make"
	elog "Ghidra report a duplicate."
}