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

altdir := /opt/alt/php82
testdir := /opt/alt/tests/alt-php82-snuffleupagus_$(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
php_inidir := $(_sysconfdir)/php.d.all
php_stdinidir := $(_sysconfdir)/php.d.std
NJOBS := -j$(shell getconf _NPROCESSORS_ONLN)

# snuffleupagus links against PCRE; build it against the same PCRE flavor as
# the PHP it targets. Detect what the alt-php binary is linked to and add the
# matching include/lib paths.
phpXY := $(altdir)/usr/bin/php
# Detect the alt-pcre flavor by header presence rather than `ldd` of the php
# binary: on some debian13/ubuntu targets ldd did not report the alt/pcre link
# for the php7.0-7.3 line, so the include path was dropped and the build failed
# with "pcre.h: No such file or directory". The header only exists for the
# flavor the targeted PHP actually uses (alt-pcre for <=7.3, alt-pcre2 for >=8.0).
ifneq (,$(wildcard /opt/alt/pcre/usr/include/pcre.h))
LIBPCRE := /opt/alt/pcre/usr/lib/$(DEB_HOST_MULTIARCH)
CPPFLAGS += -I/opt/alt/pcre/usr/include
LDFLAGS += -L$(LIBPCRE) -Wl,-rpath=$(LIBPCRE)
endif

ifneq (,$(wildcard /opt/alt/pcre2/usr/include/pcre2.h))
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)

%:
	dh $@

override_dh_auto_build:
	# The orig tarball has a single top-level dir, so dpkg-source (3.0 quilt)
	# already unpacks the upstream tree flat into the build dir. The PHP
	# extension lives in the src/ subdirectory (config.m4 is there), so build
	# from within src/.
	cd src && $(altdir)/usr/bin/phpize
	cd src && ./configure --libdir=$(_libdir) \
				--with-php-config=$(altdir)/usr/bin/php-config \
				--with-libdir=$(DEB_HOST_MULTIARCH) \
				--enable-snuffleupagus
	# Pre-generate the config scanner from the shipped cached copy.
	# Upstream generates src/sp_config_scanner.c from sp_config_scanner.re at
	# build time via src/Makefile.frag (re2c, or a plain copy of the cached
	# .c when re2c is absent). That fragment is pulled into the Makefile by
	# PHP_ADD_MAKEFILE_FRAGMENT() in config.m4 - but the alt-php deb -dev build
	# system does not emit the fragment rule, so on debian/ubuntu the Makefile
	# has no rule for sp_config_scanner.c and (with no re2c installed) the build
	# dies with "No rule to make target sp_config_scanner.c". Copy the cached
	# scanner ourselves (exactly upstream's no-re2c fallback) so the source
	# exists before make; touch it newer than the .re so make never tries to
	# regenerate it.
	cp src/sp_config_scanner.cached.c src/sp_config_scanner.c
	touch src/sp_config_scanner.c
	cd src && make $(NJOBS)

	# Drop in the bit of configuration (mirrors the RPM prep-stage ini)
	echo "; Enable 'snuffleupagus' extension module" > snuffleupagus.ini
	echo "extension = $(php_extdir)/snuffleupagus.so" >> snuffleupagus.ini
	echo "" >> snuffleupagus.ini
	echo "; Path to rules configuration files, glob or comma separated list" >> snuffleupagus.ini
	echo "sp.configuration_file = '$(php_inidir)/snuffleupagus-*.rules'" >> snuffleupagus.ini
	echo ";sp.allow_broken_configuration = 0" >> snuffleupagus.ini

	echo "# Default rules see https://snuffleupagus.readthedocs.io/config.html" > snuffleupagus-default.rules

override_dh_auto_install:
	mkdir -p $(buildroot)$(altdir)
	install -d $(buildroot)$(php_inidir)
	install -d $(buildroot)$(php_stdinidir)
	cd src && make install INSTALL_ROOT=$(buildroot)
	install -D -m 644 snuffleupagus.ini $(buildroot)$(php_inidir)/snuffleupagus.ini
	install -D -m 644 snuffleupagus.ini $(buildroot)$(php_stdinidir)/snuffleupagus.ini
	install -D -m 644 snuffleupagus-default.rules $(buildroot)$(php_inidir)/snuffleupagus-default.rules
	install -D -m 644 snuffleupagus-default.rules $(buildroot)$(php_stdinidir)/snuffleupagus-default.rules

	dh_movefiles -palt-php82-snuffleupagus \
		.$(php_inidir)/snuffleupagus.ini \
		.$(php_inidir)/snuffleupagus-default.rules \
		.$(php_stdinidir)/snuffleupagus.ini \
		.$(php_stdinidir)/snuffleupagus-default.rules \
		.$(php_extdir)/snuffleupagus.so

override_dh_auto_test:

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	rm -rf .cache
