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

use Symfony\Component\Console\Application;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Finder\Finder;
use Vantoozz\PHPCDM\Defaults;
use Vantoozz\PHPCDM\DensityCommand;
use Vantoozz\PHPCDM\DensityMeter;
use Vantoozz\PHPCDM\Metadata;

$loaded = false;

foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
    if (file_exists($file)) {
        /** @noinspection PhpIncludeInspection */
        require_once $file;
        $loaded = true;
        break;
    }
}

if (false === $loaded) {
    exit(
        '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
    );
}

$command = new DensityCommand(
    new DensityMeter(Defaults::PAGE_WIDTH),
    new Finder
);

$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
    $event->getOutput()->writeln(PHP_EOL . Metadata::NAME . ' ' . Metadata::VERSION . ' by Ivan Nikitin' . PHP_EOL);
});

$application = new Application(Metadata::NAME, Metadata::VERSION);

$application->add($command);

$application->setDispatcher($dispatcher);

$application
    ->setDefaultCommand($command->getName(), true)
    ->run();