#!/usr/bin/env php
<?php
// @link https://www.tomasvotruba.cz/blog/2018/08/02/5-gotchas-of-the-bin-file-in-php-cli-applications/

gc_disable(); // performance boost

if (\Phar::running()) {
    $possibleAutoloadPaths = [
        'phar://compatinfo-db.phar/vendor/autoload.php'
    ];
} else {
    $possibleAutoloadPaths = [
        // local dev repository
        __DIR__ . '/../vendor/autoload.php',
        // dependency
        __DIR__ . '/../../../../vendor/autoload.php',
    ];
}

$isAutoloadFound = false;
foreach ($possibleAutoloadPaths as $possibleAutoloadPath) {
    if (file_exists($possibleAutoloadPath)) {
        require_once $possibleAutoloadPath;
        $isAutoloadFound = true;
        break;
    }
}

if ($isAutoloadFound === false) {
    throw new RuntimeException(sprintf(
        'Unable to find "vendor/autoload.php" in "%s" paths.',
        implode('", "', $possibleAutoloadPaths)
    ));
}

use Bartlett\CompatInfoDb\Presentation\Console\ApplicationInterface;

use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

putenv('APP_ENV=' . ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? 'prod'));

/** @var ContainerBuilder $container */
$container = require dirname(__DIR__) . '/config/container.php';

/** @var ApplicationInterface $app */
$app = $container->get(ApplicationInterface::class);
$app->setContainer($container);

// @link https://symfony.com/doc/current/console/lazy_commands.html
$app->setCommandLoader($container->get(CommandLoaderInterface::class));

exit($app->run());
