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

/*
|--------------------------------------------------------------------------
| Crunz
|--------------------------------------------------------------------------
|
| This file is part of Crunz library.
| (c) Reza M. Lavaryan <mrl.8081@gmail.com>
| For the full copyright and license information, please view the LICENSE
| file that was distributed with this source code.
|
*/

use Composer\InstalledVersions;
use Crunz\Application;

if (!\defined('CRUNZ_BIN')) {
    \define('CRUNZ_BIN', __FILE__);
}

$generatePath = static fn(string ...$parts): string => \implode(DIRECTORY_SEPARATOR, $parts);
$autoloadPaths = [
    // Dependency
    $generatePath(
        \dirname(__DIR__, 2),
        'autoload.php'
    ),
    // Vendor/Bin
    $generatePath(
        \dirname(__DIR__),
        'autoload.php'
    ),
    // Local dev
    $generatePath(
        __DIR__,
        'vendor',
        'autoload.php'
    ),
];
$loadAutoloader = static function () use($autoloadPaths): void {
    foreach ($autoloadPaths as $autoloadPath) {
        if (\file_exists($autoloadPath) === true) {
            require_once $autoloadPath;
            return;
        }
    }

    throw new RuntimeException(
        \sprintf(
            'Unable to find "vendor/autoload.php" in "%s" paths.',
            \implode('", "', $autoloadPaths)
        )
    );
};
$loadAutoloader();

$application = new Application(
    'Crunz Command Line Interface',
    InstalledVersions::getPrettyVersion('crunzphp/crunz') ?? '1.0.x-dev',
);
$application->run();
