# Contributor: Bart Ribbers <bribbers@disroot.org>
# Maintainer: Bart Ribbers <bribbers@disroot.org>
pkgname=olm
pkgver=3.2.16
pkgrel=1
pkgdesc="Implementation of the olm and megolm cryptographic ratchets"
arch="all"
url="https://gitlab.matrix.org/matrix-org/olm"
license="Apache-2.0"
makedepends="
	cmake
	py3-cffi
	py3-setuptools
	python3-dev
	samurai
	"
checkdepends="
	py3-pytest
	py3-pytest-benchmark
	py3-pytest-cov
	py3-future
	py3-aspectlib
	"
source="https://gitlab.matrix.org/matrix-org/olm/-/archive/$pkgver/olm-$pkgver.tar.gz"
subpackages="$pkgname-dev py3-olm-pyc py3-olm:py3"

build() {
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib
	cmake --build build

	cd python
	python3 setup.py build
}

check() {
	ctest --test-dir build/tests --output-on-failure

	cd "$builddir"/python
	local _pyarch
	case "$CARCH" in
		x86_64|aarch64|ppc64le) _pyarch="$CARCH" ;;
		x86) _pyarch=i686 ;;
		armhf|armv7) _pyarch=armv8l ;;
		s390x) _pyarch=s390x;;
	esac

	local _py3ver=$(python3 -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))')

	# TODO: fails to find _libolm.abi3.so
	# PYTHONPATH="$PWD/build/lib.linux-$_pyarch-$_py3ver" pytest
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	DESTDIR="$pkgdir" make -C "$builddir"/python install-python3
}

py3() {
	depends="
		py3-cffi
		py3-future
		python3
		"
	amove usr/lib/python3*
}

sha512sums="
a75dbaa4321baf53dab820599ddb8740e5982a4537dd2f1df2de2d7bb5c9500d0d1af25f08495e37cc50d3176646d8d6f78bb24bc827f3c246242f07db90eea8  olm-3.2.16.tar.gz
"
