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

use duckpony\Console\Command\PurgeDatabaseCommand;
use duckpony\Console\Command\PurgeIssueFolderCommand;
use duckpony\Console\Command\PurgeIssueDatabaseCommand;
use duckpony\Console\Command\PurgeIssueServiceCommand;
use duckpony\Console\Command\RemoveOrphanedSymlinksCommand;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Application;

if (is_dir($vendor = __DIR__ . '/../vendor')) {
    require $vendor . '/autoload.php';
} elseif (is_dir($vendor = __DIR__ . '/../../..')) {
    require $vendor . '/autoload.php';
} else {
    die(
        'You must set up the project dependencies.' . PHP_EOL .
        'To do that, run the following commands:' . PHP_EOL . PHP_EOL .
        '$ curl -s http://getcomposer.org/installer | php' . PHP_EOL .
        '$ php composer.phar install' . PHP_EOL
    );
}

if (!class_exists(Application::class)) {
    die(
        'You must install the symfony/console package in order ' .
        'to use the command-line tool.' . PHP_EOL
    );
}

(static function () {
    /** @var ContainerInterface $container */
    $container = require __DIR__ . '/../config/container.php';

    $app = $container->get(Application::class);
    $app->add($container->get(PurgeIssueFolderCommand::class));
    $app->add($container->get(PurgeIssueServiceCommand::class));
    $app->add($container->get(RemoveOrphanedSymlinksCommand::class));
    $app->add($container->get(PurgeIssueDatabaseCommand::class));
    $app->add($container->get(PurgeDatabaseCommand::class));

    try {
        $app->run();
    } catch (Throwable $throwable) {
        $logger = $container->get(LoggerInterface::class);
        $logger->critical($throwable->getMessage(), ['exception' => $throwable]);
    }
})();
