# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=keycloak
pkgver=24.0.6
pkgrel=0
pkgdesc="Open source identity and access management for modern applications and services"
url="https://www.keycloak.org"
# armhf,armv7,ppc64le,riscv64,x86: blocked by openjdk17
# s390x: blocked by java-netty-transport-native
# loongarch64: blocked by maven
arch="noarch !armhf !armv7 !ppc64le !riscv64 !s390x !x86 !loongarch64"
license="Apache-2.0"
depends="
	java-jre-headless
	java-netty-transport-native
	"
makedepends="
	bash
	maven
	nodejs
	npm
	java-jdk
	"
pkgusers="keycloak"
pkggroups="keycloak"
install="$pkgname.pre-install"
subpackages="$pkgname-openrc"
source="https://github.com/keycloak/keycloak/archive/$pkgver/keycloak-$pkgver.tar.gz
	support-for-readonly-usr-share.patch
	remove-deps.patch
	keycloak.conf
	$pkgname.initd
	$pkgname.confd
	$pkgname.logrotated
	"
# net: fetches dependencies from Maven repositories
options="!check net"

# NOTE: Maven 3 dropped support for the M2_HOME variable.
export MAVEN_ARGS="--batch-mode -Duser.home=$srcdir/.home"

prepare() {
	default_prepare

	# Workaround for frontend-maven-plugin to use system-provided node and npm.
	# https://github.com/eirslett/frontend-maven-plugin/issues/1089
	mkdir -p node
	cp -r /usr/lib/node_modules node/
	ln -s /usr/bin/node node/
	ln -s /usr/bin/npm node/
	ln -s /usr/bin/npx node/
	ln -s ../node js/
}

build() {
	# NOTE: -Dskip.installnodenpm doesn't work, frontend-maven-plugin always
	#  downloads prebuilt node and npm, unless it's already present in the
	#  ./node directory and the version matches.
	local mvn_opts="-DskipTests -Dnode.version=$(node --version)"

	cd quarkus
	mvn -f ../pom.xml install $mvn_opts -DskipExamples -DskipTestsuite
	mvn install $mvn_opts
	tar -C dist -xf dist/target/keycloak-*.tar.gz
}

package() {
	cd quarkus/dist/keycloak-*

	local destdir="$pkgdir/usr/share/$pkgname"
	local builddir="$(pwd)"

	mkdir -p "$destdir"
	cp -r bin lib providers themes version.txt "$destdir"/
	rm -f "$destdir"/bin/*.bat
	rm -f "$destdir"/bin/*.orig

	cd "$pkgdir"

	mkdir -p usr/bin
	local i; for i in kc kcadm kcreg; do
		ln -s ../share/$pkgname/bin/$i.sh usr/bin/$i
	done

	install -d -m755 -o keycloak -g keycloak \
		var/lib/$pkgname
	install -d -m750 -o keycloak -g keycloak \
		var/lib/$pkgname/data \
		var/log/$pkgname

	mv "$destdir"/lib/quarkus "$destdir"/lib/quarkus.dist
	ln -s /var/lib/$pkgname/lib/quarkus "$destdir"/lib/quarkus

	install -d -m750 -o root -g keycloak etc/$pkgname
	install -D -m640 -o keycloak -g keycloak -t etc/keycloak/ \
		"$builddir"/conf/cache-ispn.xml \
		"$srcdir"/keycloak.conf

	install -D -m755 "$srcdir"/$pkgname.initd etc/init.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.confd etc/conf.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.logrotated etc/logrotate.d/$pkgname
}

sha512sums="
e669b4e76be95cfde5b060458b0904e597541dc4bac022e87ec1dfa9bb760e79095a05da628b2cec08525a21f99b05f3ee21aefe9bbe871d391d178c203c7ea3  keycloak-24.0.6.tar.gz
50c8bd0395e36d9be94225e3f4cf63d9c64bf063c954af2584962f66c96b90a648ff0d6d65b080b81157294cc456cb7418e6c0f197ea05a0b09c5382479b27e5  support-for-readonly-usr-share.patch
934998230c109a8fbf84b6967c93203f45d0d6d1fdcaf2b7302a57d70143aa1394deb20f898347a21dc2e4df95f189758baf94f8cacd527576ee2d19917ed761  remove-deps.patch
2fc1a25796fa2077a253b72e8f9900ab33cb4ab652f8c1ecc8450c79a6efb6398a6000adc57a45148abd9497a766f811cf7a12997956fbb3e1c76b8e6f2bdb7e  keycloak.conf
86ef13bf7f4383bb969b0c4d9f5118c4278ad370675da37deb7e6fa056da5d1e5e03782378ca7aad0e51d11ce4ef7b0ab53bf5f6973fcc49a48ddb8d328a28a7  keycloak.initd
1a763c430ab1b614ee96f0c80634143baf5ae00b5b7d76096ad3230792982ffafc91148be90dc3b5c8672cb05df8d636f5003445a7138b5fe97428113a08b3b3  keycloak.confd
5fe5972a71c630e7335f6a73b10d7258dcbb0448f3a9dab2aac94fe9a4905c0cbb5ebbc4c74113696296e19a207fd019c72430ff53feab7255edce91ed89fd11  keycloak.logrotated
"
