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

function includeIfExists($file) {
    return file_exists($file) ? include $file : false;
}
if (
    (!$loader = includeIfExists(__DIR__.'/../vendor/autoload.php')) &&
    (!$loader = includeIfExists(__DIR__.'/../../../autoload.php'))
   ) {
    echo 'You must set up the project dependencies using `composer install`'.PHP_EOL.
        'See https://getcomposer.org/download/ for instructions on installing Composer'.PHP_EOL;
    exit(1);
}

use AtomicDeploy\Client\Config;
use AtomicDeploy\Client\Vendor\LFTPVendorUpdater;
use AtomicDeploy\Client\Console\Application;
use AtomicDeploy\Client\Console\CleanupCommand;
use AtomicDeploy\Client\Console\ComposerTransferInstalledCommand;
use AtomicDeploy\Client\Console\GitFtpCommand;
use AtomicDeploy\Client\Console\ListCommand;
use AtomicDeploy\Client\Console\PushCommand;
use AtomicDeploy\Client\Console\RunCommand;
use AtomicDeploy\Client\Console\UseCommand;

date_default_timezone_set('UTC');

$dotenv = new Dotenv\Dotenv(getcwd());
$path = getcwd() . '/.env';
if(file_exists($path)) {
    echo 'Loading environment configuration from ' . $path . "\n";
    $dotenv->load();
}

$config = new Config(require getcwd() . '/deploy.php');

$application = new Application($config, 'AtomicDeploy Client', '0.1.0');
$application->add(new GitFtpCommand());
$application->add(new ComposerTransferInstalledCommand(new LFTPVendorUpdater($config)));
$application->add(new PushCommand());
$application->add(new ListCommand());
$application->add(new UseCommand());
$application->add(new RunCommand());
$application->add(new CleanupCommand());
$application->run();
