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

$files = array(
    __DIR__ . '/../../../../vendor/autoload.php',
    __DIR__ . '/../vendor/autoload.php'
);

$projectRoot = null;
foreach ($files as $file) {
    if (file_exists($file)) {
        require $file;
        $projectRoot = dirname(dirname(realpath($file)));
        break;
    }
}

if (empty($projectRoot)) {
    die(
      'You need to set up the project dependencies using the following commands:' . PHP_EOL .
      'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
      'php composer.phar install' . PHP_EOL
    );
}

$shortParams = array('v', 'V', 'h', 'q');
$cliParameters = getopt(join($shortParams), array('max-script-instance:'));
foreach ($cliParameters as $k => $v) {
    if (in_array($k, $shortParams)) {
        $cliParameters[$k] = true;
    }
}
$args = array_filter($argv, function($v) {
    return substr($v, 0, 1) != '-'; // remove switches
});
array_shift($args); // remove script name
$command = null;
if (count($args) > 0) {
    $command = array_shift($args);
}
$commandOptions = $args;

$runner = new \Cubes\DbSmart2\CliRunner($cliParameters);
$runner->run($projectRoot, $command, $commandOptions);
