# Contributor: Russ Webber <russ@rw.id.au>
# Maintainer: Russ Webber <russ@rw.id.au>
pkgname=mongo-cxx-driver
pkgver=3.8.0
pkgrel=0
pkgdesc="C++ driver for MongoDB"
url="http://mongocxx.org/"
arch="all"
license="Apache-2.0"
makedepends="
	cmake
	libbson-dev
	mongo-c-driver-dev
	mongo-c-driver-static
	openssl-dev
	python3
	samurai
	snappy-dev
	zstd-dev
	"
subpackages="$pkgname-dev:_dev libbsoncxx libbsoncxx-dev"
source="mongo-cxx-driver-$pkgver.tar.gz::https://github.com/mongodb/mongo-cxx-driver/archive/r$pkgver.tar.gz
	no-mongo-tests.patch
	"
builddir="$srcdir/mongo-cxx-driver-r$pkgver"

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=True \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_CXX_STANDARD=17 \
		-DBSONCXX_POLY_USE_STD=ON \
		-DBUILD_VERSION="$pkgver" \
		$CMAKE_CROSSOPTS
	cmake --build build
}

check() {
	# mongo tests require a running server, so they are disabled.
	# libbsoncxx tests are still ran
	ctest --test-dir build
}

package() {
	# abuild can't trace this
	depends="libbsoncxx=$pkgver-r$pkgrel"
	DESTDIR="$pkgdir" cmake --install build
	rm -rf "$pkgdir"/usr/share/mongo-cxx-driver/
}

libbsoncxx() {
	pkgdesc="Building, parsing, and iterating BSON documents using C++"

	amove usr/lib/libbsoncxx.so.*
}

_dev() {
	# This should be removed once abuild knows how to search for pkg-config
	# files that are in another subpackage that is also being created alongside
	# this one.
	depends_dev="libbsoncxx-dev=$pkgver-r$pkgrel"

	default_dev

	# libbsoncxx pkg-config files
	mkdir -p "$pkgdir"/usr/lib/pkgconfig
	mv "$subpkgdir"/usr/lib/pkgconfig/libbsoncxx.pc "$pkgdir"/usr/lib/pkgconfig

	# libbsoncxx solink
	mkdir -p "$pkgdir"/usr/lib
	mv "$subpkgdir"/usr/lib/libbsoncxx.so "$pkgdir"/usr/lib

	# libbsoncxx cmake file
	mkdir -p "$pkgdir"/usr/lib/cmake
	mv "$subpkgdir"/usr/lib/cmake/libbsoncxx-* "$pkgdir"/usr/lib/cmake

	# libbsoncxx include files
	mkdir -p "$pkgdir"/usr/include
	mv "$subpkgdir"/usr/include/bsoncxx "$pkgdir"/usr/include
}

sha512sums="
f9f0d83ee2ef1f3c2dae1f3663b893d819737d4e646d0137fb623b84663382eb93ab36123c2a85247f43ae979a95276fa231d5da8ad62a6da002ab9daecaa0d4  mongo-cxx-driver-3.8.0.tar.gz
18eaace179782c8e59d585def40da2cb941c767484f2739a6714dc1526c62cd8595bfe89545ddfa32e781a618a0f73a53ac3ee9ecdeb86c7b3250bd380b4d276  no-mongo-tests.patch
"
