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

/**
 * This file is part of Gush.
 *
 * (c) Luis Cordova <cordoval@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

$paths[] = __DIR__.'/../vendor';
$paths[] = __DIR__.'/../../..';

$filename = null;

foreach ($paths as $path) {
    $filePath = $path.'/autoload.php';
    if (file_exists($filePath)) {
        $filename = $filePath;
        break;
    }
}

if (null === $filename) {
        echo "Cannot find an autoupload.php file, have you executed composer install command?\n";
        exit(1);
}
    
require_once $filename;

error_reporting(-1);
ini_set('display_errors', 1);

$process = new Symfony\Component\Process\Process('git --version', getcwd());
$process->run();

if (!$process->isSuccessful()) {
    throw new \RuntimeException('Git is required.');
}

$app = new Gush\Application();

// The first time the application runs it will configure the github credentials
// and create the cache folder
if (!file_exists(__DIR__.'/../.first-time-run')) {
    @file_put_contents(__DIR__.'/../.first-time-run', '');

    $config = Gush\Factory::createConfig();
    $localFilename = $config->get('home').'/.gush.yml';

    if (file_exists($localFilename)) {
        $yaml = new Symfony\Component\Yaml\Yaml();
        $parsed = $yaml->parse($localFilename);
        $config->merge($parsed['parameters']);
    }

    if (!$config->isValid()) {
        $app->run(new Symfony\Component\Console\Input\ArrayInput(['command' => 'core:configure']));
    }
}

$app->run();
