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

if (php_sapi_name() !== 'cli') {
    exit;
}

$app_root = $source_root = dirname(__DIR__);

if (!is_file($app_root . '/vendor/autoload.php')) {
    $app_root = dirname(__DIR__, 4);
    $source_root = $app_root . '/vendor/erikaheidi/yamldocs';
}

require $app_root . '/vendor/autoload.php';

use Minicli\App;
use Minicli\Exception\CommandNotFoundException;

$config = include $source_root . '/config.php';
if (is_file($app_root . '/yamldocs.php')) {
    $config = array_merge($config, include $app_root . '/yamldocs.php');
}

$app = new App($config);
try {
    $app->runCommand($argv);
} catch (CommandNotFoundException $notFoundException) {
    $app->getPrinter()->error("Command Not Found.");
    return 1;
} catch (Exception $exception) {
    if ($app->config->debug) {
        $app->getPrinter()->error("An error occurred:");
        $app->getPrinter()->error($exception->getMessage());
    }
    return 1;
}

return 0;