#!/bin/sh
# Test the installed php<version>-<ext> packages built by pkg-pecl.mk: every
# extension they ship must load, and the upstream tests must pass.  autodep8
# runs this from the top of the source tree for Testsuite: autopkgtest-pkg-pecl.
set -eu

DH_PHP_DIR=$(dirname "$(readlink -f "$0")")
tmp=${AUTOPKGTEST_TMP:-}
if [ -z "${tmp}" ]; then
	tmp=$(mktemp -d)
	trap 'rm -rf "${tmp}"' EXIT
fi

# The same name as gen-control and pkg-pecl.mk use for php<version>-<ext>
PECL_NAME=$(sed -n 's/^X-PHP-PECL-Name: //p' debian/control.in)
if [ -z "${PECL_NAME}" ]; then
	PECL_NAME=$(sed -ne 's/PECL_NAME_OVERRIDE[[:space:]]*:*=[[:space:]]*//p' debian/rules)
fi
if [ -z "${PECL_NAME}" ]; then
	PECL_NAME=$(sed -n 's/^Source: //p' debian/control.in | sed -e 's/^php-//')
fi

versions=$(dpkg-query -W -f '${Package} ${Status}\n' "php*-${PECL_NAME}" 2>/dev/null |
	sed -n "s/^php\([0-9]*\.[0-9]*\)-${PECL_NAME} install ok installed\$/\1/p" | sort -V)
if [ -z "${versions}" ]; then
	echo "No php<version>-${PECL_NAME} package is installed" >&2
	exit 1
fi

layout=$("${DH_PHP_DIR}/php-ext-info" layout)

# source_dir <version>
# The directory with the upstream sources for the PHP version, as in pkg-pecl.mk
source_dir() {
	if [ "${layout}" = pie ]; then
		echo .
		return
	fi
	for package_xml in "package-$1.xml" "package-${1%.*}.xml" package.xml; do
		if [ -f "${package_xml}" ]; then
			echo "$(xml2 <"${package_xml}" | sed -n 's,^/package/name=,,p')-$(xml2 <"${package_xml}" | sed -n 's,^/package/version/release=,,p')"
			return
		fi
	done
}

rc=0
for ver in ${versions}; do
	echo "=== php${ver}-${PECL_NAME}"
	if ! command -v "php${ver}" >/dev/null; then
		echo "php${ver} is not installed" >&2
		rc=1
		continue
	fi

	# Every extension in the .ini files of the package is mapped into PHP,
	# with all its symbols resolved
	maps=$(LD_BIND_NOW=1 "php${ver}" -r 'readfile("/proc/self/maps");') || true
	sos=$(dpkg-query -L "php${ver}-${PECL_NAME}" | grep "^/etc/php/${ver}/mods-available/.*\.ini\$" |
		xargs -r sed -n 's/^[[:space:]]*\(zend_\)\{0,1\}extension[[:space:]]*=[[:space:]]*"\{0,1\}\([^"[:space:]]*\)"\{0,1\}.*/\2/p')
	if [ -z "${sos}" ]; then
		echo "php${ver}-${PECL_NAME} enables no extension" >&2
		rc=1
		continue
	fi
	for so in ${sos}; do
		so=${so##*/}
		so=${so%.so}.so
		if printf '%s\n' "${maps}" | grep -q "/${so}\$"; then
			echo "${so} loads"
		else
			echo "${so} does not load in PHP ${ver}" >&2
			rc=1
		fi
	done

	srcdir=$(source_dir "${ver}")
	builddir=.
	if [ "${layout}" = pie ]; then
		builddir=$("${DH_PHP_DIR}/php-ext-info" build-path)
		builddir=${builddir:-.}
	fi
	if [ -z "${srcdir}" ] || [ ! -d "${srcdir}/${builddir}/tests" ]; then
		echo "No upstream tests"
		continue
	fi

	# Upstream tests are written to run one at a time, but run-tests.php from
	# PHP 8.6 runs them in parallel unless told otherwise; -j exists since PHP 7.4
	jobs=
	if "php${ver}" -r 'exit(PHP_VERSION_ID >= 70400 ? 0 : 1);'; then
		jobs=-j1
	fi
	# run-tests.php writes its files next to the tests, so it works on a copy
	rm -rf "${tmp}/src"
	cp -a "${srcdir}" "${tmp}/src"
	if ! (cd "${tmp}/src/${builddir}" &&
		NO_INTERACTION=1 REPORT_EXIT_STATUS=1 TEST_PHP_EXECUTABLE="$(command -v "php${ver}")" \
			"php${ver}" "$(php-config"${ver}" --extension-dir)/build/run-tests.php" ${jobs:+"${jobs}"} tests); then
		find "${tmp}/src/${builddir}/tests" -name '*.diff' -exec sh -c 'for f; do echo "--- ${f#"$0"/}"; cat "$f"; done' "${tmp}/src/${builddir}/tests" {} +
		rc=1
	fi
done

exit "${rc}"
