#!/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.
 */

require __DIR__.'/../src/bootstrap.php';

use Gush\Adapter;

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.');
}

$version = trim(explode(' ', $process->getOutput())[2]);
if (version_compare($version, '1.9.1', 'lt')) {
    throw new \RuntimeException('It is advisable to upgrade your version of GIT to 1.9.1 or latest.');
}

$adapterFactory = new Gush\Factory\AdapterFactory();

$adapters = [
    'github' => 'Gush\Adapter\GitHubFactory',
    'github_enterprise' => 'Gush\Adapter\GitHubEnterpriseFactory',
    'bitbucket' => 'Gush\Adapter\BitbucketFactory',
    'gitlab' => 'Gush\Adapter\GitLabFactory',
];

foreach ($adapters as $adapterName => $factoryClass) {
    if (!class_exists($factoryClass)) {
        continue;
    }

    $adapterFactory->registerAdapter(
        $adapterName,
        [$factoryClass, 'createAdapter'],
        [$factoryClass, 'createAdapterConfigurator']
    );
}

$issueTrackers = [
    'github' => 'Gush\Adapter\GitHubFactory',
    'github_enterprise' => 'Gush\Adapter\GitHubEnterpriseFactory',
    'bitbucket' => 'Gush\Adapter\BitbucketFactory',
    'gitlab' => 'Gush\Adapter\GitLabFactory',
    'jira' => 'Gush\Adapter\JiraFactory',
    'jira_enterprise' => 'Gush\Adapter\JiraEnterpriseFactory',
];

foreach ($issueTrackers as $trackerName => $factoryClass) {
    if (!class_exists($factoryClass)) {
        continue;
    }

    $adapterFactory->registerIssueTracker(
        $trackerName,
        [$factoryClass, 'createIssueTracker'],
        [$factoryClass, 'createIssueTrackerConfigurator']
    );
}

$app = new Gush\Application($adapterFactory);

// 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', '');

    try {
        $config = Gush\Factory::createConfig();
    } catch (Gush\Exception\FileNotFoundException $exception) {
        $app->run(new Symfony\Component\Console\Input\ArrayInput(['command' => 'core:configure']));
    }
}

$app->run();
