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

// if you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
//umask(0000);

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Doctrine\Common\Annotations\AnnotationRegistry;

$loader = require __DIR__ . '/../ext/vendor/autoload.php';

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

$input = new ArgvInput();
$dbname = $input->getParameterOption('--dbname', 'devprom');

putenv('DB_NAME='.$dbname);
define('SERVER_ROOT_PATH', dirname(dirname(__FILE__)) . '/');

$document_root = rtrim($input->getParameterOption('--document-root', $_SERVER['DOCUMENT_ROOT'] != '' ? $_SERVER['DOCUMENT_ROOT'] : SERVER_ROOT_PATH),'/\\').'/';
$kernel = trim($input->getParameterOption('--kernel', 'ServiceDeskAppKernel'));

include SERVER_ROOT_PATH . 'common.php';

// let's remove custom keys from ArgInput
foreach ($_SERVER['argv'] as $k => $v)
{
    if (preg_match('/\-\-dbname/', strtolower($v))) {
        unset($_SERVER['argv'][$k]);
    }

    if (preg_match('/\-\-document\-root/', strtolower($v))) {
        unset($_SERVER['argv'][$k]);
    }

    if (preg_match('/\-\-kernel/', strtolower($v))) {
        unset($_SERVER['argv'][$k]);
    }
}

// loading kernel
$input = new ArgvInput();

require_once __DIR__."/../app/Devprom/Component/HttpKernel/$kernel.php";

if ($kernel === 'PmApplicationKernel') {
    include_once SERVER_ROOT_PATH.'core/classes/SessionPortfolioAllProjects.php';
    include_once SERVER_ROOT_PATH.'co/classes/SessionBuilderCommon.php';
    include_once SERVER_ROOT_PATH.'pm/classes/sessions/SessionBuilderProject.php';
}

$className = "Devprom\\Component\\HttpKernel\\$kernel";
$kernel = new $className('prod', false);
$application = new Application($kernel);
$exitCode = $application->run($input);
exit($exitCode);
