/**
 * This function will look at the given program arguments and will validate them.
 * If the required arguments have been passed then it will call the Tag detector function,
 * otherwise it will stop the program.
 * This function is called at the end of this script (see last line of this file).
 * The help text below describes the arguments that are needed.
 */
function parseArgs()
{
    $rules = array('sourceDir|d=s' => 'Required. The base directory that the project resides in', 'resourceDbFileName|i=s' => 'Required. SQLite file that contains the project\'s files, classes, and methods. This file is created by ' . 'Triumph; Triumph performs INSERTs as it indexes a project.', 'outputDbFileName|o-s' => 'Optional. If given, the output will be written to the given file. If not given, output goes to STDOUT.', 'help|h' => 'This help message');
    $opts = new Zend_Console_Getopt($rules);
    // trim any quotes
    $sourceDir = trim($opts->getOption('sourceDir'), " \r\n\t'\"");
    $resourceDbFileName = trim($opts->getOption('resourceDbFileName'), " \r\n\t'\"");
    $outputDbFileName = trim($opts->getOption('outputDbFileName'), " \r\n\t'\"");
    $help = trim($opts->getOption('help'), " \r\n\t'\"");
    if ($help) {
        $helpMessage = <<<EOF
The goal of this script is to discover all of the 'dynamic' tags for any PHP projects that use the
CodeIgniter framework. In a nutshell, Triumph cannot provide Code completion for calling the
core CodeIgniter classes within a controller (ie "{$this->input}" will not trigger code completion").
This script will add he CI core objects and libraries to the Triumph tag cache so that 
Triumph can know about them during code completion. It will also
detect any user-created libraries as well. This script.
will be called via a command line; it is a normal command line script.

This script is part of Triumph's Tag Detection feature; it enables the editor to have a 
more useful list when performing code completion. More
info can be about Triumph's Tag detector feature can be found at 
http://docs.triumph4php.com/tag-detectors/

When a required argument is invalid or missing, the program will exit with an error code (-1)

EOF;
        echo $helpMessage;
        echo "\n";
        echo $opts->getUsageMessage();
        exit(-1);
    }
    if (!$sourceDir) {
        echo "Missing argument: --sourceDir. See --help for details.\n";
        exit(-1);
    }
    if (!is_dir($sourceDir)) {
        echo "sourceDir is not a valid directory. Is \"{$sourceDir}\" a directory? Is it readable? \n";
        exit(-1);
    }
    if (!$resourceDbFileName) {
        echo "Missing argument: --resourceDbFileName. See --help for details.\n";
        exit(-1);
    }
    if (!extension_loaded('pdo_sqlite') || !extension_loaded('PDO')) {
        echo "The script " . basename(__FILE__) . " requires the PDO and pdo_sqlite PHP extensions.";
        exit(-1);
    }
    // call the function that will return all detected URLs
    $doSkip = TRUE;
    $arrTags = detectTags($sourceDir, $resourceDbFileName, $doSkip);
    if ($doSkip) {
        echo "Detector " . basename(__FILE__, '.php') . " cannot detect tags for  {$sourceDir} ... skipping\n";
    } else {
        if ($outputDbFileName) {
            // now send the detected URLs to either STDOUT or store in the
            // sqlite DB
            $pdo = Zend_Db::factory('Pdo_Sqlite', array("dbname" => $outputDbFileName));
            $resourceTable = new Triumph_DetectedTagTable($pdo);
            $resourceTable->saveTags($arrTags, $sourceDir);
            echo "Tag detection complete, written to {$outputDbFileName}\n";
        } else {
            if (!empty($arrTags)) {
                echo str_pad("Type", 10) . str_pad("Class", 25) . str_pad("Member", 25) . str_pad("Return Type", 20) . "\n";
                foreach ($arrTags as $tag) {
                    echo str_pad(Triumph_DetectedTag::typeString($tag->type), 10);
                    echo str_pad($tag->className, 25);
                    echo str_pad($tag->identifier, 25);
                    echo str_pad($tag->returnType, 25);
                    echo "\n";
                }
            } else {
                echo "No tags were detected for {$sourceDir}\n";
            }
        }
    }
}