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

_prefix := /usr
altdir := /opt/alt/php84
tideways_testdir := /opt/alt/tests/alt-php84-pecl-xhprof-tideways_$(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//")
export CFLAGS := $(CPPFLAGS)
export CXXFLAGS := $(CPPFLAGS)
LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)
export PATH := $(altdir)/usr/bin:${PATH}

CPPFLAGS += -Wno-error=array-bounds

CFLAGS += -I$(altdir)/usr/include/php/ext
export EXTRA_LDFLAGS=$(LDFLAGS)
export EXTRA_CFLAGS=$(CPPFLAGS)

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)

%:
	dh $@


override_dh_auto_build:
	patch -p1 < debian/patches/tideways-5.0.4-tests-compat-82.patch
	patch -p1 < debian/patches/tideways-5.0.4-tests-compat-84.patch
	patch -p1 < debian/patches/php-84-support.patch
	$(altdir)/usr/bin/phpize
	./configure --libdir=$(_libdir) \
				--with-php-config=$(altdir)/usr/bin/php-config \
				--enable-tideways-xhprof \
				--with-libdir=$(DEB_HOST_MULTIARCH)
	make $(NJOBS)



override_dh_auto_install:
	mkdir -p $(buildroot)$(altdir)
	install -d $(buildroot)$(_sysconfdir)/php.d.all
	make install INSTALL_ROOT=$(buildroot)
	echo -e ";Enable tideways_xhprof extension module\nextension=tideways_xhprof.so" > $(buildroot)$(_sysconfdir)/php.d.all/tideways_xhprof.ini
	install -d $(buildroot)$(tideways_testdir)/tests
	cp -r tests/* $(buildroot)$(tideways_testdir)/tests/


	dh_movefiles -palt-php84-pecl-xhprof-tideways \
		.$(_sysconfdir)/php.d.all/tideways_xhprof.ini \
		.$(php_extdir)/tideways_xhprof.so

	dh_movefiles -palt-php84-pecl-xhprof-tideways-tests \
		.$(tideways_testdir)



override_dh_auto_test:

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	rm -rf .cache
