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

/**
 * Elefant CMS - http://www.elefantcms.com/
 *
 * Copyright (c) 2011 Johnny Broadway
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * This is the Elefant command line utility. It can run the Elefant
 * install routines, create the scaffolding for new apps or the index
 * for new translation files, or encrypt a password for you.
 *
 * Usage:
 *
 *     $ cd /path/to/my/site
 *     $ ./elefant COMMAND
 *
 * To set the ELEFANT_ENV environment variable and use a specific configuration,
 * call via:
 *
 *     $ ./elefant --env=production COMMAND
 */

// Prevent non-CLI access
if (! defined ('STDIN')) {
	echo "** Error: Must be run from the command line.\n";
	die;
}

// Make sure we're in an Elefant site root
if (getcwd () !== __DIR__) {
	chdir (__DIR__);
}

if (! file_exists ('conf/config.php')) {
	echo "** Error: Must be run from the root of an Elefant website.\n";
	die;
}

ini_set ('error_reporting', 247);

// Remove the script name
array_shift ($argv);

// Set ELEFANT_ENV based on --env=production parameter
if (isset ($argv[0]) && strpos ($argv[0], '--env=') === 0) {
	$env = str_replace ('--env=', '', $argv[0]);
	array_shift ($argv);
	$argc--;
} else {
	$env = 'config';
}

if (count ($argv) === 0) {
	// No command, use default handler
	$handler = 'cli/index';
} else {
	$handler = array_shift ($argv);
	if (strpos ($handler, '/') === false) {
		// Built-in command
		$handler = 'cli/' . $handler;
	} else {
		// Extended command, verify it's valid first
		list ($app, $tmp) = explode ('/', $handler, 2);
		if (! file_exists ('apps/' . $app . '/conf/cli.php')) {
			printf ("** Error: Unknown option: %s\n", $handler);
			return;
		}

		$parsed = parse_ini_file ('apps/' . $app . '/conf/cli.php');
		if (! in_array ($handler, array_keys ($parsed['commands']))) {
			printf ("** Error: Unknown option: %s\n", $handler);
			return;
		}
	}
}

// Run the command through Elefant's CLI handling
foreach ($argv as $k => $v) {
	$argv[$k] = escapeshellarg ($v);
}
system ('export ELEFANT_ENV=' . escapeshellarg ($env) . ' && php index.php ' . $handler . ' ' . join (' ', $argv));
