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

/*
 * This file is part of the Moodle Plugin CI package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

use Dotenv\Dotenv;
use Moodlerooms\MoodlePluginCI\Command\AddConfigCommand;
use Moodlerooms\MoodlePluginCI\Command\AddPluginCommand;
use Moodlerooms\MoodlePluginCI\Command\BehatCommand;
use Moodlerooms\MoodlePluginCI\Command\CodeCheckerCommand;
use Moodlerooms\MoodlePluginCI\Command\CodeFixerCommand;
use Moodlerooms\MoodlePluginCI\Command\CompletionCommand;
use Moodlerooms\MoodlePluginCI\Command\CopyPasteDetectorCommand;
use Moodlerooms\MoodlePluginCI\Command\CoverallsUploadCommand;
use Moodlerooms\MoodlePluginCI\Command\CSSLintCommand;
use Moodlerooms\MoodlePluginCI\Command\InstallCommand;
use Moodlerooms\MoodlePluginCI\Command\JSHintCommand;
use Moodlerooms\MoodlePluginCI\Command\MessDetectorCommand;
use Moodlerooms\MoodlePluginCI\Command\ParallelCommand;
use Moodlerooms\MoodlePluginCI\Command\PHPLintCommand;
use Moodlerooms\MoodlePluginCI\Command\PHPUnitCommand;
use Moodlerooms\MoodlePluginCI\Command\ShifterCommand;
use Moodlerooms\MoodlePluginCI\Command\ValidateCommand;
use Symfony\Component\Console\Application;

// Global install.
if (file_exists(__DIR__.'/../../../autoload.php')) {
    /** @noinspection PhpIncludeInspection */
    require_once __DIR__.'/../../../autoload.php';
// Project install.
} elseif (file_exists(__DIR__.'/../vendor/autoload.php')) {
    require_once __DIR__.'/../vendor/autoload.php';
} else {
    fwrite(STDERR, 'Failed to find Composer\'s autoload file. You might need to run Composer\'s install on this project'.PHP_EOL);
    die(1);
}

define('ENV_DIR', dirname(__DIR__));
define('ENV_FILE', ENV_DIR.'/.env');

if (file_exists(ENV_FILE)) {
    // Use this file because PHP cannot write to the environment.
    // These are used to allow the install command to relay important
    // information to the other commands so they can run with no args.
    $env = new Dotenv(ENV_DIR);
    $env->load();
}

$application = new Application('Moodle Plugin CI', '1.5.2');
$application->add(new AddConfigCommand());
$application->add(new AddPluginCommand(ENV_FILE));
$application->add(new BehatCommand());
$application->add(new CodeCheckerCommand());
$application->add(new CodeFixerCommand());
$application->add(new CompletionCommand());
$application->add(new CopyPasteDetectorCommand());
$application->add(new CoverallsUploadCommand());
$application->add(new CSSLintCommand());
$application->add(new InstallCommand(ENV_FILE));
$application->add(new JSHintCommand());
$application->add(new MessDetectorCommand());
$application->add(new ParallelCommand());
$application->add(new PHPLintCommand());
$application->add(new PHPUnitCommand());
$application->add(new ShifterCommand());
$application->add(new ValidateCommand());
$application->run();
