#!/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.
 */
    
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;

require_once __DIR__.'/vendor/autoload.php';

$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);
$app->setAutoExit(false);

foreach ($app->getCommands() as $command) {
    $output = new BufferedOutput();
    $input = new StringInput(sprintf('%s --help --format md', $command->getName()));

    $app->run($input, $output);

    $header = <<<EOT
---
layout: docu-page
full_title: "Gush: Rapid workflow for project maintainers and contributors"
---
{% block content %}
EOT;

    $footer = <<<EOT
{% endblock content %}
EOT;

    file_put_contents(
        'web/'.$command->getName().'.md',
        sprintf("%s\n%s\n%s", $header, $output->fetch(), $footer)
    );
}
