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

altdir := /opt/alt/php53
testdir := /opt/alt/tests/alt-php53-pecl-imagick_$(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

# ImageMagick 7 on Debian 13+ requires explicit include path
MAGICK_CFLAGS := $(shell pkg-config --cflags MagickWand)
CPPFLAGS += $(MAGICK_CFLAGS)
# 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

buildroot := $(CURDIR)/debian/tmp
_sourcedir := debian/src
_sysconfdir := $(altdir)/etc
_libdir := $(altdir)/usr/lib/$(DEB_HOST_MULTIARCH)
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

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

# Fix pcre.h not found on Debian 13 for PHP 5.6 (uses alt-pcre)
# C_INCLUDE_PATH is searched for #include "file" before -I paths
export C_INCLUDE_PATH := /opt/alt/pcre/usr/include
export CPLUS_INCLUDE_PATH := /opt/alt/pcre/usr/include

%:
	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)* .

	# ALTEAPLN-46: refresh 024-ispixelsimilar.phpt for ImageMagick >= 7.1.2-0.
	# debian/source/format is 3.0 (quilt) but there is no series file, so
	# nothing is applied automatically -- apply it here, as alt.spec does with
	# Patch3. Applies to all three lineups (the test is byte-identical in
	# 3.4.4, 3.7.0 and 3.8.1).
	patch -p1 --fuzz=0 < debian/patches/imagick-024-ispixelsimilar-im712-fuzz.patch

	# Fails in test system
	rm tests/014-setresourcelimit.phpt

	# php_stream_locate_url_wrapper() takes "char **path_for_open" up to PHP 5.5
	# and "const char **" from 5.6 on, while imagick passes a const char ** in
	# both 3.4.4 and 3.7.0. On Debian 13 GCC makes that mismatch an error rather
	# than a warning, so php53/54/55 fail to build. The mismatch is const-only --
	# same representation -- so cast at the single call site. Gated below 56 on
	# purpose: adding the cast on php56+ reintroduces the error there.
	# (ALTPHP-2397)
	sed -i 's|php_stream_locate_url_wrapper(filename, &path_for_open,|php_stream_locate_url_wrapper(filename, (char **) \&path_for_open,|' imagick_file.c

	# Calibrated against an older ImageMagick than deb13 ships (7.1.2): the 3.7.0
	# test expects kurtosis -0.93792610350105 but gets -0.7092599567492. alt.spec
	# removes this test unconditionally ("Fails on ImageMagick 7.1.1-39"); mirror
	# that here, gated below 56 so php56+ keeps the test it currently passes
	# (3.8.1 updated the expected values). Absent from 3.4.4, hence rm -f.
	# (ALTPHP-2397)
	rm -f tests/316_Imagick_getImageKurtosis.phpt

	# imagick 3.4.4 returns the format verbatim from ImageMagick ("PNG") while the
	# test expects "png"; 3.7.0+ normalise it, so only the php52/53 lineup fails.
	# (ALTPHP-2397)
	rm -f tests/009_properties.phpt

	# 3.4.4 predates ImageMagick 7, and setImageAlpha's pixel values differ under
	# deb13's IM 7.1.2 ("Pixel values appear incorrect for pixelType 3").
	rm -f tests/274_imagick_setImageAlpha.phpt

	# These three use PHP 5.4+ short-array syntax, a parse error on PHP 5.3.
	# Confirmed by linting all 276 packaged tests of 3.4.4 against PHP 5.3.29:
	# these are the only three that fail to parse. 275 is currently masked by its
	# SKIPIF but cannot run on php53 either, so it goes too.
	rm -f tests/275_ImagickPixel_unitializedObjects.phpt
	rm -f tests/279_ImagickDraw_setTextInterlineSpacing.phpt
	rm -f tests/280_imagickkernel_exception_invalid_origin.phpt

	#warn: XFAIL section but test passes)
	rm tests/031_Imagick_affineTransformImage_basic.phpt
	rm tests/268_ImagickDraw_getDensity_basic.phpt
	rm tests/bug_73840.phpt



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

	echo ";Enable imagick extension module" > imagick.ini
	echo "extension=imagick.so" >> imagick.ini

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

	dh_movefiles -palt-php53-pecl-imagick \
		.$(_sysconfdir)/php.d.all/imagick.ini \
		.$(_sysconfdir)/php.d.std/imagick.ini \
		.$(_libdir)/php/modules/imagick.so

	dh_movefiles -palt-php53-pecl-imagick-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/imagick
	cp -f *.h $(buildroot)$(altdir)/usr/include/php/ext/imagick/ 2>/dev/null || true
	dh_movefiles -palt-php53-pecl-imagick-dev \
		.$(altdir)/usr/include

override_dh_auto_test:

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	rm -rf .cache
