#!/usr/bin/env php
<?php
/**
 * Created by PhpStorm.
 * User: janhuang
 * Date: 15/3/19
 * Time: 下午8:03
 * Github: https://www.github.com/janhuang
 * Coding: https://www.coding.net/janhuang
 * SegmentFault: http://segmentfault.com/u/janhuang
 * Blog: http://segmentfault.com/blog/janhuang
 * Gmail: bboyjanhuang@gmail.com
 */

set_time_limit(0);
date_default_timezone_set('PRC');

// autoload composer
foreach ([
             __DIR__ . '/../../autoload.php',
             __DIR__ . '/../vendor/autoload.php',
             __DIR__ . '/vendor/autoload.php'
         ] as $value) {
    if (file_exists($value)) {
        define('FASTD_COMPOSER_INSTALL', $value);
        break;
    }
}

if (!defined('FASTD_COMPOSER_INSTALL')) {
    fwrite(STDERR,
        'You need to set up the project dependencies using the following commands:' . PHP_EOL .
        'wget http://getcomposer.org/composer.phar' . PHP_EOL .
        'php composer.phar install' . PHP_EOL
    );
}

include FASTD_COMPOSER_INSTALL;

if (!class_exists('Application')) {
    foreach ([
                 __DIR__ . '/application.php',
                 __DIR__ . '/app/application.php',
                 __DIR__ . '/../application.php',
                 __DIR__ . '/../app/application.php',
                 __DIR__ . '/../../application.php',
                 __DIR__ . '/../../app/application.php',
                 __DIR__ . '/../../../application.php',
                 __DIR__ . '/../../../app/application.php',
             ] as $value) {
        if (file_exists($value)) {
            include $value;
            break;
        }
    }
}

use FastD\Console\Input\ArgvInput;
use FastD\Framework\Kernel\AppConsole;
use FastD\Framework\Kernel\AppKernel;
use FastD\Console\Input\InputDefinition;

$argvInput = new ArgvInput(null, new InputDefinition);

$env = $argvInput->hasOption(['env', 'e']) ? $argvInput->getOption(['env', 'e']) : AppKernel::ENV_DEV;

$console = new AppConsole(new Application($env));

$console->run($argvInput);
