<?php

use EasySwoole\EasySwoole\Command\CommandRunner;
use EasySwoole\Command\Caller;

$file = null;
foreach ([ __DIR__ . '/../../../autoload.php', __DIR__ . '/../../vendor/autoload.php',__DIR__ . '/../vendor/autoload.php' ] as $file) {
    if (file_exists($file)) {
        require $file;
        break;
    }
}
if(!file_exists($file)){
    die("include composer autoload.php fail\n");
}
$realCwd = substr(realpath($file),0,-strlen("/vendor/autoload.php"));

defined('IN_PHAR') or define('IN_PHAR', boolval(\Phar::running(false)));
defined('RUNNING_ROOT') or define('RUNNING_ROOT', $realCwd);
defined('EASYSWOOLE_ROOT') or define('EASYSWOOLE_ROOT', IN_PHAR ? \Phar::running() : $realCwd);

if(file_exists(EASYSWOOLE_ROOT.'/bootstrap.php')){
    require_once EASYSWOOLE_ROOT.'/bootstrap.php';
}

$caller = new Caller();
$caller->setScript(current($argv));
$caller->setCommand(next($argv));
$caller->setParams($argv);
reset($argv);

$ret = CommandRunner::getInstance()->run($caller);
if($ret && !empty($ret->getMsg())){
    echo $ret->getMsg()."\n";
}
