#!/usr/bin/env php
<?php
if (PHP_SAPI !== 'cli') {
	echo "You must use the command line to run this script.\n";
	die(1);
}

$dir = getcwd();
$files = [
	// Own dependencies
	__DIR__ . '/../../autoload.php',
	__DIR__ . '/../vendor/autoload.php',
	__DIR__ . '/vendor/autoload.php',
	// Elgg installation (in case elgg-cli is installed globally)
	$dir . '/../../autoload.php',
	$dir . '/../vendor/autoload.php',
	$dir . '/vendor/autoload.php',
];

foreach ($files as $file) {
	if (file_exists($file)) {
		require_once $file;
	}
}

if (!class_exists('\Elgg\Application') || !class_exists('\Symfony\Component\Console\Application')) {
	fwrite(STDERR, "Composer dependencies are not installed "
			. "or you are trying to run the script outside of an Elgg installation's root directory.\n");
	die(2);
}

$json = file_get_contents(__DIR__ . "/package.json");
$app = json_decode($json);

if (!$app) {
	fwrite(STDERR, "Can not read package.json.\n");
	die(3);
}

$cli = new \Symfony\Component\Console\Application($app->name, $app->version);
(new \Elgg\CLI($cli))->run();
