#!/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-pecl-swoole_$(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//")
export CFLAGS := $(CPPFLAGS)
export CXXFLAGS := $(CPPFLAGS)
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 pcre.h not found on Debian 13 (uses alt-pcre)
CPPFLAGS += -I/opt/alt/pcre/usr/include
LDFLAGS += -L/opt/alt/pcre/usr/lib -Wl,-rpath=/opt/alt/pcre/usr/lib

phpXY := $(altdir)/usr/bin/php
use_pcre1 = $(shell grep -q 'include "pcre.h"' $(altdir)/usr/include/php/ext/pcre/php_pcre.h && echo 1 || echo 0)
use_pcre2 = $(shell grep -q 'include "pcre2.h"' $(altdir)/usr/include/php/ext/pcre/php_pcre.h && echo 1 || echo 0)
ldd_alt_pcre := $(shell ldd $(phpXY) | grep -q "alt/pcre/" && 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 := $(shell ldd $(phpXY) | grep -q "alt/pcre2/" && echo 1 || echo 0)

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)

buildroot := $(CURDIR)/debian/tmp
_sourcedir := debian/src
_sysconfdir := $(altdir)/etc
_libdir := $(altdir)/usr/lib/$(DEB_HOST_MULTIARCH)
NJOBS := -j$(shell getconf _NPROCESSORS_ONLN)

%:
	dh $@
override_dh_auto_configure:
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)* .

	patch -p1 < debian/patches/swoole-tests-dns-timeout.patch
	# Skip faulty tests (-f for tests which are not present in all versions)
	rm -f tests/swoole_process_pool/export_socket.phpt
	rm tests/swoole_coroutine/bug_2387.phpt
	rm tests/swoole_coroutine/private_access.phpt
	rm -f tests/swoole_client_coro/dtls.phpt
	rm -f tests/swoole_client_coro/unsock_dgram.phpt

	# Fails only in test systen
	rm -f tests/swoole_process_pool/create_websocket_server.phpt
	rm -f tests/swoole_process_pool/enable_coroutine.phpt
	rm -f tests/swoole_process_pool/enable_coroutine2.phpt
	rm -f tests/swoole_process_pool/start_pool_twice_in_same_process.phpt
	rm -f tests/swoole_runtime/sockets/basic/mcast_ipv6_recv.phpt
	rm tests/swoole_socket_coro/setopt/multicast.phpt
	rm tests/swoole_client_coro/connect_with_dns.phpt
	rm -f tests/swoole_redis_coro/setOptions.phpt
	rm -f tests/swoole_socket_coro/recv_bad_packet.phpt
	rm tests/swoole_socket_coro/recvAll_timeout.phpt

	# online test, but it isn't skipped
	rm tests/swoole_coroutine/pdo_error_handing.phpt
	rm tests/swoole_runtime/block.phpt
	rm tests/swoole_runtime/pdo.phpt

	# Uses different error codes
	rm -f tests/swoole_redis_coro/connect_to_wrong.phpt
	rm -f tests/swoole_redis_coro/different_connect.phpt

	# Crashes sometimes in build system
	rm tests/swoole_runtime/file_hook/read.phpt

	# Fails on some newer OS due to security limitations
	rm -f tests/swoole_redis_coro/auth.phpt

	# Random failures
	rm tests/swoole_runtime/stream_select/bug46024.phpt
	rm -f tests/swoole_runtime/file_hook/lock_nb_2.phpt
	rm -f tests/swoole_runtime/destruct.phpt
	rm -f tests/swoole_runtime/sleep.phpt
	rm -f tests/swoole_runtime/stream_select/bug60602.phpt
	rm -f tests/swoole_redis_coro/auth.php
	rm -f tests/swoole_redis_coro/unconnected.phpt
	rm -f tests/swoole_redis_coro/auto_reconnect.phpt
	rm -f tests/swoole_redis_coro/donot_retry_after_server_down.phpt
	rm -f tests/swoole_redis_coro/connect_twice-2.phpt
	rm tests/swoole_client_coro/connect_timeout.phpt
	rm tests/swoole_client_coro/udp_client.phpt
	rm -f /tests/swoole_client_coro/length_protocol_func.phpt
	rm -f tests/swoole_function/substr_unserialize.phpt
	rm -f tests/swoole_socket_coro/protocol/bug_3586.phpt
	rm -f tests/swoole_socket_coro/closed.phpt
	rm -f tests/swoole_socket_coro/peek_and_checkLiveness.phpt
	rm -f tests/swoole_coroutine/bailout/error.phpt
	rm -f tests/swoole_coroutine/bailout/co_redis_in_shutdown_function.phpt
	rm -f tests/swoole_coroutine/bailout/error_in.phpt
	rm -f tests/swoole_coroutine/bailout/error_out.phpt
	rm -f tests/swoole_coroutine/bailout/error_internal.phpt
	rm -f tests/swoole_coroutine/bailout/error_internal2.phpt
	rm -f tests/swoole_coroutine/bailout/co_redis_in_shutdown_function.phpt
	rm -f tests/swoole_process_pool/master_pid.phpt
	rm -f tests/swoole_process_pool/message_bus_sync.phpt
	rm -f tests/swoole_coroutine/swoole_async_dns_lookup_coro.phpt
	rm -f tests/swoole_runtime/ssl_server.phpt
	rm -f tests/swoole_table/bug_2290.phpt
	rm -f tests/swoole_client_coro/length_protocol_func.phpt
	rm -f tests/swoole_runtime/persistent.phpt
	rm -f tests/swoole_socket_coro/accept.phpt
	rm -f tests/swoole_socket_coro/recv_timeout.phpt
	rm -f tests/swoole_socket_coro/setopt/ipv6_pktinfo.phpt
	rm -f tests/swoole_runtime/stream_select/base.phpt
	rm -f tests/swoole_runtime/stream_select/bug64770.phpt
	rm -f tests/swoole_runtime/stream_select/blocked.phpt
	rm -f tests/swoole_runtime/sockets/tcp_client.phpt
	rm -f tests/swoole_process_pool/getprocess_1.phpt
	rm -f tests/swoole_process_pool/getprocess_2.phpt
	rm -f tests/swoole_redis_coro/subscribe_2.phpt
	rm -f tests/swoole_socket_coro/writeVector_ssl_eagain.phpt
	rm -f tests/swoole_socket_coro/import_5.phpt
	rm -f tests/swoole_client_coro/enableSSL.phpt
	rm -f tests/swoole_runtime/stream_select/bug53427.phpt
	rm -f tests/swoole_process_pool/msgqueue.phpt
	rm -f tests/swoole_runtime/ssl_server.phpt
	rm -f tests/swoole_process_pool/reuse_port.phpt
	rm -f tests/swoole_process_pool/getprocess_3.phpt
	rm -f tests/swoole_socket_coro/readv_eagain.phpt
	rm -f tests/swoole_coroutine/getElasped.phpt
	rm -f tests/swoole_process_pool/msgqueue_2.phpt
	rm -f tests/swoole_process_pool/message_bus.phpt
	rm -f tests/swoole_socket_coro/reuse_2.phpt
	rm -f tests/swoole_process_pool/bug_2652.phpt



	# fails
	rm tests/swoole_client_coro/connect_dns_timeout.phpt
	# Swoole\Coroutine\Redis::__construct() is deprecated
	rm -f tests/swoole_redis_coro/zpop.phpt
	rm -f tests/swoole_runtime/file_hook/iouring.phpt
	rm -f tests/swoole_process_pool/master_callback.phpt

	# dynamic property warnings
	rm tests/swoole_runtime/sockets/basic/socket_set_option_rcvtimeo.phpt
	rm tests/swoole_runtime/sockets/basic/socket_set_option_sndtimeo.phpt
	rm tests/swoole_runtime/sockets/basic/socket_setopt_basic.phpt
	rm tests/swoole_runtime/sockets/basic/socket_set_block-retval.phpt
	rm tests/swoole_runtime/sockets/basic/socket_set_nonblock-retval.phpt
	# Swoole\Coroutine\Redis::__construct() is deprecated

	# use alt-php in tests
	if [[ -e tests/swoole_runtime/stream_select/stream_set_blocking.phpt ]]; then \
        sed -i "s@proc_open('php '@proc_open('$(phpXY) '@g" tests/swoole_runtime/stream_select/stream_set_blocking.phpt; fi

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

	echo ";Enable swoole extension module" > swoole.ini
	echo "extension=swoole.so" >> swoole.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 swoole.ini $(buildroot)$(_sysconfdir)/php.d.all/swoole.ini
	install -d $(buildroot)$(testdir)/tests
	install -d $(buildroot)$(testdir)/examples
	install -d $(buildroot)$(testdir)/tools
	cp -r tests/* $(buildroot)$(testdir)/tests/
	cp -r examples/* $(buildroot)$(testdir)/examples/
	cp -r tools/* $(buildroot)$(testdir)/tools/

	dh_movefiles -palt-php82-pecl-swoole \
		.$(_sysconfdir)/php.d.all/swoole.ini \
		.$(_libdir)/php/modules/swoole.so

	dh_movefiles -palt-php82-pecl-swoole-tests \
		.$(testdir)

	dh_movefiles -palt-php82-pecl-swoole-dev \
		.$(altdir)/usr/include

override_dh_auto_test:

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	rm -rf .cache
