#!/usr/bin/env php
arguments->add([
    'environment' => [
        'prefix'        => 'e',
        'longPrefix'    => 'env',
        'description'   => 'Configuration Environment',
        'defaultValue'  => 'localhost'
    ]
]);
$climate->arguments->parse();
$environment = $climate->arguments->get('environment');
$grav = Grav::instance(array('loader' => $autoload));
$grav->setup($environment);
$grav->initializeCli();
$app     = new Application('Grav Plugins Commands', GRAV_VERSION);
$pattern = '([A-Z]\w+Command\.php)';
// get arguments and  strip the application name
if (null === $argv) {
    $argv = $_SERVER['argv'];
}
$bin  = array_shift($argv);
$name = array_shift($argv);
$argv = array_merge([$bin], $argv);
$input = new ArgvInput($argv);
/** @var \Grav\Common\Data\Data $plugin */
$plugin = $grav['plugins']->get($name);
$output = new ConsoleOutput();
$output->getFormatter()->setStyle('red', new OutputFormatterStyle('red', null, array('bold')));
$output->getFormatter()->setStyle('white', new OutputFormatterStyle('white', null, array('bold')));
if (!$name) {
    $output->writeln('');
    $output->writeln('Usage:');
    $output->writeln("  {$bin} [slug] [command] [arguments]");
    $output->writeln('');
    $output->writeln('Example:');
    $output->writeln("  {$bin} error log -l 1 --trace");
    $list = Folder::all('plugins://', ['compare' => 'Pathname', 'pattern' => '/\/cli\/' . $pattern . '$/usm', 'levels' => 2]);
    $total = 0;
    if (count($list)) {
        $available = [];
        $output->writeln('');
        $output->writeln('Plugins with CLI available:');
        foreach ($list as $index => $entry) {
            $split = explode('/', $entry);
            $entry = array_shift($split);
            $index = str_pad($index++ + 1, 2, '0', STR_PAD_LEFT);
            $total = str_pad($total++ + 1, 2, '0', STR_PAD_LEFT);
            if (\in_array($entry, $available, true)) {
                $total--;
                continue;
            }
            $available[] = $entry;
            $commands_count = $index - $total + 1;
            $output->writeln('  ' . $total . '. ' . str_pad($entry, 15) . " {$bin} {$entry} list");
        }
    }
    exit;
} else {
    if (is_null($plugin)) {
        $output->writeln('');
        $output->writeln("$name plugin not found");
        die;
    }
    if (!$plugin->enabled) {
        $output->writeln('');
        $output->writeln("$name not enabled");
        die;
    }
}
if ($plugin === null) {
    $output->writeln("Grav Plugin '{$name}' is not installed");
    exit;
}
$path = 'plugins://' . $name . '/cli';
try {
    $commands = Folder::all($path, ['compare' => 'Filename', 'pattern' => '/' . $pattern . '$/usm', 'levels' => 1]);
} catch (\RuntimeException $e) {
    $output->writeln("No Console Commands for '{$name}' where found in '{$path}'");
    exit;
}
foreach ($commands as $command_path) {
    $full_path = $grav['locator']->findResource("plugins://{$name}/cli/{$command_path}");
    require_once $full_path;
    $command_class = 'Grav\Plugin\Console\\' . preg_replace('/.php$/', '', $command_path);
    $command = new $command_class();
    $app->add($command);
}
$app->run($input);