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

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use PHPCR\Util\Console\Command;

if (!class_exists(Application::class)) {
    if (is_file(__DIR__.'/../vendor/autoload.php')) {
        require __DIR__.'/../vendor/autoload.php';
    } elseif (is_file(__DIR__.'/../../../autoload.php')) {
        require __DIR__.'/../../../autoload.php';
    } else {
        echo 'Cannot find the vendor directory, have you executed composer install?'.PHP_EOL;
        echo 'See https://getcomposer.org to get Composer.'.PHP_EOL;
        exit(1);
    }
}

$configFile = getcwd() . DIRECTORY_SEPARATOR . 'cli-config.php';

$helperSet = null;
if (file_exists($configFile)) {
    if (!is_readable($configFile)) {
        trigger_error(
            "Configuration file [$configFile] does not have read permission.", E_USER_ERROR
        );
    }

    require $configFile;

    foreach ($GLOBALS as $helperSetCandidate) {
        if ($helperSetCandidate instanceof HelperSet) {
            $helperSet = $helperSetCandidate;
            break;
        }
    }
} else {
    trigger_error(
        "Configuration file [$configFile] does not exist. See https://github.com/doctrine/phpcr-odm/wiki/Command-line-tool-configuration", E_USER_ERROR
    );
}

$helperSet = $helperSet ?: new HelperSet();

$cli = new Application('PHPCR Command Line Interface', '0.1');

$cli->setCatchExceptions(true);
$cli->setHelperSet($helperSet);

$cli->addCommands([
    new Command\NodeDumpCommand(),
    new Command\NodeMoveCommand(),
    new Command\NodeRemoveCommand(),
    new Command\NodeTouchCommand(),
    new Command\NodesUpdateCommand(),
    new Command\NodeTypeListCommand(),
    new Command\NodeTypeRegisterCommand(),
    new Command\WorkspaceCreateCommand(),
    new Command\WorkspaceDeleteCommand(),
    new Command\WorkspaceExportCommand(),
    new Command\WorkspaceImportCommand(),
    new Command\WorkspacePurgeCommand(),
    new Command\WorkspaceQueryCommand(),
]);

$cli->run();

