#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
export SHELL := /bin/bash
.SHELLFLAGS := -c
export DH_VERBOSE = 1
export DH_OPTIONS = -v

OS_NAME := $(shell cat /etc/os-release | grep '^ID=' | cut -d'=' -f2 | sed -e 's@"@@g')
OS_VER  := $(shell cat /etc/os-release | grep '^VERSION_ID=' | cut -d'=' -f2 | sed -e 's@"@@g')

export PHPVERSION=56
export altdir = /opt/alt/php56

# see FEATURE AREAS in dpkg-buildflags(1)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
# export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CPPFLAGS_MAINT_STRIP = -Wformat -Werror=format-security
export DEB_CFLAGS_MAINT_STRIP = -Wformat -Werror=format-security
# DPKG_EXPORT_BUILDFLAGS = 1
# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
# export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# package maintainers tm append LDFLAGS
LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)
ifeq ($(shell [[ "$(OS_NAME)" == "ubuntu" && $$(expr $(OS_VER) \<= 20.04) == 1 ]] || [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \<= 10) == 1 ]] || [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \>= 13) == 1 ]] && echo 0), 0)
CPPFLAGS += -I/opt/alt/pcre/usr/include
LDFLAGS := -L/opt/alt/pcre/usr/lib/$(DEB_HOST_MULTIARCH)
endif
# Suppress warnings for old extensions on Debian 13+ (GCC is stricter)
ifeq ($(shell [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \>= 13) == 1 ]] && echo 0), 0)
export CFLAGS += -Wno-incompatible-pointer-types -Wno-error=implicit-function-declaration -Wno-int-conversion
endif
export LDFLAGS
export CPPFLAGS
export PATH := $(PATH):$(altdir)/usr/bin:${PATH}

# list all modules
export MODS := $(shell ls -d */ | sed -e "/debian/d")

buildroot := $(CURDIR)/debian/tmp
_sourcedir := debian/src
_prefix := /usr
_libdir := $(_prefix)/lib/$(DEB_HOST_MULTIARCH)
_sysconfdir := /etc

NJOBS := -j$(shell getconf _NPROCESSORS_ONLN)

%:
	dh $@

override_dh_auto_configure:
	for i in $(shell ls -1 $(_sourcedir)); do \
		tar xzf $(_sourcedir)/$${i}; \
	done

	patch -p1 < debian/patches/xrange-1.3.2_tests.patch
	pushd quickhash-*; patch -p1 < ../debian/patches/quickhash-1.0.0-symbol-not-found.patch; popd

	# Create postinst scripts
	for i in $(shell grep 'Package' debian/control | awk '{print $$2}'); do \
		if [ ! -f debian/$${i}.postinst ]; then \
			echo "#!/bin/bash" > debian/$${i}.postinst; \
			if [ "${PHP_VER}" == "-internal" ]; then \
				echo "chmod -Rc og-rwx /opt/alt/php-internal" >> debian/$${i}.postinst; \
			else \
				echo "[ -x /usr/bin/cl-linksafe-apply-group ] && /usr/bin/cl-linksafe-apply-group $${i} || :" >> debian/$${i}.postinst; \
			fi; \
		fi; \
	done

override_dh_auto_build:
	for i in $(MODS); do \
		config_opts=""; \
		extra_includes=""; \
		extra_ldflags=""; \
		mod=$$(echo $${i} | sed -e "s#-[0-9][/0-9.a-zA-Z-]*##"); \
		echo "mod: $${mod}"; \
		if [ "$${mod}" = "newrelic-php5" ]; then \
			mod="newrelic"; \
		fi; \
		modname=$$(echo $${mod} | tr [A-Z] [a-z]); \
		if [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \>= 13) == 1 ]] && [[ "$${modname}" == "rsync" ]]; then \
			echo "Skipping $${modname} on Debian 13+ (incompatible bundled librsync)"; \
			continue; \
		fi; \
		pushd $${i}; \
		case "$${modname}" in \
		esac; \
		if [[ "$${modname}" == "mongo" ]] && [[ "$(OS_NAME)" == "ubuntu" && "$(OS_VER)" == "16.04" ]] && [[ -d /opt/alt/openssl ]]; then \
			echo "Building mongo with alt-openssl on Ubuntu 16.04"; \
			extra_includes="$${extra_includes} -I/opt/alt/openssl/include"; \
			extra_ldflags="-L/opt/alt/openssl/lib/$(DEB_HOST_MULTIARCH) -Wl,-rpath=/opt/alt/openssl/lib/$(DEB_HOST_MULTIARCH)"; \
		fi; \
		$(altdir)/usr/bin/phpize; \
		CPPFLAGS="$$CPPFLAGS $${extra_includes}" LDFLAGS="$$LDFLAGS $${extra_ldflags}" ./configure --libdir=$(altdir)$(_libdir) \
					--with-php-config=$(altdir)/usr/bin/php-config \
					--with-libdir=lib/$(DEB_HOST_MULTIARCH) $${config_opts}; \
		make ${NJOBS} || exit 1; \
		popd; \
	done

override_dh_auto_install:
	mkdir -p $(buildroot)$(altdir)
	install -d $(buildroot)$(altdir)$(_sysconfdir)/php.d.all

	for i in $(MODS); do \
		mod=$$(echo $${i} | sed -e "s#-[0-9][/0-9.a-zA-Z-]*##"); \
		if [ "$${mod}" = "newrelic-php5" ]; then \
			mod="newrelic"; \
		fi; \
		modname=$$(echo $${mod} | tr [A-Z] [a-z]); \
		if [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \>= 13) == 1 ]] && [[ "$${modname}" == "rsync" ]]; then \
			echo "Skipping install of $${modname} on Debian 13+ (not built)"; \
			continue; \
		fi; \
		pushd $${i}; \
		make install INSTALL_ROOT=$(buildroot); \
		if [ "$${modname}" = "mongo" ]; then \
			echo -e "; Enable $${modname} extension module\nextension=json.so\nextension=$${modname}.so" > $(buildroot)$(altdir)$(_sysconfdir)/php.d.all/$${modname}.ini; \
		elif [ "$${modname}" = "libevent" ]; then \
			echo -e "; Enable $${modname} extension module\nextension=sockets.so\nextension=$${modname}.so" > $(buildroot)$(altdir)$(_sysconfdir)/php.d.all/$${modname}.ini; \
		elif [ "$${modname}" = "zendopcache" ]; then \
			echo -e "; Enable opcache extension module\nzend_extension=$(altdir)/usr/lib/$(DEB_HOST_MULTIARCH)/php/modules/opcache.so" > $(buildroot)$(altdir)$(_sysconfdir)/php.d.all/opcache.ini; \
		else \
			echo -e "; Enable $${modname} extension module\nextension=$${modname}.so" > $(buildroot)$(altdir)$(_sysconfdir)/php.d.all/$${modname}.ini; \
		fi; \
		popd; \
	done

	dh_movefiles -palt-php56-pecl-ext \
		.$(altdir)/${_prefix}/* \
		.$(altdir)$(_sysconfdir)/php.d.all/*.ini
