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

altdir := /opt/alt/php56
testdir := /opt/alt/tests/alt-php56-pecl-apcu_$(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)

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

# Fix pcre.h not found on Debian 13 for PHP 5.6 (uses alt-pcre)
CPPFLAGS += -I/opt/alt/pcre/usr/include
LDFLAGS += -L/opt/alt/pcre/usr/lib -Wl,-rpath=/opt/alt/pcre/usr/lib

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)* .
	rm tests/apc54_014.phpt
	rm tests/apc54_018.phpt
	rm tests/ghbug176.phpt
	rm tests/bug63224.phpt


	$(altdir)/usr/bin/phpize
	./configure --libdir=$(_libdir) \
				--with-php-config=$(altdir)/usr/bin/php-config \
				--enable-apc-bc \
				--enable-apcu \
				--with-libdir=$(DEB_HOST_MULTIARCH)
	make $(NJOBS)
	echo ";Enable apcu extension module" > apcu.ini
	echo "extension=$(php_extdir)/apcu.so" >> apcu.ini


override_dh_auto_install:
	mkdir -p $(buildroot)$(altdir)
	install -d $(buildroot)$(_sysconfdir)/php.d.all
	make install INSTALL_ROOT=$(buildroot)
	install -D -m 644 apcu.ini $(buildroot)$(_sysconfdir)/php.d.all/apcu.ini
	install -d $(buildroot)$(testdir)/tests
	cp -r tests/* $(buildroot)$(testdir)/tests/
	dh_movefiles -palt-php56-pecl-apcu \
		.$(_sysconfdir)/php.d.all/apcu.ini \
		.$(php_extdir)/apcu.so
	dh_movefiles -palt-php56-pecl-apcu-tests \
		.$(testdir)
	# Manually install header files for -dev package (make install doesn't do this for PECL on Debian 13)
	install -d $(buildroot)$(altdir)/usr/include/php/ext/apcu
	cp -f *.h $(buildroot)$(altdir)/usr/include/php/ext/apcu/ 2>/dev/null || true
	dh_movefiles -palt-php56-pecl-apcu-dev \
		.$(altdir)/usr/include/php/ext/*


override_dh_auto_test:

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	rm -rf .cache
