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

namespace BHayes\CLI;

//to allow this to run any class by name as a global command, first check for a localized autoloader.
if (file_exists('vendor/autoload.php')) {
    include_once 'vendor/autoload.php';
}
//manually include the this version of CLI files in case it is also installed as a project package.
require_once __DIR__ . '/../php/UserResponse.php';
foreach (glob(__DIR__ . '/../php/CLI.php') as $file) {
    require_once $file;
}

if (php_sapi_name() != 'cli') {
    throw new \Error("This is a command line tool only.");
}

$subject = null;
if (count($argv) === 1) {
    echo "This CLI bin wrapper if given a classname can attempt to load and run the class for you.";
}

$potentialSubject = $argv[1] ?? null;
If ($potentialSubject && class_exists($potentialSubject)) {
    $subject = new $potentialSubject();
}
if ($potentialSubject && is_file($potentialSubject)) {

}

//cleanup the arg v before we run the subject if it was loaded via an argument
if ($subject) {
    unset($argv[1]);
    $argv = array_values($argv);
}

(new CLI($subject))->run();

