#!/usr/bin/env php
<?php

use Ninja\Cosmic\Environment\Env;

if (false === in_array(PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
    echo PHP_EOL . 'This app may only be invoked from a command line, got "' . PHP_SAPI . '"' . PHP_EOL;
    exit(1);
}

(static function (): void {
    if (file_exists($autoload = __DIR__ . '/vendor/autoload.php')) {
        include_once $autoload;
        return;
    }
    throw new RuntimeException('Unable to find the Composer autoloader.');
})();

$root = Phar::running() ?: __DIR__;
require sprintf("%s/bootstrap/environment.php", $root);
require sprintf("%s/bootstrap/lifecycle.php", $root);
require sprintf("%s/bootstrap/signals.php", $root);

$app = require sprintf("%s/bootstrap/application.php", $root);


try {
    $instance = $app();
    if (is_dir(Env::basePath("src"))) {
        $instance->registerCommands([Env::basePath("src")]);
    }

    $instance->run();
} catch (Throwable $e) {
    throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
}
