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

altdir := /opt/alt/php72
testdir := /opt/alt/tests/alt-php72-pecl-stomp_$(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
CPPFLAGS += -Wno-incompatible-pointer-types -Wno-int-conversion

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)

extension_dir := $(CURDIR)/.libs

phpXY := $(altdir)/usr/bin/php

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


%:
	dh $@

override_dh_auto_build:
	mv $(shell ls -trd -- */ | head -n 1)* .
	for f in $$(find tests -name '*.phpt'); do \
		if grep -q '^--INI--' "$$f"; then \
			sed -i '/^--INI--/a stomp.default_broker=tcp://127.0.0.1:61613' "$$f"; \
		else \
			sed -i 's/^--FILE--/--INI--\nstomp.default_broker=tcp:\/\/127.0.0.1:61613\n--FILE--/' "$$f"; \
		fi; \
	done
	# Tests added in stomp 2.x that the CoilMQ smoke broker cannot satisfy:
	# 003-connect/003 expects an auth error on CONNECT (CoilMQ accepts any login).
	rm tests/003-connect/003.phpt
	# The 009-readFrame family is timing-sensitive against CoilMQ's async
	# delivery and flakes on any platform (different subsets per run);
	# 009-readFrame/006 additionally expects a pure MESSAGE frame stack.
	rm -rf tests/009-readFrame
	$(altdir)/usr/bin/phpize
	./configure --libdir=$(_libdir) \
				--with-php-config=$(altdir)/usr/bin/php-config \
				--with-libdir=$(DEB_HOST_MULTIARCH) \
				--enable-stomp
	make $(NJOBS)
	echo ";Enable stomp extension module" > stomp.ini
	echo "extension=stomp.so" >> stomp.ini
	# Legacy platform pip builds an incomplete coilmq wheel (missing subpackages)
	# under PEP 517 build isolation - upgrade pip and force a modern setuptools.
	# Pin coilmq to a known-good commit for reproducible builds.
	/opt/alt/python-internal/bin/python3 -m venv .venv
	. .venv/bin/activate && \
	pip install --upgrade pip "setuptools>=75.3.4" "setuptools-scm>=8" && \
	pip install --no-build-isolation git+https://github.com/hozn/coilmq.git@98d16b230 && \
	sed -i "s/        self.headers.setdefault('content-length', len(self.body))/        if self.body:\n            self.headers.setdefault('content-length', len(self.body))/" \
	    $$(find .venv -path '*/coilmq/util/frames.py') && \
	sed -i 's/from coilmq.protocol import STOMP11/from coilmq.protocol import STOMP10/;s/protocol=STOMP11/protocol=STOMP10/' \
	    $$(find .venv -path '*/coilmq/start.py') && \
	find .venv -name '__pycache__' -exec rm -rf {} + 2>/dev/null; true

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 stomp.ini $(buildroot)$(_sysconfdir)/php.d.all/stomp.ini
	install -D -m 644 stomp.ini $(buildroot)$(_sysconfdir)/php.d.std/stomp.ini
	install -d $(buildroot)$(testdir)/tests
	if [ -d tests ]; then cp -r tests/* $(buildroot)$(testdir)/tests/; fi
	install -d $(buildroot)$(testdir)/tools
	cp -a .venv $(buildroot)$(testdir)/tools/.venv
	for f in $(buildroot)$(testdir)/tools/.venv/bin/*; do \
	    if [ -L "$$f" ]; then continue; fi; \
	    sed -i '1s|^#!.*\.venv.*|#!/opt/alt/python-internal/bin/python3|' "$$f"; \
	done
	install -d $(buildroot)$(testdir)/scripts
	install -m 755 $(_sourcedir)/prepare $(buildroot)$(testdir)/scripts/prepare
	install -m 755 $(_sourcedir)/teardown $(buildroot)$(testdir)/scripts/teardown
	dh_movefiles -palt-php72-pecl-stomp \
		.$(_sysconfdir)/php.d.all/stomp.ini \
		.$(_sysconfdir)/php.d.std/stomp.ini \
		.$(php_extdir)/stomp.so
	dh_movefiles -palt-php72-pecl-stomp-tests \
		.$(testdir)

override_dh_auto_test:
	$(altdir)/usr/bin/php -d 'extension_dir=$(extension_dir)' -d 'extension=stomp.so' -m | grep ^Stomp$$
	. .venv/bin/activate; \
	coilmq -b 0.0.0.0 -p 61613 & \
	COILMQ_PID=$$!; \
	for attempt in $$(seq 1 20); do \
	    if python3 -c "import socket; s=socket.socket(); s.settimeout(0.5); s.connect(('127.0.0.1', 61613)); s.close()" 2>/dev/null; then break; fi; \
	    sleep 0.25; \
	done; \
	TEST_PHP_ARGS="-d stomp.default_broker=tcp://127.0.0.1:61613" NO_INTERACTION=1 REPORT_EXIT_STATUS=1 make test; TEST_RESULT=$$?; \
	kill $$COILMQ_PID 2>/dev/null || true; \
	wait $$COILMQ_PID 2>/dev/null || true; \
	exit $$TEST_RESULT

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	rm -rf .cache
