<?php
declare(strict_types=1);

define('APP_ROOT', dirname(__DIR__));
define('APP_PATH', APP_ROOT . '/app');
define('CONFIG_PATH', APP_ROOT . '/config');
define('STORAGE_PATH', APP_ROOT . '/storage');
define('PUBLIC_PATH', __DIR__);

// Compute subdirectory base path (e.g. '/r' when installed at domain.com/r/)
(function () {
    $script = $_SERVER['SCRIPT_NAME'] ?? '/index.php';
    if (str_ends_with($script, '/public/index.php')) {
        $base = substr($script, 0, -strlen('/public/index.php'));
    } elseif (str_ends_with($script, '/index.php')) {
        $base = substr($script, 0, -strlen('/index.php'));
    } else {
        $base = '';
    }
    define('APP_BASE', rtrim($base, '/'));
})();

// First-run installer
if (!file_exists(STORAGE_PATH . '/installed.lock')) {
    header('Location: ' . APP_BASE . '/install.php');
    exit;
}

require APP_ROOT . '/vendor/autoload.php';

use App\Core\Application;

$app = new Application();
$app->run();
