# Maintainer: Leonardo Arena <rnalrd@alpinelinux.org>

# FIXME: This is totally wrong, fix it before moving into upper repository!
test -e ../libbson/APKBUILD && . ../libbson/APKBUILD && libbson_ver=$pkgver || return 1
test -e ../mongo-c-driver/APKBUILD && . ../mongo-c-driver/APKBUILD && mongocdrv_ver=$pkgver || return 1

_php=php7
pkgname=mongo-${_php}-driver
_realname=mongo-php-driver
pkgver=1.3.2
pkgrel=0
pkgdesc="Low-level PHP driver for MongoDB"
url="https://github.com/mongodb/mongo-php-driver"
arch="all"
license="Apache-2.0"
makedepends="autoconf automake libressl-dev pkgconfig
	mongo-c-driver pcre-dev ${_php}-dev bash"
options="!check" #fails, is attended
subpackages=""
install=""
source="$pkgname-$pkgver.tar.gz::https://github.com/mongodb/$_realname/archive/$pkgver.tar.gz
	libbson-${libbson_ver}.tar.gz::https://github.com/mongodb/libbson/archive/${libbson_ver}.tar.gz
	mongo-c-driver-${mongocdrv_ver}.tar.gz::https://github.com/mongodb/mongo-c-driver/archive/${mongocdrv_ver}.tar.gz
	mongodb.ini
	"

builddir="$srcdir"/$_realname-$pkgver

unpack() {
	local u
	if [ -z "$force" ]; then
		verify
		initdcheck
	fi
	mkdir -p "$srcdir"
	for u in $source; do
		local s="$SRCDEST/$(filename_from_uri $u)"
		echo $s
		case "$s" in
			*$pkgname-$pkgver.*)
				msg "Unpacking $s..."
				tar xzf "$s" -C "$srcdir" || return 1;;
			*libbson*)
				msg "Unpacking $s..."
				tar xzf "$s" --strip 1 -C "$srcdir"/$_realname-$pkgver/src/libbson || return 1;;
			*mongo-c-driver*)
				msg "Unpacking $s..."
				tar xzf "$s" --strip 1 -C "$srcdir"/$_realname-$pkgver/src/libmongoc || return 1;;
		esac
	done
}

prepare() {
	cd "$builddir"
	phpize
}

build() {
	cd "$builddir"
	./configure --prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info
	make
}

package() {
	cd "$builddir"
	make INSTALL_ROOT="$pkgdir" EXTENSION_DIR="/usr/lib/$_php/modules" \
		install
	install -D -m644 "$srcdir"/mongodb.ini \
		"$pkgdir"/etc/${_php}/conf.d/mongodb.ini
}

check() {
	cd "$builddir"
	make test
}

sha512sums="37bbe416714fc9a3c587ce7c21c7223de48f9fde3be4c93e9d9d975dec51fe7486e090da3e797cfac07bdf34e274e7eda53a4e8f4bbea6eebf329141544d05ab  mongo-php7-driver-1.3.2.tar.gz
afe0dcb007546360fa4226403c7a036055e4bfaf58eafbd41b91f9fa3769203514ddde85c6cebec1d7ef5418f9207a6a1caada6f8269854dc65fdff862b3ceb0  libbson-1.8.0.tar.gz
8e46c695abf72186a2bc220a837580bdbb33f3bef6a618b84c4e47053317415228b95751d2b306f799d46dea0c0eaeee2536c0d0e854ba23be35996061a0f26a  mongo-c-driver-1.8.0.tar.gz
acc09667ce1ceb1b5854a2d80f635be6ca7539df5a59043ff80e96759150e1715e5d16f8e97612ec43b504e50d08cda71da416b91d5d4da54f8284da1eefa2f3  mongodb.ini"
