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

export apiver      20180731
export zendver     20180731
export pdover      20170320

ifdef __imunify
export IMUNIFY = -imunify
else
export IMUNIFY =
endif

export PHP_VER = 73$(IMUNIFY)
export altdir = /opt/alt/php$(PHP_VER)
scl_dir := /opt/cloudlinux/alt-php73
_bindir := $(altdir)/usr/bin
_sbindir := $(altdir)/usr/sbin
buildroot := $(CURDIR)/debian/tmp

litespeed_ver = 8.1

CODENAME ?= $(shell lsb_release -sc)
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 mods = pgsql odbc ldap snmp xmlrpc imap \
 mbstring gd dom xsl soap bcmath dba xmlreader xmlwriter \
 pdo pdo_pgsql pdo_odbc pdo_sqlite json zip \
 phar fileinfo intl pspell wddx mssql \
 sodium tidy enchant recode \
 nd_mysqli nd_pdo_mysql mysqlnd interbase pdo_firebird \
 posix sysvshm sysvsem sysvmsg opcache sockets pdo_dblib

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

LDFLAGS = -L$(altdir)/usr/lib/$(DEB_HOST_MULTIARCH) -Wl,-rpath=$(altdir)/usr/lib/$(DEB_HOST_MULTIARCH)

ifeq ($(shell [[ "$(OS_NAME)" == "ubuntu" && $$(expr $(OS_VER) \<= 18.04) == 1 ]] || [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \<= 10) == 1 ]] && echo 0), 0)
LIBPCRE=-rpath=/opt/alt/pcre2/usr/lib/$(DEB_HOST_MULTIARCH)
export PKG_CONFIG_PATH := ${PKG_CONFIG_PATH}:/opt/alt/pcre2/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
export PATH := ${PATH}:/opt/alt/pcre2/usr/bin
CFLAGS += -I/opt/alt/pcre2/usr/include
LDFLAGS += -L/opt/alt/pcre2/usr/lib/$(DEB_HOST_MULTIARCH) -Wl,$(LIBPCRE)
endif

LIBICU=-rpath=/opt/alt/libicu/usr/lib/$(DEB_HOST_MULTIARCH)
LIBC_CLIENT=-rpath=/opt/alt/libc-client/lib/$(DEB_HOST_MULTIARCH)
LIBCURL=-rpath=/opt/alt/curlssl/usr/lib/$(DEB_HOST_MULTIARCH)
LIBSSL=-rpath=/opt/alt/openssl/lib/$(DEB_HOST_MULTIARCH)

ifeq ($(shell [[ "$(OS_NAME)" == "ubuntu" ]] && echo 0), 0)
  ifeq ($(shell [[ $$(expr $(OS_VER) \<= 20.04) == 1 ]] && echo 0), 0)
LDFLAGS += -L/usr/lib -L/opt/alt/libc-client/lib/$(DEB_HOST_MULTIARCH) -Wl,$(LIBICU),$(LIBC_CLIENT)
PKG_CONFIG_PATH += /opt/alt/libicu/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
  else
LDFLAGS += -L/opt/alt/curlssl/usr/lib/$(DEB_HOST_MULTIARCH) -L/opt/alt/openssl/lib/$(DEB_HOST_MULTIARCH) -L/opt/alt/curlssl/usr/lib -Wl,$(LIBCURL),$(LIBSSL)
export PKG_CONFIG_PATH = /opt/alt/curlssl/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig:/opt/alt/openssl/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
export CC = gcc -DTRUE=1 -DFALSE=0
export CXX = g++ -DTRUE=1 -DFALSE=0
  endif
else
  ifeq ($(shell [[ $$(expr $(OS_VER) \<= 9) == 1 ]] && echo 0), 0)
LDFLAGS += -L/opt/alt/libicu/usr/lib/$(DEB_HOST_MULTIARCH) -Wl,$(LIBICU)
  endif
endif

SED_ALT_LIBS := \
sed -i 's@SQLITE3_SHARED_LIBADD =@SQLITE3_SHARED_LIBADD = -Wl,-rpath,/opt/alt/sqlite/usr/lib/$(DEB_HOST_MULTIARCH) @' Makefile; \
sed -i 's@PDO_SQLITE_SHARED_LIBADD =@PDO_SQLITE_SHARED_LIBADD = -Wl,-rpath,/opt/alt/sqlite/usr/lib/$(DEB_HOST_MULTIARCH) @' Makefile;

ifeq ($(shell [[ "$(OS_NAME)" == "ubuntu" && $$(expr $(OS_VER) \<= 20.04) == 1 ]] || [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \<= 9) == 1 ]] && echo 0), 0)
SED_ALT_LIBS += \
sed -i 's@INTL_SHARED_LIBADD =@INTL_SHARED_LIBADD = -Wl,-rpath,/opt/alt/libicu/usr/lib/$(DEB_HOST_MULTIARCH) @' Makefile;
endif

CFLAGS += -I/usr/include -fno-strict-aliasing -Wno-pointer-sign
ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -O2
else
  CFLAGS += -O0
endif
export CFLAGS
export LDFLAGS

# Install extension modules in %${libdir}/php/modules.
export EXTENSION_DIR=$(altdir)/usr/lib/$(DEB_HOST_MULTIARCH)/php/modules

# Set PEAR_INSTALLDIR to ensure that the hard-coded include_path
# includes the PEAR directory even though pear is packaged
# separately.
export PEAR_INSTALLDIR=$(altdir)/usr/share/pear

# see FEATURE AREAS in dpkg-buildflags(1)
# export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# package maintainers to append LDFLAGS
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

NJOBS := -j$(shell getconf _NPROCESSORS_ONLN)

TARGETS := cgi litespeed fpm mysqlnd

COMMON_CONFIG := \
	   --build=$(DEB_BUILD_GNU_TYPE) \
	   --host=$(DEB_HOST_GNU_TYPE) \
	   --target=$(DEB_BUILD_GNU_TYPE) \
	   --program-prefix= \
	   --prefix=$(altdir) \
	   --exec-prefix=$(altdir) \
	   --bindir=$(altdir)/usr/bin \
	   --sbindir=$(altdir)/usr/sbin \
	   --sysconfdir=$(altdir)/etc \
	   --datadir=$(altdir)/usr/share \
	   --includedir=$(altdir)/usr/include \
	   --libdir=$(altdir)/usr/lib/$(DEB_HOST_MULTIARCH) \
	   --with-libdir=lib/$(DEB_HOST_MULTIARCH) \
	   --libexecdir=$(altdir)/usr/libexec \
	   --localstatedir=/var \
	   --sharedstatedir=/usr/com \
	   --mandir=$(altdir)/usr/share/man \
	   --infodir=$(altdir)usr/share/info \
	   --cache-file=$(CURDIR)/config.cache \
	   --with-config-file-scan-dir=$(altdir)/link/conf \
	   --with-config-file-path=$(altdir)/etc \
	   --disable-debug \
	   --enable-calendar \
	   --enable-exif \
	   --enable-ftp \
	   --enable-gd-native-ttf \
	   --enable-huge-code-pages \
	   --enable-jit \
	   --enable-opcache \
	   --enable-opcache-file \
	   --enable-shmop \
	   --enable-unicode \
	   --enable-xml \
	   --with-bz2 \
	   --with-freetype-dir=/usr \
	   --with-gettext \
	   --with-gmp \
	   --with-iconv \
	   --with-jpeg-dir=/usr \
	   --with-kerberos \
	   --with-layout=GNU \
	   --with-libxml-dir=/usr \
	   --with-mhash \
	   --with-openssl=yes \
	   --with-pcre-jit \
	   --with-pic \
	   --with-png-dir=/usr \
	   --with-readline \
	   --with-webp-dir=/usr \
	   --with-xpm-dir=/usr \
	   --with-zlib \
	   --with-zlib-dir=/ \
	   --without-gdbm \
	   --without-pear
ifeq ($(shell [[ "$(OS_NAME)" == "ubuntu" && $$(expr $(OS_VER) \<= 20.04) == 1 ]] || [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \>= 9) == 1 ]] && echo 0), 0)
COMMON_CONFIG += \
	   --with-curl=/usr \
	   --with-openssl-dir=/usr
else
COMMON_CONFIG += \
	   --with-curl=/opt/alt/curlssl/usr \
	   --with-openssl-dir=/opt/alt/openssl
endif
ifeq ($(shell [[ "$(OS_NAME)" == "ubuntu" && $$(expr $(OS_VER) \<= 18.04) == 1 ]] || [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \<= 10) == 1 ]] && echo 0), 0)
COMMON_CONFIG += \
	   --with-pcre-dir=/opt/alt/pcre2/usr \
	   --with-pcre-regex=/opt/alt/pcre2/usr
else
COMMON_CONFIG += \
	   --with-pcre-dir=/usr \
	   --with-pcre-regex=/usr
endif

export cgi_config = \
	   $(COMMON_CONFIG) \
	   --enable-bcmath=shared \
	   --enable-dba=shared \
	   --enable-dom=shared \
	   --enable-fastcgi \
	   --enable-fileinfo=shared \
	   --enable-force-cgi-redirect \
	   --enable-intl=shared \
	   --enable-json=shared \
	   --enable-mbregex \
	   --enable-mbstring=shared \
	   --enable-opcache \
	   --enable-opcache-file \
	   --enable-pcntl \
	   --enable-pdo=shared \
	   --enable-phar=shared \
	   --enable-posix=shared \
	   --enable-soap=shared \
	   --enable-sockets=shared \
	   --enable-sysvmsg=shared \
	   --enable-sysvsem=shared \
	   --enable-sysvshm=shared \
	   --enable-wddx=shared \
	   --enable-xmlreader=shared --enable-xmlwriter=shared \
	   --enable-zip=shared \
	   --with-enchant=shared,/usr \
	   --with-gd=shared \
	   --with-interbase=shared,/usr \
	   --with-ldap=shared --with-ldap-sasl \
	   --with-pdo-dblib=shared,/usr \
	   --with-pdo-firebird=shared,/usr \
	   --with-pdo-odbc=shared,unixODBC,/usr \
	   --with-pdo-pgsql=shared,/usr \
	   --with-pgsql=shared \
	   --with-pspell=shared \
	   --with-recode=shared,/usr \
	   --with-snmp=shared,/usr \
	   --with-sodium=shared,/usr \
	   --with-tidy=shared,/usr \
	   --with-unixODBC=shared,/usr \
	   --with-xmlrpc=shared \
	   --with-xsl=shared,/usr \
	   --with-sqlite3=/opt/alt/sqlite/usr \
	   --with-pdo-sqlite=shared,/opt/alt/sqlite/usr

ifeq ($(shell [[ "$(OS_NAME)" == "ubuntu" ]] && echo 0), 0)
  ifeq ($(shell [[ $$(expr $(OS_VER) \<= 20.04) == 1 ]] && echo 0), 0)
export cgi_config += \
	   --with-icu-dir=/opt/alt/libicu/usr \
	   --with-imap=shared,/opt/alt/libc-client --with-imap-ssl \
	   --with-libzip=/opt/alt/libzip/usr
  else
export cgi_config += \
	   --with-icu-dir=/usr \
	   --with-imap=shared --with-imap-ssl=/opt/alt/openssl \
	   --with-libzip
  endif
else
  ifeq ($(shell [[ $$(expr $(OS_VER) \<= 9) == 1 ]] && echo 0), 0)
export cgi_config += \
	   --with-icu-dir=/opt/alt/libicu/usr \
	   --with-libzip=/opt/alt/libzip/usr
  else
export cgi_config += \
	   --with-icu-dir=/usr \
	   --with-libzip
  endif
export cgi_config += \
	   --with-imap=shared --with-imap-ssl
endif

export without_shared = \
	   --disable-bcmath \
	   --disable-dba \
	   --disable-dom \
	   --disable-fileinfo \
	   --disable-intl \
	   --disable-json \
	   --disable-mbstring \
	   --disable-pdo \
	   --disable-phar \
	   --disable-posix \
	   --disable-soap \
	   --disable-sockets \
	   --disable-sysvmsg \
	   --disable-sysvsem \
	   --disable-sysvshm \
	   --disable-wddx \
	   --disable-xmlreader \
	   --disable-xmlwriter \
	   --disable-zip \
	   --without-enchant \
	   --without-gd \
	   --without-imap \
	   --without-ldap \
	   --without-mssql \
	   --without-pgsql \
	   --without-pspell \
	   --without-recode \
	   --without-snmp \
	   --without-sodium \
	   --without-tidy \
	   --without-unixODBC \
	   --without-xmlrpc \
	   --without-xsl

export litespeed_config = $(COMMON_CONFIG) \
	   --with-litespeed \
	   --disable-pdo \
	   --enable-pcntl \
	   --without-mysqli \
	   $(without_shared)

export fpm_config = $(COMMON_CONFIG) \
	   --enable-fpm \
	   --disable-pdo \
	   --enable-pcntl \
	   --with-fpm-systemd \
	   --without-mysqli \
	   $(without_shared)

export mysqlnd_config = $(COMMON_CONFIG) \
	   --enable-mysqlnd=shared \
	   --enable-pdo=shared \
	   --with-mysqli=shared,mysqlnd \
	   --with-pdo-mysql=shared,mysqlnd \
	   --with-pdo-pgsql=shared,/usr \
	   --with-pdo-sqlite=shared,/opt/alt/sqlite/usr

%:
	dh $@ --with apache2

prep_build:
ifdef __imunify
	sed -i "s/php73/php$(PHP_VER)/g" debian/control
	mv debian/src/alt-php73-fpm debian/src/alt-php$(PHP_VER)-fpm
	mv debian/src/alt-php73-fpm.service debian/src/alt-php$(PHP_VER)-fpm.service
	for inst in `ls debian | grep install`; do \
	  sed -i "s/php73/php$(PHP_VER)/g" debian/$${inst}; \
	  mv debian/$${inst} debian/`echo $${inst} | sed -r "s/alt-php73-(.*)/alt-php$(PHP_VER)-\1/"`; \
	done
endif
	tar xzf debian/src/php-litespeed-$(litespeed_ver).tgz
	cp litespeed*/*.{h,c} sapi/litespeed
ifeq ($(shell [[ "$(OS_NAME)" == "ubuntu" && $$(expr $(OS_VER) \>= 22.04) == 1 ]] && echo 0), 0)
	patch -p1 < debian/patches/change-autoconf-ver.patch
endif
	./buildconf --force
	patch -p1 < debian/patches/php-7.0-recode.patch
	patch -p1 < debian/patches/cloudlinux-php-fpm.7.3.dl.ea-php.v2.patch
	patch -p1 < debian/patches/php-7.1.x-mail-header.patch
ifeq ($(shell [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \>= 12) == 1 ]] && echo 0), 0)
	patch -p1 < debian/patches/php-7.3.33-fix-for-openssl-3.0.x.patch
endif
	patch -p1 < debian/patches/litespeed-8.0.1-process_lsapi_phpini.patch
	patch -p1 < debian/patches/litespeed-8.0.1-userini_homedir.patch
	patch -p1 < debian/patches/litespeed-8.0.1-log.patch
	patch -p1 < debian/patches/litespeed-8.0.1.use_reject.patch
	patch -p1 < debian/patches/litespeed-8.0.1.avoid_zombies.patch
	patch -p1 < debian/patches/litespeed-8.0.1.dis_keeplistener.patch
	patch -p1 < debian/patches/litespeed-8.0.1.crash_limit.patch
	patch -p1 < debian/patches/litespeed-8.0.1-tsrmls.patch
	patch -p1 < debian/patches/litespeed-8.1-cloudlinux.patch
	patch -p1 < debian/patches/litespeed-8.0.1.wink_fix.patch
	patch -p1 < debian/patches/litespeed-8.0.1-move_override_ini.patch
	patch -p1 < debian/patches/php-7.0.14-bug71465.patch
	patch -p1 < debian/patches/php-7.2.0-curl-mariadb102.patch
	patch -p1 < debian/patches/php-7.x.y-bug70110.patch
	patch -p1 < debian/patches/php-7.2.1-pdo_oci_m4.patch
	patch -p1 < debian/patches/fix_path_imap_lib.patch
ifeq ($(shell [[ "$(OS_NAME)" == "ubuntu" && $$(expr $(OS_VER) \>= 22.04) == 1 ]] || [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \>= 9) == 1 ]] && echo 0), 0)
	patch -p1 < debian/patches/find-libc-client.patch
	patch -p1 < debian/patches/php-7.3-intl-use-icu-namespace.patch
else
	patch -p1 < debian/patches/hardened-bug80747.patch
endif
ifeq ($(shell [[ "$(OS_NAME)" == "ubuntu" && $$(expr $(OS_VER) \>= 22.04) == 1 ]] || [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \>= 11) == 1 ]] && echo 0), 0)
  ifeq ($(shell [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \>= 12) == 1 ]] && echo 0), 0)
	patch -p1 < debian/patches/replace-enchant-with-enchant2_deb12.patch
  else
	patch -p1 < debian/patches/replace-enchant-with-enchant2.patch
  endif
endif
	patch -p1 < debian/patches/php-7.3.6-pcre2.patch
ifeq ($(shell [[ "$(OS_NAME)" == "ubuntu" && $$(expr $(OS_VER) \>= 20.04) == 1 ]] || [[ "$(OS_NAME)" == "debian" && $$(expr $(OS_VER) \>= 10) == 1 ]] && echo 0), 0)
	patch -p1 <debian/patches/0047-Use-pkg-config-for-FreeType2-detection.patch
endif
	patch -p1 < debian/patches/hardened-bug80713.patch
	patch -p1 < debian/patches/hardened-bug80024.patch
	patch -p1 < debian/patches/hardened-bug70091.patch
	patch -p1 < debian/patches/hardened-bug80682.patch
	patch -p1 < debian/patches/hardened-bug75776.patch
	patch -p1 < debian/patches/hardened-bug80880.patch
	patch -p1 < debian/patches/hardened-bug80719.patch
	patch -p1 < debian/patches/hardened-bug80838.patch
	patch -p1 < debian/patches/hardened-bug80763.patch
	patch -p1 < debian/patches/hardened-bug80783.patch
	patch -p1 < debian/patches/hardened-bug80950.patch
	patch -p1 < debian/patches/hardened-bug80889.patch
	patch -p1 < debian/patches/hardened-bug80774.patch
	patch -p1 < debian/patches/hardened-bug79812.patch
	patch -p1 < debian/patches/hardened-bug75850.patch
	patch -p1 < debian/patches/hardened-bug51903.patch
	patch -p1 < debian/patches/hardened-bug78680.patch
	patch -p1 < debian/patches/hardened-bug80757.patch
	patch -p1 < debian/patches/hardened-bug80648.patch
	patch -p1 < debian/patches/hardened-bug78719.patch
	patch -p1 < debian/patches/hardened-bug74779.patch
	patch -p1 < debian/patches/hardened-bug80915.patch
	patch -p1 < debian/patches/hardened-bug53467.patch
	patch -p1 < debian/patches/hardened-bug73533.patch
	patch -p1 < debian/patches/hardened-bug80817.patch
	patch -p1 < debian/patches/hardened-bug80654.patch
	patch -p1 < debian/patches/hardened-bug66783.patch
	patch -p1 < debian/patches/hardened-bug80781.patch
	patch -p1 < debian/patches/hardened-bug80771.patch
	patch -p1 < debian/patches/hardened-bug69668.patch
	patch -p1 < debian/patches/hardened-72595.patch
	patch -p1 < debian/patches/hardened-74544.patch
	patch -p1 < debian/patches/hardened-81294.patch
	patch -p1 < debian/patches/hardened-81283.patch
	patch -p1 < debian/patches/hardened-80849.patch
	patch -p1 < debian/patches/hardened-81305.patch
	patch -p1 < debian/patches/hardened-66719.patch
	patch -p1 < debian/patches/hardened-81252.patch
	patch -p1 < debian/patches/hardened-72146.patch
	patch -p1 < debian/patches/hardened-51498.patch
	patch -p1 < debian/patches/php-7.3-CVE-2022-31625.patch
	patch -p1 < debian/patches/php-7.3-CVE-2022-31626.patch
	patch -p1 < debian/patches/php-upstream-CVE-2022-31629.patch
	patch -p1 < debian/patches/php-7-CVE-2022-31628.patch
	patch -p1 < debian/patches/php-7.4-CVE-2022-37454.patch
	patch -p1 < debian/patches/php-upstream-CVE-2022-31631.patch
	patch -p1 < debian/patches/upstream-CVE-2023-0568.patch
	patch -p1 < debian/patches/php-7.0-CVE-2023-0662.patch
	patch -p1 < debian/patches/upstream-CVE-2023-0567.patch
	patch -p1 < debian/patches/php-7.3-CVE-2023-3823.patch
	patch -p1 < debian/patches/php-upstream-CVE-2023-3824.patch
	patch -p1 < debian/patches/php-7.1-CVE-2023-3247.patch
	patch -p1 < debian/patches/sock-path-mysqlnd.patch
	patch -p1 < debian/patches/php7.2-CVE-2024-3096.patch
	patch -p1 < debian/patches/php-7.3-CVE-2024-2756.patch

	cp -p Zend/LICENSE Zend/ZEND_LICENSE
	cp -p TSRM/LICENSE TSRM_LICENSE
	cp -p ext/gd/libgd/README gd_README
	# Multiple builds for multiple SAPIs
	for target in $(TARGETS); do \
	  mkdir -p build-$${target}/Zend && cp Zend/zend_{language,ini}_{parser,scanner}.[chy] build-$${target}/Zend; \
	done
	# Remove bogus test; position of read position after fopen(, "a+")
	# # is not defined by C standard, so don't presume anything.
	rm -f ext/standard/tests/file/bug21131.phpt

	# Tests that fail.
	rm -f ext/standard/tests/file/bug22414.phpt ext/iconv/tests/bug16069.phpt

	# Remove tests which require external network access
	rm -f ext/sockets/tests/socket_bind.diff

	# Easter eggs no not shipped
	rm -f tests/basic/php_egg_logo_guid.diff
	for i in $(shell grep 'Package' debian/control | awk '{print $$2}'); do \
		if [ ! -f debian/$${i}.postinst ]; then \
			echo "#!/bin/bash" > debian/$${i}.postinst; \
			echo "[ -x /usr/bin/cl-linksafe-apply-group ] && /usr/bin/cl-linksafe-apply-group $${i} || :" >> debian/$${i}.postinst; \
		fi; \
	done

override_dh_auto_configure: prep_build
	cat `aclocal --print-ac-dir`/{libtool,ltoptions,ltsugar,ltversion,lt~obsolete}.m4 >>aclocal.m4

	# Force use of system libtool:
	libtoolize --force --copy
	cat `aclocal --print-ac-dir`/{libtool,ltoptions,ltsugar,ltversion,lt~obsolete}.m4 >build/libtool.m4

	# Regenerate configure scripts (patches change config.m4's)
	#touch configure.in
	./buildconf --force

	for target in $(TARGETS); do \
	  pushd build-$${target}; \
	  ../configure $$(eval echo \$${$${target}_config}); \
	  $(SED_ALT_LIBS) \
	  if [ "mysqlnd" = "$${target}" ]; then \
	    pushd modules; \
	    rm -f `ls | grep -v mysql`; \
	    popd; \
	  fi; \
	popd; \
	done

override_dh_auto_build:
	for target in $(TARGETS); do \
	  $(MAKE) $(NJOBS) -C build-$${target}; \
	done


override_dh_auto_install:
	install -m 755 -d $(CURDIR)/debian/tmp$(altdir)/etc
	install -m 755 -d $(CURDIR)/debian/tmp$(altdir)/etc/php-fpm.d
	$(MAKE) -C build-mysqlnd install-modules INSTALL_ROOT=$(CURDIR)/debian/tmp
	for target in litespeed fpm cgi; do \
	  $(MAKE) -C build-$${target} install INSTALL_ROOT=$(CURDIR)/debian/tmp; \
	done
	install -m 644 $(CURDIR)/debian/src/php.ini $(CURDIR)/debian/tmp$(altdir)/etc/
	cd $(CURDIR)/debian/tmp$(altdir)/usr/lib/$(DEB_HOST_MULTIARCH)/php/modules && \
	  mv mysqli.so nd_mysqli.so && \
	  mv pdo_mysql.so nd_pdo_mysql.so
	install -m 755 -d $(CURDIR)/debian/tmp/var/www/icons
	install -m 644    *.gif $(CURDIR)/debian/tmp/var/www/icons/

	# Fix path for sed
	sed 's@[/a-z]*sed@sed@' -i $(buildroot)$(altdir)/usr/bin/phpize

	# For third-party packaging:
	install -m 755 -d $(CURDIR)/debian/tmp$(altdir)/usr/lib/$(DEB_HOST_MULTIARCH)/php/pear \
	                  $(CURDIR)/debian/tmp$(altdir)/usr/share/php
	install -m 755 -d $(CURDIR)/debian/tmp$(altdir)/etc/php.d.all
	install -m 755 -d $(CURDIR)/debian/tmp$(altdir)/var/lib/php
	install -m 700 -d $(CURDIR)/debian/tmp$(altdir)/var/lib/php/session
	install -m 755 -d $(CURDIR)/debian/tmp$(altdir)/link
	install -m 755 -d $(CURDIR)/debian/tmp$(altdir)/etc/php.d
	ln -sf $(altdir)/etc/php.d $(CURDIR)/debian/tmp$(altdir)/link/conf
	install -m 644 $(CURDIR)/debian/src/default.ini $(CURDIR)/debian/tmp$(altdir)/etc/php.d
	install -m 755 -d $(CURDIR)/debian/tmp$(altdir)/etc/php-fpm.d
	install -m 755 -d $(CURDIR)/debian/tmp/usr/lib/systemd/system
	install -m 644  $(CURDIR)/debian/src/alt-php$(PHP_VER)-fpm.service $(CURDIR)/debian/tmp/usr/lib/systemd/system
	cd $(CURDIR)/debian/tmp$(altdir)/usr/bin && ln -sfn phar.phar phar
	# Generate files lists and stub .ini files for each subpackage
	for mod in $(mods); do \
	  if test ! -e $(CURDIR)/debian/tmp$(altdir)/usr/lib/$(DEB_HOST_MULTIARCH)/php/modules/$${mod}.so ; then \
	  continue; \
	  fi; \
	  if test $${mod} = xsl || test $${mod} = xmlreader ; then \
	    echo -e "; Enable $${mod} extension module \
	      \nextension=dom.so \
	      \nextension=$${mod}.so" > $(CURDIR)/debian/tmp$(altdir)/etc/php.d.all/$${mod}.ini; \
	  elif test  $${mod} = pdo_pgsql || test $${mod} = pdo_odbc || test $${mod} = pdo_sqlite || test $${mod} = pdo_dblib || test $${mod} = pdo_firebird ; then \
	    echo -e "; Enable $${mod} extension module \
		  \nextension=pdo.so \
		  \nextension=$${mod}.so " > $(CURDIR)/debian/tmp$(altdir)/etc/php.d.all/$${mod}.ini; \
	  elif test $${mod} = opcache ; then \
	    echo -e "; Enable $${mod} extension module \
	      \nzend_extension=$(altdir)/usr/lib/$(DEB_HOST_MULTIARCH)/php/modules/opcache.so" > $(CURDIR)/debian/tmp$(altdir)/etc/php.d.all/$${mod}.ini; \
	  elif test $${mod} = nd_mysqli ; then \
	    echo -e "; Enable $${mod} extension module \
	    \nextension=mysqlnd.so \
	    \nextension=$${mod}.so" > $(CURDIR)/debian/tmp$(altdir)/etc/php.d.all/$${mod}.ini; \
	  elif test $${mod} = nd_pdo_mysql ; then \
	    echo -e "; Enable $${mod} extension module \
	    \nextension=pdo.so \
	    \nextension=mysqlnd.so \
	    \nextension=$${mod}.so" > $(CURDIR)/debian/tmp$(altdir)/etc/php.d.all/$${mod}.ini; \
	  else \
	    echo -e "; Enable $${mod} extension module \
	    \nextension=$${mod}.so" > $(CURDIR)/debian/tmp$(altdir)/etc/php.d.all/$${mod}.ini; \
	  fi; \
	done
	rm -f $(CURDIR)/debian/tmp$(altdir)/usr/lib/$(DEB_HOST_MULTIARCH)/php/modules/*.a \
		  $(CURDIR)/debian/tmp/var/www/icons/php.gif

	# SCL
	install -d $(CURDIR)/debian/tmp/etc/scl/prefixes
	install -d $(CURDIR)/debian/tmp$(scl_dir)
	echo '/opt/cloudlinux' >> $(CURDIR)/debian/tmp/etc/scl/prefixes/alt-php73
	echo 'export PATH=$(scl_dir)/root/usr/bin:$(scl_dir)/root/usr/sbin$${PATH:+:$${PATH}}' >> $(CURDIR)/debian/tmp$(scl_dir)/enable
	echo 'export MANPATH=$(scl_dir)/root/usr/share/man:$${MANPATH}' >> $(CURDIR)/debian/tmp$(scl_dir)/enable
	ln -sf $(altdir) $(CURDIR)/debian/tmp$(scl_dir)/root

	# fix order of system rpath dir in RPATH env
	for i in $$(find $(buildroot) -type f -name "*.so") $$(ls -1 $(buildroot)$(_bindir)/* $(buildroot)$(_sbindir)/*); do \
	    $\(chrpath -l $${i} | grep -oP '(?<=RUNPATH=).*' | grep -P "(^|:)/usr/lib/$(DEB_HOST_MULTIARCH)[/]?:") > /dev/null; \
	    if [ $$? -eq 0 ]; then \
	        rpath_str=$$(chrpath -l $${i} | grep -oP '(?<=RUNPATH=).*' | grep -P "(^|:)/usr/lib/$(DEB_HOST_MULTIARCH)[/]?:"); \
	        new_rpath_str=$$(echo $${rpath_str} | sed -e "s@\(^\|:\)/usr/lib/$(DEB_HOST_MULTIARCH)[/]\?:@:@g;s@^:@@"); \
	        new_rpath_str="$${new_rpath_str}:/usr/lib/$(DEB_HOST_MULTIARCH)"; \
	        echo $${new_rpath_str}; \
	        chrpath --replace $${new_rpath_str} $${i}; \
	    fi; \
	done

override_dh_missing:
	dh_missing --fail-missing

.PHONY: prep_build mod mods clean
