#!/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:
	# 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:

override_dh_auto_install:
