#!/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);

set_time_limit(0);

$basedir = dirname($_SERVER['SCRIPT_FILENAME']) . DIRECTORY_SEPARATOR . '..';

require_once $basedir . '/app/bootstrap.php.cache';
require_once $basedir . '/app/AppKernel.php';

use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Debug\Debug;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\Console\Helper\FormatterHelper;

$input = new ArgvInput();

$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';

if ($debug) {
    Debug::enable();
}

$kernel = new AppKernel($env, $debug);
$kernel->boot();

try {
    $application = $kernel->getContainer()->get('hoathis.console');
    $application->setVersion($application->getVersion() . PHP_EOL . '  <info>with HoathisConsoleBundle</info>');

    $application->run($input, $kernel->getContainer()->get('hoathis.console.output'));
} catch (ServiceNotFoundException $exception) {
    $output = new ConsoleOutput();
    $formatter = new FormatterHelper();
    
    $output->writeln($formatter->formatBlock(
        array(
            'It seems you forgot to register HoathisConsoleBundle!',
            'Please add the required line to your AppKernel.php file.',
            '',
            'Previous exception:',
            '',
            sprintf('[%s]', get_class($exception)),
            $exception->getMessage()
        ),
        'error',
        true
    ));

    exit($exception->getCode() ?: 1);
}
