<?php

declare(strict_types=1);

$root = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR;

$relativePath = str_replace('\\', '/', (string)($argv[1] ?? ''));
if ($relativePath === '' || str_contains($relativePath, '..')) {
    fwrite(STDERR, 'Missing or invalid legacy test path.' . PHP_EOL);
    exit(2);
}
$bootstrap = strtolower((string)($argv[2] ?? 'app'));
if (!in_array($bootstrap, ['app', 'lite'], true)) {
    fwrite(STDERR, 'Invalid legacy bootstrap mode: ' . $bootstrap . PHP_EOL);
    exit(2);
}

$scriptPath = $root . str_replace('/', DIRECTORY_SEPARATOR, $relativePath);
if (!is_file($scriptPath)) {
    fwrite(STDERR, 'Legacy test not found: ' . $relativePath . PHP_EOL);
    exit(2);
}

if ($bootstrap === 'app') {
    require_once $root . 'tests/Support/legacy_bootstrap.php';
}

try {
    require $scriptPath;
} catch (Throwable $throwable) {
    fwrite(STDERR, get_class($throwable) . ': ' . $throwable->getMessage() . PHP_EOL);
    fwrite(STDERR, $throwable->getFile() . ':' . $throwable->getLine() . PHP_EOL);
    exit(1);
}
