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

use Codelicia\Xulieta\Command\App;
use Codelicia\Xulieta\Config\ConfigFile;
use Composer\InstalledVersions;
use Symfony\Component\Console\Application;

(static function (): void {
    $autoload = array_filter(
        [
            __DIR__ . '/../../../autoload.php',
            __DIR__ . '/../../autoload.php',
            __DIR__ . '/../vendor/autoload.php',
        ],
        is_file(...)
    );

    if (! count($autoload)) {
        fwrite(
            STDERR,
            'You need to set up the project dependencies using the following commands:' . PHP_EOL .
            'wget https://getcomposer.org/composer.phar' . PHP_EOL .
            'php composer.phar install' . PHP_EOL
        );

        die(1);
    }

    $autoloadPath = realpath(current($autoload));

    /* @noinspection PhpIncludeInspection */
    require $autoloadPath;

    $application = new Application('Xulieta', InstalledVersions::getVersion('codelicia/xulieta') ?: 'Undefined');
    $application->add(new App(null, ConfigFile::loadInDirectory(__DIR__)));

    $application->run();
})();
