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

if (strpos(__DIR__, 'vendor') === false) {
    require __DIR__ . '/../vendor/autoload.php';
} else {
    // assume vendor to be on include_path if installed
    require 'vendor/autoload.php';
}

use Graviton\Deployment\Command\CloudFoundry\CheckApplicationCommand;
use Graviton\Deployment\Command\CloudFoundry\DeployCommand;
use Graviton\Deployment\Configuration;
use Graviton\Deployment\Deployment;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Console\Application;
use Symfony\Component\Process\ProcessBuilder;

$configuration = new Configuration(new Processor(), new FileLocator(getcwd() . '/app/config'));
$processBuilder = new ProcessBuilder();

$conf = $configuration->load();

if (!empty($conf['cf_process_timeout'])
    && $conf['cf_process_timeout'] === (int) $conf['cf_process_timeout']
) {
    $processBuilder->setTimeout($conf['cf_process_timeout']);
}

$deploymentHandler = new Deployment($processBuilder);

$application = new Application();
$application->add(new CheckApplicationCommand($configuration));
$application->add(new DeployCommand($deploymentHandler, $configuration));
$application->run();
