#!/usr/bin/make -f
export SHELL := /bin/bash
export DH_VERBOSE = 1
export DH_OPTIONS = -v

altdir := /opt/alt/php85
testdir := /opt/alt/tests/alt-php85-pecl-mongodb_$(shell dpkg-parsechangelog | grep Version: | cut -d' ' -f2-)

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
CPPFLAGS = $(shell dpkg-buildflags --get CFLAGS | sed -e "s/-Wformat//;s/-Werror=format-security//")
LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)
export PATH := $(altdir)/usr/bin:${PATH}



buildroot := $(CURDIR)/debian/tmp
_sourcedir := debian/src
_sysconfdir := $(altdir)/etc
_libdir := $(altdir)/usr/lib/$(DEB_HOST_MULTIARCH)
php_extdir := $(_libdir)/php/modules
NJOBS := -j$(shell getconf _NPROCESSORS_ONLN)

phpXY := $(altdir)/usr/bin/php
ldd_alt_pcre := $(shell ldd $(phpXY) | grep -q "alt/pcre/" && echo 1 || echo 0)
ldd_alt_pcre2 := $(shell ldd $(phpXY) | grep -q "alt/pcre2/" && echo 1 || echo 0)
# some PHPs are linked statically
ldd_no_pcre := $(shell ldd $(phpXY) | grep -qv "pcre" && echo 1 || echo 0)

# use alt-pcre for these static linked PHPs
use_alt_pcre1 = $(shell [[ $(ldd_alt_pcre) -eq 1 ]] || [[ $(ldd_no_pcre) ]] && echo 1 || echo 0)
use_alt_pcre2 := $(ldd_alt_pcre2)

ifeq ($(use_alt_pcre), 1)
LIBPCRE:= /opt/alt/pcre/usr/lib/$(DEB_HOST_MULTIARCH)
CPPFLAGS += -I/opt/alt/pcre/usr/include
LDFLAGS += -L$(LIBPCRE) -Wl,-rpath=$(LIBPCRE)
endif

ifeq ($(use_alt_pcre2), 1)
LIBPCRE2:= /opt/alt/pcre2/usr/lib/$(DEB_HOST_MULTIARCH)
CPPFLAGS += -I/opt/alt/pcre2/usr/include
LDFLAGS += -L$(LIBPCRE2) -Wl,-rpath=$(LIBPCRE2)
endif

export EXTRA_LDFLAGS=$(LDFLAGS)
export EXTRA_CFLAGS=$(CPPFLAGS)
export CFLAGS := $(CPPFLAGS)
export CXXFLAGS := $(CPPFLAGS)

HAS_JSON := 1

%:
	dh $@

override_dh_auto_build:
	# dpkg-source 3.0 (quilt) doesn't extract the upstream PECL tgz into the
	# build dir — the .tgz files in cwd are committed copies, not unpacked
	# sources. Extract the one matching the active version explicitly, drop
	# the package.xml metadata, and flatten the resulting mongodb-X.Y.Z/
	# subdirectory so phpize finds config.m4 in cwd.
	set -ex; \
	PKGVER=$$(dpkg-parsechangelog -S Version | cut -d- -f1); \
	tar xzf "mongodb-$${PKGVER}.tgz"; \
	rm -f package.xml; \
	mv "mongodb-$${PKGVER}"/* . ; \
	rmdir "mongodb-$${PKGVER}"


	$(altdir)/usr/bin/phpize
	./configure --libdir=$(_libdir) \
				--with-php-config=$(altdir)/usr/bin/php-config \
				--with-libdir=$(DEB_HOST_MULTIARCH)
	make $(NJOBS)

	echo ";Enable mongodb extension module" > mongodb.ini
ifeq ($(HAS_JSON),0)
	echo "extension=json.so" >> mongodb.ini
endif
	echo "extension=$(php_extdir)/mongodb.so" >> mongodb.ini

	# tests are placed in directories in tarball, move them to a flat structure
	# tests/utils should go one level higher
	mv tests/utils .
	for dir in tests/*; do mv $$dir/* tests/; rmdir $$dir; done

	# garbages console
	rm -f tests/bson-binary-clone-*.phpt
	rm -f tests/bson-binary-serialization-*.phpt
	rm -f tests/bson-binary-set_state*.phpt
	rm -f tests/bson-document-fromBSON_error-*.phpt
	rm -f tests/bson-toPHP_error-004.phpt
	rm -f tests/bson-toPHP_error-005.phpt
	rm -f tests/bulkwrite-delete_error-002.phpt
	rm -f tests/bulkwrite-insert_error-*.phpt
	rm -f tests/bulkwrite-update_error-*.phpt
	rm -f tests/bson-binary-jsonserialize-001.phpt
	# fails
	rm -f tests/bson-int64-operation-002.phpt
	rm -f tests/bug0851-002.phpt
	rm -f tests/manager-ctor-directconnection-error-002.phpt
	rm -f tests/manager-ctor-srvMaxHosts_error-001.phpt
	rm -f tests/manager-ctor-tls-error-001.phpt
	rm -f tests/manager-getencryptedfieldsmap-001.phpt
	rm -f tests/manager-ctor-tls-error-002.phpt
	# fails without running server
	rm -f tests/manager-ctor-server.phpt

override_dh_auto_install:
	mkdir -p $(buildroot)$(altdir)
	install -d $(buildroot)$(_sysconfdir)/php.d.all
	make install INSTALL_ROOT=$(buildroot)
	install -D -m 644 mongodb.ini $(buildroot)$(_sysconfdir)/php.d.all/mongodb.ini

	install -d $(buildroot)$(testdir)/tests
	cp tests/* $(buildroot)$(testdir)/tests/
	install -d $(buildroot)$(testdir)/utils
	cp utils/* $(buildroot)$(testdir)/utils/

	dh_movefiles -palt-php85-pecl-mongodb \
		.$(_sysconfdir)/php.d.all/mongodb.ini \
		.$(php_extdir)/mongodb.so

	dh_movefiles -palt-php85-pecl-mongodb-tests \
		.$(testdir)

override_dh_auto_test:

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	rm -rf .cache
