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

altdir := /opt/alt/php55
testdir := /opt/alt/tests/alt-php55-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}

CPPFLAGS += -Wno-error=array-bounds

# Fix bundled libmongoc/libbson include paths (ALTPHP-2305)
CPPFLAGS += -I./src -I./src/contrib -I./src/libmongoc/src/libbson/src -I./src/libmongoc/src/common -I./src/libmongoc/src/libmongoc/src
# PHP < 7.4 uses alt-pcre (PCRE1), add include and lib paths
CPPFLAGS += -I/opt/alt/pcre/usr/include
LDFLAGS += -L/opt/alt/pcre/usr/lib/$(DEB_HOST_MULTIARCH) -Wl,-rpath=/opt/alt/pcre/usr/lib/$(DEB_HOST_MULTIARCH)

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 := $(shell $(altdir)/usr/bin/php -m | grep -q json && echo 1 || echo 0)

%:
	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.
	# ALTPHP-2397: the 1.2.11 tarball contains only mongodb-1.2.11/ with no
	# root-level package.xml, so dpkg-source treats that as the source root and
	# strips it -- the sources are already flattened and there is nothing to
	# move. 1.5.5 and newer ship package.xml alongside the directory, so
	# dpkg-source keeps both and the flatten is still required. Handle either.
	set -ex; \
	PKGVER=$$(dpkg-parsechangelog -S Version | cut -d- -f1); \
	rm -f package.xml; \
	if [ -d "mongodb-$${PKGVER}" ]; then \
	    mv "mongodb-$${PKGVER}"/* . ; \
	    rmdir "mongodb-$${PKGVER}"; \
	fi

	export SED=/usr/bin/sed

	$(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

	# ALTPHP-2397: alt-php-pecl-mongodb.spec carries a php_version-le-55 block
	# that the deb side never got -- the 1.2.x/1.5.x lineups predate a lot of
	# this test suite and these cases do not hold on PHP 5.x. Mirrored here.
	# (Do not write the macro keyword itself in a comment: dbpp parses this file
	# and treats it as a statement, which fails only for the versions that enter
	# the enclosing block.)
	# The ten bson-binary-*/bson-document-*/bson-toPHP_error-*/bulkwrite-*_error-*
	# patterns in that block are already removed unconditionally above, so only
	# the remainder is repeated.
	# garbages console
	rm -f tests/bson-binary-compare-001.phpt
	rm -f tests/bson-binary-compare-002.phpt
	rm -f tests/bson-binary-001.phpt
	rm -f tests/bson-binary-get_properties-002.phpt
	# fails
	rm -f tests/bug0572.phpt
	rm -f tests/bug0720.phpt
	rm -f tests/bug1045.phpt
	rm -f tests/bug1151-*.phpt
	rm -f tests/bug1152-*.phpt
	rm -f tests/causal-consistency-*.phpt
	rm -f tests/cursor-session-*.phpt
	rm -f tests/manager-executeBulkWrite_error-010.phpt
	rm -f tests/manager-executeCommand_error-005.phpt
	rm -f tests/manager-executeReadWriteCommand_error-002.phpt
	rm -f tests/manager-executeWriteCommand_error-004.phpt
	rm -f tests/manager-set-uri-options-002.phpt
	rm -f tests/retryable-writes-*.phpt
	# the spec also lists ten individual session-* cases; this glob covers them
	rm -f tests/session-*.phpt
	rm -f tests/standalone-ssl-no_verify-*.phpt
	rm -f tests/standalone-ssl-verify_cert-*.phpt
	rm -f tests/standalone-x509-auth-*.phpt
	rm -f tests/transaction-integration-*.phpt
	rm -f tests/transaction-integration_error-*.phpt
	rm -f tests/bson-toJSON-001.phpt

override_dh_auto_install:
	mkdir -p $(buildroot)$(altdir)
	install -d $(buildroot)$(_sysconfdir)/php.d.all
	install -d $(buildroot)$(_sysconfdir)/php.d.std
	make install INSTALL_ROOT=$(buildroot)
	install -D -m 644 mongodb.ini $(buildroot)$(_sysconfdir)/php.d.all/mongodb.ini
	install -D -m 644 mongodb.ini $(buildroot)$(_sysconfdir)/php.d.std/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-php55-pecl-mongodb \
		.$(_sysconfdir)/php.d.all/mongodb.ini \
		.$(_sysconfdir)/php.d.std/mongodb.ini \
		.$(php_extdir)/mongodb.so

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

override_dh_auto_test:

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	rm -rf .cache
