#!/usr/bin/env php
<?php
if (file_exists(__DIR__ . '/../../../autoload.php')) {
    require_once __DIR__ . '/../../../autoload.php';
} else {
    require_once __DIR__.'/../vendor/autoload.php';
}

try {
    $opts = getopt('a::h::p::r::', array('all::', 'help::', 'path::', 'repo::'));

    // check if there's a repo to import
    $repo = isset($opts['r']) ? $opts['r'] : isset($opts['repo']) ? $opts['repo'] : false;

    // path to project (can be passed by param, or defaults to pwd
    $path = isset($opts['p']) ? $opts['p'] : isset($opts['path']) ? $opts['path'] : false;
    $path = $path ?: getcwd();

    if ($repo !== false) {
        exec("git clone $repo $path");
    }

    // bootstrap
    $config = new Cauditor\Config($path, $path.DIRECTORY_SEPARATOR.'.cauditor.yml');
    $analyzer = new Cauditor\Analyzers\PDepend($config);
    $api = new \Cauditor\Api('https://www.cauditor.org');

    if (isset($opts['h']) || isset($opts['help'])) {
        $runner = new \Cauditor\Runners\Help();
    } elseif (isset($opts['a']) || isset($opts['all'])) {
        $runner = new \Cauditor\Runners\All($config, $api, $analyzer);
    } else {
        $runner = new \Cauditor\Runners\Current($config, $api, $analyzer);
    }

    $runner->execute();
} catch (Exception $e) {
    exit(1);
}
