phad

#!/usr/bin/env php  
<?php  
/**  
 * This cli is used to setup bin scripts
 *  
 *  @usage vendor/bin/tlf-cli help
 *  @usage vendor/bin/tlf-cli setup
 *  
 */  
  
$own_autloader = dirname(__DIR__).'/vendor/autoload.php'; 
$their_autoloader = dirname(__DIR__, 3).'/autoload.php'; // presuming vendor/taeluf/cli/bin
//$pwd_autoloader = getcwd().'/vendor/autoload.php';
//if (file_exists($pwd_autoloader))require_once($pwd_autoloader); else
if (file_exists($their_autoloader))require_once($their_autoloader);
else if (file_exists($own_autloader))require_once($own_autloader);
else {
    echo "\nautoload file not found.\n";
    return;
}
 
$cli = new \Tlf\Cli();  
$cli->exclude_from_help[] = 'main';
$cli->exclude_from_help[] = 'help';
// load_json_file fails silently if the file does not exist  
$cli->load_json_file(__DIR__.'/../src/defaults.json');   
$cli->load_json_file(getcwd().'/.config/phad.json');   
$cli->load_json_file(getcwd().'/config/phad.json');   
//$cli->load_inputs(json_decode(file_get_contents(__DIR__.'/user_configs.json'),true));  
$cli->load_stdin();  
  
$cli->load_command('main',  
    function($cli, $args){  
        $cli->call_command('help',[]);
    }, 'Show this help menu'  
);  
  
$cli->load_command('setup',   
    function($cli, $args){  
        $path = '/.config/phad.json';
        $file = getcwd().$path;
        $sample_config = __DIR__.'/../src/defaults.json';
        $write_file = false;
        if (file_exists($file)){
            $write_file = $cli->ask("Overwrite config file '{$path}'");
        } else {
            $write_file = $cli->ask("Create config file '{$path}'");
        }

        if ($write_file)file_put_contents($file, file_get_contents($sample_config));
        else return;

    }, "Create a config file for Phad."  
);  
  
$cli->execute();