#!/bin/bash
### Copyright 1999-2025. WebPros International GmbH. All rights reserved.

PATH=/opt/psa/admin/sbin:$PATH
LANG=C
unset GREP_OPTIONS
umask 022

for variable in "PHP_CLI" "PHP_INI" "PHP_ID"; do
	[ -z "${!variable}" ] && not_set="$not_set
$variable"
done

[ -n "$not_set" ] && echo "The following environment variables are empty, this script is not intended to be run outside of php_handlers_control: $not_set" 1>&2 && exit 1

if ! echo "$PHP_ID" | grep -q "^alt-php" ; then
	exit 0
fi

if [ "$1" = "add" ]; then
	#workaround for bug in CloudLinux package
	php_modules_ctl --php-cli "$PHP_CLI" --php-ini "$PHP_INI" -l 2>/dev/null | grep -q "^mysqli "

	if [ "$?" -ne 0 ]; then
		config=$("$PHP_CLI" -c "$PHP_INI" -r "print(php_ini_scanned_files());" | head -n1 | cut -d"," -f1)

		if [ -f "$config" ]; then
			#During "yum groupinstall alt-phpXY" scriplet alt-phpXY-mysql-meta runnig after alt-phpXY
			#Therefore symlink /opt/alt/phpXY/usr/lib64/php/modules/mysqli.so does not exist yet
			#Enable extension manually, without php_modules_ctl
			grep -q "^\s*extension=mysqli.so\s*$" "$config" || echo "extension=mysqli.so" >> "$config"
		fi
	fi

	php_modules_ctl --php-cli "$PHP_CLI" --php-ini "$PHP_INI" -e json -e zip -e mysqli

fi
