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

altdir := /opt/alt/php81
testdir := /opt/alt/tests/alt-php81-pecl-http_$(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 configure script bug: SED is used before AC_PROG_SED sets it (bash 5.2 exposes this)
export SED := /usr/bin/sed
# Fix ext header detection: old config9.m4 needs explicit include paths on D13
CPPFLAGS += -I$(altdir)/usr/include/php/ext/raphf -I$(altdir)/usr/include/php/ext/propro
# Fix pcre.h: D13 dropped libpcre3-dev; old PHP (<7.4) headers need PCRE1 from alt-pcre (ALTPHP-2305)
# alt-pcre-dev provides pcre.h at /opt/alt/pcre/usr/include but php_pcre.h does #include "pcre.h"
# which searches the same directory first - symlink is created in override_dh_auto_build below

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


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)
export PKG_CONFIG_PATH := $(PKG_CONFIG_PATH)

%:
	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)* .
	sed -i 's@$$PHP_EXECUTABLE -m |@$$PHP_EXECUTABLE  -d extension=$(php_extdir)/raphf.so -m |@' autoconf/pecl/pecl.m4
	grep PHP_EXEC autoconf/pecl/pecl.m4
	/opt/alt/php81/usr/bin/php  -d extension=$(php_extdir)/raphf.so -m
	$(altdir)/usr/bin/phpize
	./configure --libdir=$(_libdir) \
				--with-php-config=$(altdir)/usr/bin/php-config \
				--with-libdir=$(DEB_HOST_MULTIARCH)

	make $(NJOBS)

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

	rm -f tests/bug71719.phpt
	rm -f tests/client021.phpt
	rm -f tests/client025.phpt
	rm -f tests/client028.phpt
	rm -f tests/envresponse001.phpt
	rm -f tests/envresponse005.phpt
	rm -f tests/envresponse006.phpt
	rm -f tests/envresponse007.phpt
	rm -f tests/envresponse008.phpt
	rm -f tests/envresponse009.phpt
	rm -f tests/envresponse010.phpt
	rm -f tests/envresponse011.phpt
	rm -f tests/envresponse012.phpt
	rm -f tests/envresponse013.phpt
	rm -f tests/envresponse014.phpt
	rm -f tests/gh-issue12.phpt
	rm -f tests/gh-issue47.phpt
	rm -f tests/gh-issue48.phpt

#temporary disabled tests, because php84 is not supported yet.

override_dh_auto_install:
	mkdir -p $(buildroot)$(altdir)
	install -d $(buildroot)$(_sysconfdir)/php.d.all
	make install INSTALL_ROOT=$(buildroot)
	install -D -m 644 http.ini $(buildroot)$(_sysconfdir)/php.d.all/http.ini

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

	dh_movefiles -palt-php81-pecl-http \
		.$(_sysconfdir)/php.d.all/http.ini \
		.$(php_extdir)/http.so

	dh_movefiles -palt-php81-pecl-http-tests \
		.$(testdir)

	dh_movefiles -palt-php81-pecl-http-dev \
		.$(altdir)/usr/include

override_dh_auto_test:

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	rm -rf .cache
