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

altdir := /opt/alt/php82
_sysconfigdir := /etc
buildroot := $(CURDIR)/debian/tmp

# ALTEAPLN-12: built as a variable so the php82+ only entry can be appended
# without leaving a dangling line continuation when DBPP strips the block.
movefiles := .$(altdir)$(_sysconfigdir)/php.d.all/*
movefiles += .$(altdir)$(_sysconfigdir)/php.d/00-mysqlnd.ini

%:
	dh $@

override_dh_auto_configure:

override_dh_auto_build:

override_dh_auto_install:
	install -m 755 -d $(buildroot)$(altdir)$(_sysconfigdir)/php.d.all
	echo -e "; Enable mysqli native driver extension\nextension=mysqlnd.so\nextension=nd_mysqli.so" > $(buildroot)$(altdir)$(_sysconfigdir)/php.d.all/mysqli.ini
	echo -e "; Enable pdo_mysql native driver extension\nextension=pdo.so\nextension=mysqlnd.so\nextension=nd_pdo_mysql.so" > $(buildroot)$(altdir)$(_sysconfigdir)/php.d.all/pdo_mysql.ini

	# ALTEAPLN-12: single owner for mysqlnd for the native php.d scan, in a file
	# php.d reads before every nd_* alias. Declaring it in both alias files made
	# PHP print 'Module "mysqlnd" is already loaded'; declaring it in mysqli.ini
	# alone broke pdo_mysql when mysqli was disabled. The alias names cannot
	# carry it here (mysqli.ini sorts before mysqlnd.ini, so nd_mysqli.so would
	# fail with 'undefined symbol: mysqlnd_global_stats'), so use a 00- prefixed
	# file in php.d.
	# ALTEAPLN-53: this is for php.d only and does NOT replace the
	# extension=mysqlnd.so lines in the php.d.all bodies above. The PHP Selector
	# and CageFS per-user generators build alt_php.ini from php.d.all and never
	# read php.d, and they resolve a dependency as php.d.all/<so basename>.ini,
	# so a 00- prefixed name could not be matched even if it lived there.
	install -m 755 -d $(buildroot)$(altdir)$(_sysconfigdir)/php.d
	echo -e "; mysqlnd must load before nd_mysqli/nd_pdo_mysql (php.d is scanned alphabetically)\nextension=mysqlnd.so" > $(buildroot)$(altdir)$(_sysconfigdir)/php.d/00-mysqlnd.ini

	dh_movefiles -palt-php82-mysql-meta $(movefiles)

override_dh_missing:
	dh_missing --fail-missing

override_dh_clean:
