#!/usr/bin/env php
<?php
$baseDir = realpath(__DIR__ . '/../../../../');

$tryFiles = [
    __DIR__ . '/../../../autoload.php',
    __DIR__ . '/../../autoload.php',
    __DIR__ . '/../vendor/autoload.php',
    __DIR__ . '/vendor/autoload.php',
];

$autoloadFile;

foreach ($tryFiles as $file) {
    if (file_exists($file)) {
        $autoloadFile = $file;
        break;
    }
}

if (!$autoloadFile) {
    throw new \Exception('Cannot find autoload file');
}

require $autoloadFile;

if (file_exists('setting/setting.php')) {
    $configBuilder = new \Gap\Config\ConfigBuilder(
        getcwd(),
        'setting/setting.php',
        'cache/setting-console.php'
    );

    $config = $configBuilder->build();
} else {
    $config = new \Gap\Config\Config();
    $config->set('baseDir', getcwd());
}

$app = new \Gap\Base\App($config);
$consoleHandler = new \Gap\Util\ConsoleHandler($app);
$consoleHandler->handle($argv, $argc);
