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

use Composer\XdebugHandler\XdebugHandler;
use Deptrac\Deptrac\Supportive\Console\Application;

if (PHP_VERSION_ID < 80200) {
    echo 'Required at least PHP version 8.2.0, your version: '.PHP_VERSION."\n";
    exit(1);
}

/**
 * $_composer_autoload_path is set by composer (available from composer 2.2).
 *
 * @see https://getcomposer.org/doc/articles/vendor-binaries.md#finding-the-composer-autoloader-from-a-binary
 */
if (isset($_composer_autoload_path)) {
    define('DEPTRAC_COMPOSER_INSTALL', $_composer_autoload_path);
} else {
    $autoloadPaths = [
        __DIR__.'/../../autoload.php', // inside vendor dir like "vendor/deptrac/deptrac"
        __DIR__.'/../vendor/autoload.php', // first level project dir like "bin/deptrac"
        __DIR__.'/vendor/autoload.php', // project root like (./deptrac)
    ];

    foreach ($autoloadPaths as $path) {
        if (file_exists($path)) {
            define('DEPTRAC_COMPOSER_INSTALL', $path);

            break;
        }
    }

    unset($autoloadPaths, $path);
}

if (!defined('DEPTRAC_COMPOSER_INSTALL')) {
    fwrite(
        STDERR,
        'You need to set up the project dependencies using Composer:'.PHP_EOL.PHP_EOL.
        '    composer install'.PHP_EOL.PHP_EOL.
        'You can learn all about Composer on https://getcomposer.org/.'.PHP_EOL
    );

    exit(1);
}

require_once DEPTRAC_COMPOSER_INSTALL;

$xdebug = new XdebugHandler('DEPTRAC');
$xdebug->check();
unset($xdebug);

(new Application())->run();
