#!/usr/bin/env php
<?php
/* (c) Anton Medvedev <anton@elfet.ru>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

$loaded = false;

foreach (array(__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php') as $file) {
    if (file_exists($file)) {
        require $file;
        $loaded = true;
        break;
    }
}

if (!$loaded) {
    die(
        'You need to set up the project dependencies using the following commands:' . PHP_EOL .
        'wget http://getcomposer.org/composer.phar' . PHP_EOL .
        'php composer.phar install' . PHP_EOL
    );
}

// Recipe include path
set_include_path(__DIR__ . '/../' . PATH_SEPARATOR . get_include_path());

// Init Deployer
$deployer = new \Deployer\Deployer(
    new \Symfony\Component\Console\Application('Deployer', '2.0.2'),
    new \Symfony\Component\Console\Input\ArgvInput(),
    new \Symfony\Component\Console\Output\ConsoleOutput()
);

// Require current deploy.php script
$deployFile = getcwd() . '/deploy.php';

if (is_file($deployFile) && is_readable($deployFile)) {
    require $deployFile;
}

// Self-update command
$selfUpdate = new \KevinGH\Amend\Command('self-update');
$selfUpdate->setDescription('Updates deployer.phar to the latest version');
$selfUpdate->setManifestUri('http://deployer.org/manifest.json');
$deployer->getConsole()->add($selfUpdate);
$deployer->getHelperSet()->set(new \KevinGH\Amend\Helper());

// Run Deployer
$deployer->run();
