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

use Illuminate\Foundation\Application;
use Symfony\Component\Console\Input\ArgvInput;

define('LARAVEL_START', microtime(true));

$autoloadPath = __DIR__.'/vendor/autoload.php';
if (! is_file($autoloadPath)) {
    fwrite(STDERR, "Klaxon dependencies are missing. Run composer install in this project first.\n");
    exit(1);
}
require $autoloadPath;

$arguments = array_slice($argv, 1);
if (count($arguments) === 1 && str_starts_with($arguments[0], '--team=')) {
    array_unshift($arguments, 'setup');
}
if (count($arguments) === 2 && $arguments[0] === '--team') {
    array_unshift($arguments, 'setup');
}
if ($arguments === []) {
    $input = [$argv[0], 'klaxon:execute', '--help'];
} elseif (($arguments[0] ?? null) === 'setup') {
    array_shift($arguments);
    $input = [$argv[0], 'klaxon:setup', ...$arguments];
} elseif (($arguments[0] ?? null) === 'kit') {
    array_shift($arguments);
    $input = [$argv[0], 'klaxon:kit', ...$arguments];
} elseif (($arguments[0] ?? null) === 'source-kit') {
    array_shift($arguments);
    $input = [$argv[0], 'klaxon:php-source-kit', ...$arguments];
} else {
    $input = [$argv[0], 'klaxon:execute', 'klaxon', ...$arguments];
}

/** @var Application $app */
$app = require_once __DIR__.'/bootstrap/app.php';
exit($app->handleCommand(new ArgvInput($input)));
