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

if (!ini_get('date.timezone')) {
    ini_set('date.timezone', 'UTC');
}

foreach(['vendor',null] as $vendor_dir)
{

    if(defined('PHPUNIT_COMPOSER_INSTALL'))
    {
        continue;
    }

    $dir = [__DIR__];
    for($a=0;$a<=3;$a++)
    {
        $dir[]= '..';
        $file = implode(DIRECTORY_SEPARATOR, $dir);
        if($vendor_dir)
        {
            $file.= DIRECTORY_SEPARATOR . 'vendor';
        }

        $file.= DIRECTORY_SEPARATOR . 'autoload.php';

        echo $file .PHP_EOL;

        if (file_exists($file)) {
            define('PHPUNIT_COMPOSER_INSTALL', $file);
            break;
        }
    }
}

unset($file);

if (!defined('PHPUNIT_COMPOSER_INSTALL')) {
    fwrite(STDERR,
        'You need to set up the project dependencies using the following commands:' . PHP_EOL .
        'wget http://getcomposer.org/composer.phar' . PHP_EOL .
        'php composer.phar install' . PHP_EOL
    );
    die(1);
}

require PHPUNIT_COMPOSER_INSTALL;


use Symfony\Component\Console\Application;

$command = null;
if (count($_SERVER["argv"]) >= 2) {
    switch ($_SERVER["argv"][1]) {
        case "generate":
            $command = new JuliusHaertl\PHPDocToRst\GenerateDocumentationCommand();
            break;
        case "generate-ns":
            $command = new JuliusHaertl\PHPDocToRst\GenerateNamespaceDocumentationCommand();
            break;
        case "config":
            $command = new JuliusHaertl\PHPDocToRst\ConfigDocumentationCommand();
            break;
    }

    $application = new Application();
    $application->add($command);
    $application->run();
} 
