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

use Castor\Console\ApplicationFactory;

$argv = $_SERVER['argv'];

if (count($argv) > 2 && $argv[1] === 'run-phar') {
    $phar = $argv[2];

    if (!file_exists($phar)) {
        echo "Phar file not found: $phar\n";
        exit(1);
    }

    // override the argv
    $_SERVER['argv'] = array_merge(
        array($argv[0]),
        array_slice($argv, 3)
    );
    $_SERVER['argc'] = count($_SERVER['argv']);

    require $phar;

    exit(0);
}

if (file_exists($file = __DIR__ . '/../vendor/autoload.php')) {
    require $file;
} elseif (file_exists($file = __DIR__ . '/../../../../vendor/autoload.php')) {
    require $file;
} else {
    throw new \RuntimeException('Unable to find autoloader.');
}

ApplicationFactory::create()->run();
