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

EAPI=8

inherit desktop java-pkg-2 xdg

if [[ ${PV} == 9999 ]]; then
	inherit git-r3
	EGIT_REPO_URI="https://github.com/apache/netbeans.git"
else
	SRC_URI="https://github.com/apache/netbeans/archive/refs/tags/${PV}.tar.gz -> ${P}.tar.gz"
	S="${WORKDIR}/netbeans-${PV}"
	KEYWORDS="~amd64"
fi

DESCRIPTION="Apache NetBeans IDE"
HOMEPAGE="https://netbeans.apache.org/"
LICENSE="Apache-2.0"
SLOT="$(ver_cut 1-1)"

BDEPEND="
	dev-java/ant[bsf]
	>=virtual/jdk-21:*
	net-misc/wget
"
RDEPEND=">=virtual/jdk-21:*"

JAVA_PKG_BSFIX="off"
INSTALL_DIR="/usr/share/${PN}-${SLOT}"

QA_PREBUILT="
	${INSTALL_DIR#/}/ide/bin/nativeexecution/Linux-x86_64/*
	${INSTALL_DIR#/}/ide/modules/lib/amd64/linux/*.so
	${INSTALL_DIR#/}/platform/modules/lib/amd64/linux/*.so
	${INSTALL_DIR#/}/platform/modules/lib/libflatlaf-linux-x86_64.so
	${INSTALL_DIR#/}/profiler/lib/deployed/cvm/linux/*.so
	${INSTALL_DIR#/}/profiler/lib/deployed/jdk15/linux-amd64/*.so
	${INSTALL_DIR#/}/profiler/lib/deployed/jdk16/linux-amd64/*.so
"

pkg_pretend() {
	if has network-sandbox ${FEATURES}; then
		eerror
		eerror "NetBeans downloads dependencies during the build, so the"
		eerror "network-sandbox feature must be disabled for this package."
		eerror "A package.env entry can disable it; see:"
		eerror "https://wiki.gentoo.org/wiki//etc/portage/package.env"
		eerror
		die "network-sandbox is enabled"
	fi
}

src_compile() {
	local binary_cache=${NETBEANS_BINARY_CACHE:-${T}/binaries-cache}
	local -x GRADLE_USER_HOME="${T}/gradle-home"
	local -x ANT_OPTS="-Djava.io.tmpdir=${T}"

	eant \
		-Dcluster.config=full \
		-Dbinaries.cache="${binary_cache}"
}

src_install() {
	local image_dir="${ED}${INSTALL_DIR}"

	pushd nbbuild/netbeans >/dev/null || die

	insinto "${INSTALL_DIR}"
	doins -r .

	find "${image_dir}/ide/bin/nativeexecution" \
		-mindepth 1 -maxdepth 1 -type d ! -name Linux-x86_64 \
		-exec rm -r -- {} + || die "failed to remove foreign native launchers"
	find "${image_dir}/ide/modules/lib" \
		-mindepth 1 -maxdepth 1 -type d ! -name amd64 \
		-exec rm -r -- {} + || die "failed to remove foreign IDE libraries"
	find "${image_dir}/platform/modules/lib" \
		-mindepth 1 -maxdepth 1 -type d ! -name amd64 \
		-exec rm -r -- {} + || die "failed to remove foreign platform libraries"
	find "${image_dir}/platform/modules/lib" -maxdepth 1 -type f \
		\( -name '*.dylib' -o -name '*arm64.so' \) -delete ||
		die "failed to remove foreign FlatLaf libraries"
	find \
		"${image_dir}/profiler/lib/deployed/jdk15" \
		"${image_dir}/profiler/lib/deployed/jdk16" \
		-mindepth 1 -maxdepth 1 -type d ! -name linux-amd64 \
		-exec rm -r -- {} + || die "failed to remove foreign profiler libraries"
	find "${image_dir}/profiler/lib/deployed/cvm" \
		-mindepth 1 -maxdepth 1 -type d ! -name linux \
		-exec rm -r -- {} + || die "failed to remove foreign CVM libraries"
	find "${image_dir}" -type f \
		\( -name '*.exe' -o -name '*.cmd' -o -name '*.bat' -o -name '*.dll' \) \
		-delete || die "failed to remove Windows files"

	find "${image_dir}" -type f -name '*.so*' -exec chmod 0755 {} + ||
		die "failed to make shared libraries executable"
	fperms 0755 \
		"${INSTALL_DIR}/bin/netbeans" \
		"${INSTALL_DIR}/ide/bin/nativeexecution/dorun.sh" \
		"${INSTALL_DIR}/ide/bin/nativeexecution/hostinfo.sh" \
		"${INSTALL_DIR}/ide/bin/nativeexecution/Linux-x86_64/killall" \
		"${INSTALL_DIR}/ide/bin/nativeexecution/Linux-x86_64/process_start" \
		"${INSTALL_DIR}/ide/bin/nativeexecution/Linux-x86_64/pty" \
		"${INSTALL_DIR}/ide/bin/nativeexecution/Linux-x86_64/pty_open" \
		"${INSTALL_DIR}/ide/bin/nativeexecution/Linux-x86_64/sigqueue" \
		"${INSTALL_DIR}/ide/bin/nativeexecution/Linux-x86_64/stat" \
		"${INSTALL_DIR}/extide/ant/bin/ant" \
		"${INSTALL_DIR}/extide/ant/bin/antRun" \
		"${INSTALL_DIR}/extide/ant/bin/antRun.pl" \
		"${INSTALL_DIR}/extide/ant/bin/complete-ant-cmd.pl" \
		"${INSTALL_DIR}/extide/ant/bin/runant.pl" \
		"${INSTALL_DIR}/extide/ant/bin/runant.py" \
		"${INSTALL_DIR}/java/maven/bin/mvn" \
		"${INSTALL_DIR}/java/maven/bin/mvnDebug" \
		"${INSTALL_DIR}/java/maven/bin/mvnyjp"

	dodoc DEPENDENCIES NOTICE
	dosym "${INSTALL_DIR}/bin/netbeans" "/usr/bin/${PN}-${SLOT}"

	insinto "/etc/${PN}-${SLOT}"
	doins etc/*
	rm -r "${image_dir}/etc" || die
	dosym "../../../../etc/${PN}-${SLOT}" "${INSTALL_DIR}/etc"
	sed -i \
		-e 's/#netbeans_jdkhome.*/netbeans_jdkhome=$(java-config -O)/' \
		"${ED}/etc/${PN}-${SLOT}/netbeans.conf" ||
		die "failed to set NetBeans JDK home"

	sed -i \
		-e "s:\"\$progdir\"/../etc/:/etc/${PN}-${SLOT}/:" \
		-e "s:\"\${userdir}\"/etc/:/etc/${PN}-${SLOT}/:" \
		"${image_dir}/bin/netbeans" || die "failed to set NetBeans configuration path"

	dodir /usr/share/icons/hicolor/32x32/apps
	dosym "${INSTALL_DIR}/nb/netbeans.png" \
		"/usr/share/icons/hicolor/32x32/apps/${PN}-${SLOT}.png"

	popd >/dev/null || die

	make_desktop_entry "${PN}-${SLOT}" "NetBeans ${PV}" "${PN}-${SLOT}" Development

	dodir "${INSTALL_DIR}/nb/config"
	printf '%s\n' NBGNT > "${image_dir}/nb/config/productid" || die
}