#!/usr/bin/env bash
set -euo pipefail

build_root=$1
source_root=$2

cli_dir="$build_root/src/cli"
export PATH="$cli_dir:$PATH"

for tool in phpefc-php2ast phpefc-phpast2phpir phpefc-phpir2valair phpefc-valair2vala phpefc-valair-optimize-typing phpefc-c-add-source-lines; do
    if [ ! -x "$cli_dir/$tool" ]; then
        echo "missing built PHPEFC tool: $cli_dir/$tool" >&2
        exit 99
    fi
done

pw_build_meson=${PW_BUILD_MESON:-}
if [ -z "$pw_build_meson" ]; then
    for candidate in \
        "$source_root/../../gentoo-pw-programs/dev-php_pw-build-meson/build/src/pw-build-meson" \
        "$source_root/../gentoo-pw-programs/dev-php_pw-build-meson/build/src/pw-build-meson"; do
        if [ -x "$candidate" ]; then
            pw_build_meson=$candidate
            break
        fi
    done
    if [ -z "$pw_build_meson" ]; then
        pw_build_meson=$(command -v pw-build-meson || true)
    fi
fi
if [ -z "$pw_build_meson" ] || [ ! -x "$pw_build_meson" ]; then
    echo 'missing pw-build-meson; skipping PHPEF library include activation regression' >&2
    exit 77
fi

work=${MESON_TEST_TMPDIR:-}
if [ -z "$work" ]; then
    work=$(mktemp -d)
    trap 'rm -rf "$work"' EXIT
fi
mkdir -p "$work"

prepend_project_header() {
    local project_dir=$1
    python3 - "$project_dir/meson.build" <<'PY'
import sys
from pathlib import Path
p = Path(sys.argv[1])
s = p.read_text()
p.write_text("project('phpef-library-include-activation-regression', ['c', 'vala'], version: '0.1')\n" + s)
PY
}

project="$work/library-include-activation"
mkdir -p "$project/library1"
cat > "$project/library1/main.php" <<'PHP'
<?php
include "helper.php";
print "library1-top\n";
function library1_answer() {
    return library1_helper();
}
PHP
cat > "$project/library1/helper.php" <<'PHP'
<?php
function library1_helper() {
    return "library1-helper";
}
PHP
cat > "$project/main.php" <<'PHP'
<?php
print "app-main\n";
print library1_answer();
print "\n";
print library1_helper();
print "\n";
PHP
cat > "$project/meson.build.pw" <<'PW'
phpef-runtime-embed = true
phpef-runtime-natives = standard
elib = library1/main.php + library1/helper.php -> library1
ebin = main.php -> app (library1)
PW

"$pw_build_meson" "$project" --no-doxygen >/dev/null
prepend_project_header "$project"
meson setup "$project/build" "$project" >/dev/null
CCACHE_DISABLE=1 meson compile -C "$project/build" >/dev/null

set +e
"$project/build/app" > "$project/app.out" 2>&1
app_rc=$?
set -e

if [ "$app_rc" -ne 0 ]; then
    echo 'PHPEF library include activation regression failed: app exited non-zero' >&2
    cat "$project/app.out" >&2
    exit 1
fi

expected=$'library1-top\napp-main\nlibrary1-helper\nlibrary1-helper'
actual=$(cat "$project/app.out")
if [ "$actual" != "$expected" ]; then
    echo 'unexpected PHPEF library include activation output' >&2
    printf 'expected:\n%s\nactual:\n%s\n' "$expected" "$actual" >&2
    exit 1
fi

if grep -Fq 'Cannot redeclare library1_helper()' "$project/app.out"; then
    echo 'PHPEF library include activation redeclared library1_helper()' >&2
    cat "$project/app.out" >&2
    exit 1
fi

printf 'ok - PHPEF library internal include activation does not redeclare functions\n'
