#!/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-eio_$(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

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)

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

%:
	dh $@

override_dh_auto_build:
	# PECL tarballs contain package.xml on the same level as source dir (usually called modname-1.1.1),
	# so debhelper doesn't unpack its contents in work dir.
	# We should move files from that dir to current. Source dir is the first created one, debian/ is created later.
	mv $(shell ls -trd -- */ | head -n 1)* .

	# ALTPHP-2201: fix the eio submit/result race and the fork.phpt unlink race
	# (see the patch headers).
	patch -p1 < debian/patches/eio-req-result-race.patch
	patch -p1 < debian/patches/fork-test-unlink-race.patch

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

	echo ";Enable eio extension module" > eio.ini
	echo "extension=$(php_extdir)/sockets.so" >> eio.ini
	echo "extension=$(php_extdir)/eio.so" >> eio.ini

	echo ";Enable eio extension module" > eio.ini.std
	echo "extension=$(php_extdir)/eio.so" >> eio.ini.std



# ALTPHP-2201: eio_grp_add, eio_open_error, eio_seek and fork.phpt used to be
# deleted here on debian13/ubuntu24_04 as a "floating eio environment" problem.
# They were failing because of a real race in the extension (fixed by
# eio-req-result-race.patch) and, for fork.phpt, a bug in the test itself
# (fixed by fork-test-unlink-race.patch), so they run on every platform again.
#
# eio_custom_basic.phpt stays out everywhere: eio_custom() runs the userspace
# PHP function from a libeio worker thread with no synchronisation, so it races
# with the main thread over the output buffer. See the spec for the full note.
	rm -f tests/eio_custom_basic.phpt

# eio_write_variation.phpt uses PHP 5.4+ short-array syntax (mirrors the RPM spec)

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 eio.ini $(buildroot)$(_sysconfdir)/php.d.all/eio.ini
	install -D -m 644 eio.ini.std $(buildroot)$(_sysconfdir)/php.d.std/eio.ini

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

	dh_movefiles -palt-php55-pecl-eio \
		.$(_sysconfdir)/php.d.all/eio.ini \
		.$(_sysconfdir)/php.d.std/eio.ini \
		.$(php_extdir)/eio.so

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

override_dh_auto_test:

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	rm -rf .cache
