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

build_root=$1
entry_src=$2
lib_src=$3

cli_dir="$build_root/src/cli"
php2ast="$cli_dir/phpefc-php2ast"
phpast2phpir="$cli_dir/phpefc-phpast2phpir"
phpir2valair="$cli_dir/phpefc-phpir2valair"
valair2vala="$cli_dir/phpefc-valair2vala"
valair_optimize="$cli_dir/phpefc-valair-optimize-typing"

source_main_name() {
    python3 - "$1" <<'PY'
import re, sys
path = sys.argv[1].replace('\\', '/')
path = path.rsplit('/', 1)[-1]
part = re.sub(r'[^A-Za-z0-9_]+', '_', path).strip('_') or 'unknown_php'
if not re.match(r'^[A-Za-z_]', part):
    part = '_' + part
print('phpef_file_' + part)
PY
}

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

entry_real=$(realpath "$entry_src")
lib_real=$(realpath "$lib_src")
lib_main=$(source_main_name "$lib_real")
lib_register=${lib_main%__main}__register_functions

entry_ast="$work/include_entry.phpast"
lib_ast="$work/include_lib.phpast"
entry_phpir="$work/include_entry.phpir"
lib_phpir="$work/include_lib.phpir"
entry_valair="$work/include_entry.valair"
lib_valair="$work/include_lib.valair"
entry_vala="$work/include_entry.vala"
lib_vala="$work/include_lib.vala"

"$php2ast" "$entry_src" "$entry_ast"
"$php2ast" "$lib_src" "$lib_ast"
"$phpast2phpir" "$entry_ast" "$entry_phpir" --external-phpast "$lib_ast"
"$phpast2phpir" "$lib_ast" "$lib_phpir" --external-phpast "$entry_ast"
"$phpir2valair" "$entry_phpir" "$entry_valair" --external-phpir "$lib_phpir"
"$phpir2valair" "$lib_phpir" "$lib_valair" --external-phpir "$entry_phpir"
"$valair_optimize" "$entry_valair" "$entry_valair.afteropt.typing-zval2static"
"$valair_optimize" "$lib_valair" "$lib_valair.afteropt.typing-zval2static"
"$valair2vala" "$entry_valair.afteropt.typing-zval2static" "$entry_vala"
"$valair2vala" "$lib_valair.afteropt.typing-zval2static" "$lib_vala" --no-main

if ! grep -Fq "${lib_register}();" "$entry_vala" || ! grep -Fq "${lib_main}();" "$entry_vala"; then
    echo 'entry Vala does not register included functions before calling included activation function' >&2
    cat "$entry_vala" >&2
    exit 1
fi
if ! awk -v reg="${lib_register}();" -v main="${lib_main}();" 'index($0, reg) { regline=NR } index($0, main) { mainline=NR } END { exit !(regline > 0 && mainline > regline) }' "$entry_vala"; then
    echo 'include registration must appear before included activation call' >&2
    cat "$entry_vala" >&2
    exit 1
fi

if ! grep -Fq "public static void ${lib_register}() throws Error {" "$lib_vala" || ! grep -Fq "public static void ${lib_main}() throws Error {" "$lib_vala"; then
    echo 'support Vala does not declare source-path registration and activation functions' >&2
    cat "$lib_vala" >&2
    exit 1
fi

if grep -Fq 'public static int main' "$lib_vala" || grep -Fq 'public static void phpef_main' "$lib_vala"; then
    echo 'support Vala unexpectedly contains main wrapper' >&2
    cat "$lib_vala" >&2
    exit 1
fi

if ! grep -Fq 'included-top' "$lib_vala"; then
    echo 'support Vala activation function does not preserve top-level include body' >&2
    cat "$lib_vala" >&2
    exit 1
fi

if ! awk -v reg="public static void ${lib_register}() throws Error {" '
    /included-top/ { top_line=NR }
    index($0, reg) { reg_line=NR }
    END { exit !(top_line > 0 && reg_line > top_line) }
' "$lib_vala"; then
    echo 'support Vala register_functions must be emitted after PHP-near function/top-level code' >&2
    cat "$lib_vala" >&2
    exit 1
fi

printf 'ok 1 - static include registers support-file functions then lowers to source-path activation without support main wrapper\n'
