#!/bin/sh

# Tests for php-ext-info, php-versions, gen-control and pkg-pecl.mk, run from override_dh_auto_test

top=$(cd "$(dirname "$0")/.." && pwd)
fixtures="${top}/tests/fixtures"
info="${top}/php-ext-info"
versions="${top}/php-versions"
gencontrol="${top}/gen-control"

PHPQUERY="${top}/tests/phpquery"
export PHPQUERY

tmpdir=$(mktemp -d)
trap 'rm -rf "${tmpdir}"' EXIT

count=0
failed=0

# check <directory> <expected output on one line> <command>...
check() {
	dir=$1
	expected=$2
	shift 2
	count=$((count + 1))
	actual=$(cd "${dir}" && "$@" 2>/dev/null | tr '\n' ' ' | sed -e 's/ *$//')
	if [ "${actual}" != "${expected}" ]; then
		echo "FAIL: $(basename "${dir}"): $(basename "$1") $(shift; echo "$*")"
		echo "  expected: ${expected}"
		echo "  actual:   ${actual}"
		failed=$((failed + 1))
	fi
}

# constraint <"require"."php"> <supported versions out of the installed ones>
constraint() {
	printf '{"name": "example/test", "type": "php-ext", "require": {"php": "%s"}}\n' "$1" >"${tmpdir}/composer.json"
	# shellcheck disable=SC2046
	check "${tmpdir}" "$2" "${info}" supports $("${PHPQUERY}" -V)
}

# relations <fixture> <BREAKS_NAME> <PHP version>...
# The packages of the generated debian/control with the first name in their
# Provides, Breaks and Replaces
relations() {
	rm -rf "${tmpdir}/control"
	cp -a "${fixtures}/$1" "${tmpdir}/control"
	BREAKS_NAME=$2
	export BREAKS_NAME
	shift 2
	(cd "${tmpdir}/control" && "${gencontrol}" "$@" &&
		sed -n -e 's/^Package: //p' -e 's/^\(Provides\|Breaks\|Replaces\): \([^ ,]*\).*/\1=\2/p' debian/control)
}

# overrides <dh sequence>
# The dh_auto_* override targets of pkg-pecl.mk that dh runs in the sequence,
# followed by "ok".  pkg-pecl.mk asks the real phpquery, so does this test, and
# dh must not see the options of the dh that runs the tests.
overrides() {
	rm -rf "${tmpdir}/build"
	mkdir -p "${tmpdir}/build/debian"
	(
		cd "${tmpdir}/build" || exit 1
		unset DH_INTERNAL_OPTIONS DH_INTERNAL_OVERRIDE DH_OPTIONS MAKEFLAGS MFLAGS
		PHPQUERY=/usr/sbin/phpquery
		installed=$("${PHPQUERY}" -V | tr '\n' ' ')
		sed -e "s/^X-PHP-PECL-Name: .*/&\nX-PHP-Versions: ${installed}/" "${fixtures}/control-renamed/debian/control.in" >debian/control.in
		echo 10 >debian/compat
		printf 'php-newname (1.0-1) unstable; urgency=medium\n\n  * Test\n\n -- Test <test@example.org>  Thu, 01 Jan 2026 00:00:00 +0000\n' >debian/changelog
		printf '#!/usr/bin/make -f\ninclude %s/pkg-pecl.mk\n' "${top}" >debian/rules
		chmod +x debian/rules
		"${gencontrol}" -a && dh "$1" --no-act >dh.out &&
			sed -n -e 's/^ *debian\/rules \(override_dh_auto_[a-z_-]*\)$/\1/p' dh.out && echo ok
	)
}

all="8.6 8.5 8.4 8.3 8.2 8.1 8.0 7.4 7.3 7.2 7.1 7.0 5.6"

# Layout
check "${fixtures}/pie-lz4" "pie" "${info}" layout
check "${fixtures}/pie-deepclone" "pie" "${info}" layout
check "${fixtures}/pie-ed25519" "pie" "${info}" layout
check "${fixtures}/pie-xdebug" "pie" "${info}" layout
check "${fixtures}/pie-opentelemetry" "pie" "${info}" layout
check "${fixtures}/pecl-swoole" "pecl" "${info}" layout
check "${fixtures}/pecl-prepackaged" "pecl" "${info}" layout
check "${fixtures}/pecl-igbinary" "pecl" "${info}" layout
check "${fixtures}/none-plain" "none" "${info}" layout
check "${fixtures}" "none" "${info}" layout

# Metadata
check "${fixtures}/pie-lz4" "example/lz4" "${info}" name
check "${fixtures}/pie-lz4" "lz4" "${info}" extension-name
check "${fixtures}/pie-lz4" "php" "${info}" extension-type
check "${fixtures}/pie-lz4" "80" "${info}" priority
check "${fixtures}/pie-lz4" "" "${info}" build-path
check "${fixtures}/pie-lz4" "" "${info}" depends
check "${fixtures}/pie-lz4" "" "${info}" suggests
check "${fixtures}/pie-lz4" "; configuration for php lz4 module ; priority=80 extension=lz4.so" "${info}" ini

check "${fixtures}/pie-deepclone" "igbinary" "${info}" suggests

check "${fixtures}/pie-ed25519" "mysqlnd_ed25519" "${info}" extension-name
check "${fixtures}/pie-ed25519" "30" "${info}" priority
check "${fixtures}/pie-ed25519" "hash mysqlnd pdo-mysql sodium" "${info}" depends
check "${fixtures}/pie-ed25519" "; configuration for php mysqlnd_ed25519 module ; priority=30 extension=mysqlnd_ed25519.so" "${info}" ini

check "${fixtures}/pie-xdebug" "xdebug" "${info}" extension-name
check "${fixtures}/pie-xdebug" "zend" "${info}" extension-type
check "${fixtures}/pie-xdebug" "; configuration for php xdebug module ; priority=90 zend_extension=xdebug.so" "${info}" ini

check "${fixtures}/pie-opentelemetry" "opentelemetry" "${info}" extension-name
check "${fixtures}/pie-opentelemetry" "ext" "${info}" build-path

check "${fixtures}/pie-versioned" "versioned" "${info}" extension-name
check "${fixtures}/pie-versioned" "versioned-1.2.3" "${info}" build-path

# The metadata queries need the PIE layout
check "${fixtures}/none-plain" "" "${info}" depends
check "${fixtures}/pecl-swoole" "" "${info}" extension-name

# PHP versions
check "${fixtures}/pie-lz4" "${all}" "${versions}"
check "${fixtures}/pie-deepclone" "8.6 8.5 8.4 8.3 8.2" "${versions}"
check "${fixtures}/pie-ed25519" "8.6 8.5 8.4 8.3 8.2" "${versions}"
check "${fixtures}/pie-xdebug" "8.6 8.5 8.4 8.3 8.2" "${versions}"
check "${fixtures}/pie-opentelemetry" "8.6 8.5 8.4 8.3 8.2 8.1 8.0" "${versions}"
check "${fixtures}/pie-versioned" "8.5 8.3" "${versions}"
check "${fixtures}/pecl-swoole" "8.5 8.4 8.3 8.2" "${versions}"
check "${fixtures}/pecl-prepackaged" "8.3 8.2 8.1 8.0 7.4" "${versions}"
check "${fixtures}/pecl-igbinary" "7.4 7.3 7.2 7.1 7.0 5.6" "${versions}"
check "${fixtures}/none-plain" "8.1 8.0 7.4" "${versions}"

# Composer version constraints
constraint ">=8.2" "8.6 8.5 8.4 8.3 8.2"
constraint ">= 5.6.0" "${all}"
constraint "*" "${all}"
constraint ">=8.2 <8.6" "8.5 8.4 8.3 8.2"
constraint ">=8.2,<8.7" "8.6 8.5 8.4 8.3 8.2"
constraint ">=8.2, <8.5.0" "8.4 8.3 8.2"
constraint ">8.1" "8.6 8.5 8.4 8.3 8.2 8.1"
constraint "<8.4" "8.3 8.2 8.1 8.0 7.4 7.3 7.2 7.1 7.0 5.6"
constraint "<=8.4" "8.4 8.3 8.2 8.1 8.0 7.4 7.3 7.2 7.1 7.0 5.6"
constraint "^8.1" "8.6 8.5 8.4 8.3 8.2 8.1"
constraint "^7.4 || ^8.0" "8.6 8.5 8.4 8.3 8.2 8.1 8.0 7.4"
constraint "~8.2" "8.6 8.5 8.4 8.3 8.2"
constraint "~8.2.0 || ~8.4.0" "8.4 8.2"
constraint "8.1.*||8.3.*" "8.3 8.1"
constraint "v8.2.*|8.5.x" "8.5 8.2"
constraint "8.3" "8.3"
constraint "7.4 - 8.1" "8.1 8.0 7.4"
constraint "7.4.0 - 8.1.0" "8.1 8.0 7.4"
constraint ">=8.1.0-dev" "8.6 8.5 8.4 8.3 8.2 8.1"
constraint ">=8.2 !=8.3.1" "8.6 8.5 8.4 8.3 8.2"
constraint ">=8.2@stable" "8.6 8.5 8.4 8.3 8.2"
constraint ">=8.6 <8.2" ""

# debian/control: BREAKS_NAME renames the taken over names along with the package
check "${fixtures}" "php-newname Provides=php-oldname Breaks=php-oldname Replaces=php-oldname php-newname-all-dev Provides=php-oldname-all-dev Breaks=php-oldname-all-dev Replaces=php-oldname-all-dev php8.6-newname Provides=php8.6-oldname Breaks=php8.6-oldname Replaces=php8.6-oldname php8.5-newname Provides=php8.5-oldname Breaks=php8.5-oldname Replaces=php8.5-oldname" relations control-renamed oldname 8.5 8.6
check "${fixtures}" "php-newname Provides=php-oldname Breaks=php-oldname Replaces=php-oldname php-newname-all-dev Provides=php-oldname Breaks=php-oldname Replaces=php-oldname php8.5-newname Provides=php-oldname Breaks=php-oldname Replaces=php-oldname" relations control-renamed "" 8.5

# pkg-pecl.mk: only the architecture dependent packages need the extension compiled
if [ -x /usr/sbin/phpquery ] && command -v php-config >/dev/null && command -v dh >/dev/null; then
	check "${fixtures}" "override_dh_auto_configure-arch override_dh_auto_build-arch override_dh_auto_test-arch ok" overrides build-arch
	check "${fixtures}" "ok" overrides build-indep
else
	echo "SKIP: pkg-pecl.mk: needs debhelper and an installed PHP"
fi

if [ "${failed}" -ne 0 ]; then
	echo "${failed} of ${count} tests failed"
	exit 1
fi

echo "${count} tests passed"
exit 0
