#!/usr/bin/env php
<?php
/**
 * Shell wrapper for php_testability
 * @author Edson Medina <edsonmedina@gmail.com>
 */
use Commando\Command;
use edsonmedina\php_testability\Testability;

ini_set('html_errors', 'off');
ini_set('xdebug.max_nesting_level', 2000);
error_reporting (E_ALL); // that's how we roll

// load autoloader
$autoloader_locations = [
	__DIR__.'/../../../autoload.php', // from installed bin or src dir
	__DIR__.'/../vendor/autoload.php' // from dev version
];
$loaded = false;
foreach ($autoloader_locations as $file) 
{
    if (file_exists($file)) 
    {
    	require_once $file;
    	$loaded = true;
    	break;
    }
}
if (!$loaded) {
	die ("Can't find autoloader ".__DIR__."\n");
}

// parse CLI arguments
$cmd = new Command();
$cmd->setHelp ("PHP_Testability by Edson Medina.\n\nUsage: testability [options] [path]");
$cmd->option()->require()->referToAs('path')->describeAs('A file or a directory to analyse')->default(getcwd()); 
$cmd->option('output')->alias('o')->describeAs('Path for writing the report (default: report)')->default('./report');
$cmd->option('exclude')->alias('x')->describeAs('Exclude (comma-separated) directories (default: tests,.git,.svn,vendor,tmp,temp)')->default('tests,.git,vendor,tmp,temp');
$cmd->option('csvtotals')->describeAs('Use to output CSV files for total issue counts. This can be used with Jenkin\'s Plot plugin. These files are generated under each directory of the HTML report, with a total in the root.')->boolean();
$cmd->option('verbose')->alias('v')->describeAs('More verbose output.')->boolean();

// run
$path = rtrim(realpath($cmd[0]), DIRECTORY_SEPARATOR);

if (!file_exists($path)) {
	die ("\nInvalid path:\n".$path."\n\n");
}

$excludeDirs = preg_replace('/\s*,\s*/',',',$cmd['exclude']); // trim values

$testability = new Testability ($path, $cmd['output']);
$testability->setExcludeDirs   ($excludeDirs);
$testability->setCSV ($cmd['csvtotals']);
$testability->setVerbose ($cmd['verbose']);
$testability->runReport();
