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

if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
} else {
    require_once __DIR__ . '/../../../autoload.php';
}

use DeltaCli\Command\CreateProjectConfig;
use DeltaCli\Command\SshKeyGen;
use DeltaCli\Exception\ConsoleOutputInterface;
use DeltaCli\Project;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Output\ConsoleOutput;

try {
    $output      = new ConsoleOutput();
    $application = new Application();
    $project     = new Project();

    $application->setCatchExceptions(false);
    $application->setName($project->getName());
    $application->add(new CreateProjectConfig($project));
    $application->add(new SshKeyGen());
    $application->addCommands($project->getScripts());
    $application->run(null, $output);
} catch (Exception $e) {
    $output->writeln('<error>' . get_class($e) . '</error>');

    if ($e instanceof ConsoleOutputInterface) {
        $e->outputToConsole($output);
    } else {
        $output->writeln($e->getMessage());
    }
}
