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

EAPI=8

DESCRIPTION="Apache Guacamole is a clientless remote desktop gateway"
HOMEPAGE="https://guacamole.apache.org/"

# Download mvn and node_modules:
# tar -xf guacamole-client-1.6.0.tar.gz (don't use mc!!)
# cd guacamole-client-1.6.0
# Any JDK >= 11 can be used
# LC_ALL=C LANG=en-US.UTF-8 mvn dependency:go-offline -Dmaven.repo.local=.m2 -Drat.ignoreErrors=true package

# Compression process:
# cd ..
# tar --create --auto-compress --file guacamole-client-1.6.0-mvn.tar.xz guacamole-client-1.6.0/.m2
# tar --create --auto-compress --file guacamole-client-1.6.0-node_modules.tar.xz \
#	guacamole-client-1.6.0/guacamole/src/main/frontend/node_modules guacamole-client-1.6.0/guacamole-common-js/node

if [[ "${PV}" == *9999 ]] ; then
	inherit git-r3
	EGIT_REPO_URI="https://github.com/apache/guacamole-client.git"
	EGIT_BRANCH="staging/1.6.0"
else
	SRC_URI="https://mirrors.ircam.fr/pub/apache/guacamole/${PV}/source/${P}.tar.gz"
fi
SRC_URI+="
	https://www.inode64.com/dist/${P}-node_modules.tar.xz
	https://www.inode64.com/dist/${P}-mvn.tar.xz
	mysql? ( https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-j-8.3.0.tar.gz )
	postgres? ( https://jdbc.postgresql.org/download/postgresql-42.7.3.jar )
	"

LICENSE="Apache-2.0"
SLOT="0"
KEYWORDS="~amd64"
IUSE="ldap +mysql postgres radius sso test totp"
REQUIRED_USE="|| ( mysql postgres )"
BDEPEND="
	>=dev-java/maven-bin-3.9.6
	>=virtual/jdk-11:*
	"
RDEPEND="
	ldap? ( net-nds/openldap )
	>=virtual/jre-11:*
	www-servers/tomcat:9
	"
RESTRICT="!test? ( test )"

PATCHES=(
	"${FILESDIR}"/npm_offline.diff
)

MY_PN="guacamole"
MY_PV="$(ver_cut 1-3)"
GUACAMOLE_HOME="/etc/${MY_PN}"
CLASSPATH="${GUACAMOLE_HOME}/lib"

# Maven/frontend plugins may still fetch artifacts missing from the vendor
# archives. Keep this exception until a complete offline build is verified.
RESTRICT+=" network-sandbox"

src_unpack() {
	if [[ "${PV}" == *9999 ]] ; then
		git-r3_src_unpack
	else
		unpack ${P}.tar.gz
	fi

	# livecheck regenerates these archives from an ebuild whose SRC_URI
	# does not list them yet, so they are not always in A.
	local archive
	for archive in ${P}-node_modules.tar.xz ${P}-mvn.tar.xz; do
		if has ${archive} ${A}; then
			unpack ${archive}
		fi
	done

	if use mysql; then
		# Only the driver is installed. The connector sources carry a pom.xml
		# that livecheck would take for the guacamole build.
		tar -xf "${DISTDIR}"/mysql-connector-j-8.3.0.tar.gz -C "${WORKDIR}" \
			mysql-connector-j-8.3.0/mysql-connector-j-8.3.0.jar || die
	fi
}

src_prepare() {
	default

	# Upstream turns every javac warning into an error, which breaks the
	# build with newer JDKs (new lint categories, obsolete -source/-target)
	sed -i 's|<failOnWarning>true</failOnWarning>|<failOnWarning>false</failOnWarning>|' \
		pom.xml || die
	sed -i '/<arg>-Werror<\/arg>/d' doc/guacamole-example/pom.xml || die
}

src_compile() {
	local java_major
	java_major=$(java -version 2>&1 | head -n 1 | cut -d '"' -f 2 | cut -d '.' -f 1)
	if [[ ${java_major} -lt 11 ]]; then
		die "Java >= 11 is required, select it with 'eselect java-vm'"
	fi

	export LC_ALL=C
	export LANG=en-US.UTF-8

	local myconf=(
		-Dmaven.repo.local="${S}/.m2"
		-Drat.ignoreErrors=true
		-Dmaven.compiler.release=11
		-Dmaven.javadoc.skip=true
		-P lgpl-extensions
	)

	if ! use test; then
		myconf+=(
			-DskipTests=true
	    )
	fi

	mvn package ${myconf[@]} || die
}

src_install() {
	insinto "${GUACAMOLE_HOME}/extensions"
	#doins extensions/guacamole-auth-duo/target/guacamole-auth-duo-${MY_PV}.jar
	doins extensions/guacamole-auth-header/target/guacamole-auth-header-${MY_PV}.jar
	doins extensions/guacamole-auth-json/target/guacamole-auth-json-${MY_PV}.jar
	doins extensions/guacamole-auth-quickconnect/target/guacamole-auth-quickconnect-${MY_PV}.jar
	if use radius; then
		doins extensions/guacamole-auth-radius/target/guacamole-auth-radius-${MY_PV}.jar
	fi
	if use sso; then
		doins extensions/guacamole-auth-sso/target/guacamole-auth-sso-${MY_PV}.jar
	fi
	if use totp; then
		doins extensions/guacamole-auth-totp/target/guacamole-auth-totp-${MY_PV}.jar
	fi
	doins extensions/guacamole-history-recording-storage/target/guacamole-history-recording-storage-${MY_PV}.jar

	if use mysql || use postgres; then
		insinto "${GUACAMOLE_HOME}/extensions"
		doins extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/target/guacamole-auth-jdbc-base-${MY_PV}.jar
	fi

	if use mysql; then
		insinto "${GUACAMOLE_HOME}/extensions"
		doins extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/target/guacamole-auth-jdbc-mysql-${MY_PV}.jar
		insinto "${GUACAMOLE_HOME}/lib"
		doins "${WORKDIR}"/mysql-connector-j-8.3.0/mysql-connector-j-8.3.0.jar

		insinto "/usr/share/${PN}/schema/mysql"
		find "${S}/extensions/${MY_PN}-auth-jdbc/modules/${MY_PN}-auth-jdbc-mysql/schema/" -name '*.sql' -exec doins '{}' +

		elog "Please add a mysql database and a user and load the sql files in /usr/share/${PN}/schema/ into it."
		elog "If this is an update, then you will need to apply the appropriate update script in the location above."
		elog "You will also need to adjust the DB properties in ${GUACAMOLE_HOME}/guacamole.properties!"
		elog "The default user and it's password is \"guacadmin\"."
		elog "-"
	fi

	if use postgres; then
		insinto "${GUACAMOLE_HOME}/extensions"
		doins extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-postgresql/target/guacamole-auth-jdbc-postgresql-${MY_PV}.jar
		insinto "${GUACAMOLE_HOME}/lib"
		doins "${DISTDIR}"/postgresql-42.7.3.jar

		insinto "/usr/share/${PN}/schema/postgres"
		find "${S}/extensions/${MY_PN}-auth-jdbc/modules/${MY_PN}-auth-jdbc-postgresql/schema/" \
			-name '*.sql' -exec doins '{}' +

		elog "Please add a postgresql database and a user and load the sql files in /usr/share/${PN}/schema/ into it."
		elog "If this is an update, then you will need to apply the appropriate update script in the location above."
		elog "You will also need to adjust the DB properties in ${GUACAMOLE_HOME}/guacamole.properties!"
		elog "The default user and it's password is \"guacadmin\"."
		elog "-"
	fi

	if use ldap; then
		insinto "${GUACAMOLE_HOME}/extensions"
		doins extensions/guacamole-auth-ldap/target/guacamole-auth-ldap-${MY_PV}.jar

		insinto "/usr/share/${PN}/schema"
		doins "${S}/extensions/${MY_PN}-auth-ldap/schema/guacConfigGroup.ldif" \
			"${S}/extensions/${MY_PN}-auth-ldap/schema/guacConfigGroup.schema"

		elog "You will need to add and load the .schema file in /usr/share/${PN}/schema/ to your ldap server."
		elog "You will also need to adjust the DB properties in ${GUACAMOLE_HOME}/guacamole.properties!"
		elog "There is also an example .lidf file for creating the users."
		elog "-"
	fi

	insinto "${GUACAMOLE_HOME}"
	doins "${FILESDIR}"/guacamole.properties
	doins "${S}/${MY_PN}/doc/example/user-mapping.xml"
	#doins "${S}/${MY_PN}/src/main/resources/logback.xml"
	keepdir ${CLASSPATH}

	echo "GUACAMOLE_HOME=${GUACAMOLE_HOME}" >98guacamole
	doenvd 98guacamole

	insinto "/usr/share/${PN}"
	newins "${S}/${MY_PN}/target/${MY_PN}-${MY_PV}.war" "${MY_PN}.war"

}

pkg_postinst() {
	local tomcat_instance="tomcat-9-${MY_PN}"

	elog "Install net-misc/guacamole-server on the host providing the Guacamole proxy."
	elog "Adjust ${EPREFIX}${GUACAMOLE_HOME}/guacamole.properties and load the database schema."
	elog
	elog "Create a Tomcat 9 instance using its existing tomcat user and group:"
	elog "  ${EPREFIX}/usr/share/tomcat-9/gentoo/tomcat-instance-manager.bash --create --suffix ${MY_PN}"
	elog "For an existing instance, stop it and remove the old exploded webapp before deploying the update."
	elog "Deploy the WAR installed by this package:"
	elog "  ln -sfn ${EPREFIX}/usr/share/${PN}/${MY_PN}.war ${EPREFIX}/var/lib/${tomcat_instance}/webapps/${MY_PN}.war"
	elog
	elog "With OpenRC, add export GUACAMOLE_HOME=${EPREFIX}${GUACAMOLE_HOME} to ${EPREFIX}/etc/conf.d/${tomcat_instance}."
	elog "With systemd, set Environment=GUACAMOLE_HOME=${EPREFIX}${GUACAMOLE_HOME}"
	elog "in the Tomcat service's [Service] section."
	elog "Configure www-servers/apache or www-servers/nginx as the reverse proxy and adjust ${EPREFIX}/etc/${tomcat_instance}/server.xml."
	elog "See https://guacamole.apache.org/doc/gug/configuring-guacamole.html"
	elog "    https://guacamole.apache.org/doc/gug/jdbc-auth.html"
	elog "    https://guacamole.apache.org/doc/gug/reverse-proxy.html"
}