# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=neo4j
pkgver=4.4.35
pkgrel=0
pkgdesc="Neo4j Community Edition LTS"
url="https://neo4j.com"
# other platforms are not supported by upstream
arch="aarch64 x86_64"
license="GPL-3.0-or-later"
# Neo4j 4.x requires JVM 11.
depends="
	java-jna
	java-netty-transport-native
	java-zstd-jni
	openjdk11-jre-headless
	"
_depends_shell="
	java-jna
	java-netty-transport-native
	java-jre-headless
	"
makedepends="
	$_depends_shell
	openjdk11
	maven
	unzip
	zip
	"
checkdepends="bash"
install="$pkgname.pre-install"
pkgusers="neo4j"
pkggroups="neo4j"
subpackages="
	$pkgname-doc
	$pkgname-openrc
	cypher-shell:_shell:noarch
	"
source="https://github.com/neo4j/neo4j/archive/refs/tags/$pkgver/neo4j-$pkgver.tar.gz
	system-jars.patch
	conf.patch
	wrapper.sh
	$pkgname.initd
	$pkgname.confd
	cypher-shell
	"
# net: fetches packages from Maven repos
# check: needs ulimit -n 40000
options="net !check"

# Ensure Neo4j is built with JDK11 if multiple JDKs are installed
# to prevent this error: https://github.com/neo4j/neo4j/issues/12933
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk"
export PATH="$JAVA_HOME/bin:$PATH"

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

prepare() {
	default_prepare

	# See system-jars.patch.
	local jna_ver zstdjni_ver
	jna_ver=$(_jar_version /usr/share/java/jna.jar)
	zstdjni_ver=$(_jar_version /usr/share/java/zstd-jni.jar)
	sed -i -e 's/\${alpine.jna.version}/'"$jna_ver/" \
		-e 's/\${alpine.zstd-jni.version}/'"$zstdjni_ver/"  \
		pom.xml

	# Fix version number (upstream doesn't bump it before tagging).
	mvn versions:set -DnewVersion="$pkgver" -DgenerateBackupPoms=false
}

build() {
	mvn install -DskipTests

	mkdir -p dist
	tar -C dist -xf packaging/standalone/target/neo4j-community-*-unix.tar.gz

	cd dist/neo4j-community-*

	# Remove jansi native binaries (they are actually not needed).
	zip -qd bin/tools/cypher-shell.jar 'org/fusesource/jansi/internal/native/*'
}

check() {
	mvn test
}

package() {
	cd dist/neo4j-community-*

	install -D -m644 lib/*.jar -t "$pkgdir"/usr/share/neo4j/lib/
	install -D -m755 bin/neo4j bin/neo4j-admin -t "$pkgdir"/usr/share/neo4j/bin/
	install -D -m644 LICENSE* NOTICE.txt -t "$pkgdir"/usr/share/licenses/$pkgname/
	install -D -m644 labs/README.txt -t "$pkgdir"/var/lib/neo4j/labs/
	install -D -m644 plugins/README.txt -t "$pkgdir"/var/lib/neo4j/plugins/

	install -D -m644 conf/neo4j.conf -t "$pkgdir"/etc/neo4j/
	install -d -m750 -g neo4j "$pkgdir"/etc/neo4j/certs

	install -D -m755 "$srcdir"/wrapper.sh "$pkgdir"/usr/bin/neo4j
	ln -s neo4j "$pkgdir"/usr/bin/neo4j-admin

	install -D -m755 "$srcdir"/neo4j.initd "$pkgdir"/etc/init.d/neo4j
	install -D -m644 "$srcdir"/neo4j.confd "$pkgdir"/etc/conf.d/neo4j

	# cypher-shell
	install -D -m644 bin/tools/cypher-shell.jar -t "$pkgdir"/usr/share/cypher-shell/
	install -D -m755 "$srcdir"/cypher-shell -t "$pkgdir"/usr/bin/

	cd "$pkgdir"

	install -d -o neo4j -g neo4j -m750 \
		var/lib/neo4j/data \
		var/lib/neo4j/data/databases \
		var/lib/neo4j/data/dbms \
		var/lib/neo4j/data/dumps \
		var/lib/neo4j/data/transactions \
		var/lib/neo4j/import \
		var/log/neo4j

	cd "$pkgdir"/usr/share/neo4j/lib

	# See system-jars.patch.
	ln -s /usr/share/java/zstd-jni.jar .
	ln -s /usr/share/java/jna.jar .

	msg "Checking JARs for native libraries..."
	local f; for f in *.jar; do
		if jar tf "$f" | grep -q '\.so$'; then
			error "Found native library in $f"
			return 1
		fi
	done
}

_shell() {
	pkgdesc="Cypher Shell - CLI for Neo4j"
	depends="$_depends_shell"

	amove usr/bin/cypher-shell
	amove usr/share/cypher-shell
}

_jar_version() {
	unzip -p "$1" META-INF/MANIFEST.MF | sed -En 's/Implementation-Version: (\S+).*/\1/p'
}

sha512sums="
d3bfd3bfe45b05964d5b1652116d15ce328954a04bdf3cc0687f5a850e1c318e7b4c6d0a8b815ded51c79c9eb8a0501572de6381a44022542a125f5f5aad8fe0  neo4j-4.4.35.tar.gz
1967594563adf1aa1c0c9809b0cc9fbefe042c933e556f5cb6d01f6ce0d40efcc054533c784d4e12b8fc5ebdd169fd276f2caeb8921eece634fa918219199706  system-jars.patch
6810dfe781aa46882dab971f97aefa8a22e68ed6956754bcb824c28839c7229469316ebe6695819a4bb6b701bea18848d467bd9863bf6dc2a2c01b3f55406202  conf.patch
a1b6f509228dd9925420591803595c67653924ea592c38894f7d3d26e24b40bbc9b35e8b4e67611df9e06a7f0921b13e3f2f1bb54102049c3b1f9c6a2656939d  wrapper.sh
f61a76246039187dcc61bdfd7482e4928ae7b356571e56c86f7b7a243dced79c8d61f0c6ab807ca7f993a78640cda427316a9b274844c794b769b992ff897498  neo4j.initd
8530390bb5292318f03b76d3afe23a3029971f90ce4b0e702d18913c01000c6df8644d6c7415ab6dde2d02f9a4af490234ce817aa2d6976554a971e1ff707d4f  neo4j.confd
45a3b4f9db522eca6dbc616b17005f7ec75a1168242f465dfd6d2475be3cba465573920d34e4fd192d5f0c838e049f4ea6d46d9a56193f4079a8850f4090dfeb  cypher-shell
"
