# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
#
# WATCH: https://github.com/luben/zstd-jni/issues/260
pkgname=java-zstd-jni
_jarname=zstd-jni
pkgver=1.5.5.6
_pkgver=${pkgver%.*}-${pkgver##*.}
pkgrel=0
pkgdesc="JNI binding for Zstd"
url="https://github.com/luben/zstd-jni"
# armhf,armv7,s390x,x86: sbt fails
# ppc64le: 44/146 tests fail
# riscv64: blocked by java-jdk
# loongarch64: blocked by java-jdk
arch="all !armhf !armv7 !ppc64le !riscv64 !s390x !x86 !loongarch64"
license="BSD-2-Clause"
depends="java-jre-headless"
# NOTE: It fails to build with OpenJDK 21.
makedepends="
	bash
	openjdk17-jdk
	"
subpackages="$pkgname-native"
source="https://github.com/luben/zstd-jni/archive/v$_pkgver/zstd-jni-$_pkgver.tar.gz
	gcc-flags.patch
	java-8.patch
	dont-bundle-native-lib.patch
	"
builddir="$srcdir/$_jarname-$_pkgver"
# net: fetches dependencies from Maven repos
options="net"

# java.security.manager - Allow running sbt with OpenJDK >16
#   https://github.com/sbt/sbt/issues/6558
export SBT_OPTS="-ivy $srcdir/.ivy2 -sbt-dir $srcdir/.sbt -sbt-boot $srcdir/.sbt/boot -Djava.security.manager=allow"

prepare() {
	default_prepare

	HOME="$PWD/.home" ./sbt update
}

build() {
	./sbt compile package
}

check() {
	./sbt test -Djava.library.path="$PWD/target/native/bin"
}

package() {
	depends="$depends $pkgname-native=$pkgver-r$pkgrel"

	# NOTE: zstd-jni expects file named libzstd-jni-x.y.so, not libzstd-jni.so.x.y.
	install -D -m755 target/native/bin/lib$_jarname-$_pkgver.so -t "$pkgdir"/usr/lib/
	ln -s lib$_jarname-$_pkgver.so "$pkgdir"/usr/lib/lib$_jarname.so

	install -D -m644 target/$_jarname-$_pkgver.jar -t "$pkgdir"/usr/share/java/
	ln -s $_jarname-$_pkgver.jar "$pkgdir"/usr/share/java/$_jarname.jar
}

native() {
	amove usr/lib
}

sha512sums="
cf6e25f9cff3d59245ac3da66c3b78f1d500c4d8e53fee9dd32608c4119a11a27babbdc895ff25231654bef9b6acf1cfff0251e59cb2684fc5c42d407a432020  zstd-jni-1.5.5-6.tar.gz
905a725325bd90a564811ffbf085f215cd2935d6a272dce591568517b587cc460637540dfeeba727458566cb7aeaf028a2a88ff438ec2053a27bf2032fcf3db2  gcc-flags.patch
99a1423c6e370eb26048258b2569b4a2c59f29f9dc44bfe3c5234d0d2bbce588c0d541b6624e50edf27bc3062a0ce986422f2d402e305088ce0b8c3c47b9c084  java-8.patch
26a6752f7d91b44e01525fd139ec56da093e5bd9b23a1de8ea096790a632af2ede6be85ca9283af6af125dd026734e7ab46590834be80e80d09352081db1c46f  dont-bundle-native-lib.patch
"
