#!/usr/bin/php
<?php
/**
 * Executable for CLI functions of Zoph
 *
 * Zoph is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Zoph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @author Jeroen Roos
 * @package Zoph
 */

/**
 * Prepares the environment before switching to the CLI functions inside the webroot
 */

 /**
  * Change this if your zoph.ini is in another place. Just never, EVER put it inside your webroot,
  * or the entire Internet will know your database passwords
  */
define("INI_FILE", "/etc/zoph.ini");

define("CLI", true);
define("CLI_API", 4);


define("EXIT_NO_PROBLEM", 0);
define("EXIT_NO_ARGUMENTS", 1);
define("EXIT_NO_FILES", 2);

define("EXIT_IMAGE_NOT_FOUND", 10);
define("EXIT_PERSON_NOT_FOUND", 20);
define("EXIT_PLACE_NOT_FOUND", 30);
define("EXIT_ALBUM_NOT_FOUND", 40);
define("EXIT_CATEGORY_NOT_FOUND", 50);
define("EXIT_NOT_IN_CWD", 61);
define("EXIT_ILLEGAL_DIRPATTERN", 62);
define("EXIT_NO_PARENT", 80);

define("EXIT_INI_NOT_FOUND", 90);
define("EXIT_INSTANCE_NOT_FOUND", 91);

define("EXIT_CLI_USER_NOT_ADMIN", 95);
define("EXIT_CLI_USER_NOT_VALID", 96);

define("EXIT_API_NOT_COMPATIBLE", 99);

define("EXIT_CANNOT_ACCESS_ARGUMENTS", 250);
define("EXIT_UNKNOWN_ERROR", 254);


init();

/**
 * Initialize and handover to scripts inside webroot
 */
function init() {
    global $argv;

    $instance=getInstance();
    try {
        $ini=loadINI($instance);
    } catch (CliInstanceNotFoundException $e) {
        echo $e->getMessage() . "\n";
        exit(EXIT_INSTANCE_NOT_FOUND);
    } catch (CliININotFoundException $e) {
        echo $e->getMessage() . "\n";
        exit (EXIT_INI_NOT_FOUND);
    }

    require_once("log.inc.php");
    require_once("config.inc.php");
    require_once("autoload.inc.php");
    require_once("settings.inc.php");
    settings::parseINI($ini);
    
    try {
        require_once("include.inc.php");
    } catch (CliUserNotValidException $e) {
        echo $e->getMessage() . "\n";
        exit (EXIT_CLI_USER_NOT_VALID);
    }
    
    try {
        $cli=new cli($user, CLI_API, $argv);
        $cli->run();
    } catch (CliAPINotCompatibleException $e) {
        echo $e->getMessage() . "\n";
        exit (EXIT_API_NOT_COMPATIBLE);
    } catch (CliUserNotAdminException $e) {
        echo $e->getMessage() . "\n";
        exit (EXIT_CLI_USER_NOT_ADMIN);
    } catch (CliNoFilesException $e) {
        echo $e->getMessage() . "\n";
        exit (EXIT_NO_FILES);
    } catch (CliUnknownErrorException $e) {
        echo $e->getMessage() . "\n";
        exit (EXIT_UNKNOWN_ERROR);
    } catch (CliNotInCWDException $e) {
        echo $e->getMessage() . "\n";
        exit (EXIT_PATH_NOT_IN_CWD);
    } catch (CliNotInCWDException $e) {
        echo $e->getMessage() . "\n";
        exit (EXIT_PATH_NOT_IN_CWD);
    } catch (AlbumNotFoundException $e) { 
        echo $e->getMessage() . "\n";
        exit (EXIT_ALBUM_NOT_FOUND);
    } catch (CategoryNotFoundException $e) { 
        echo $e->getMessage() . "\n";
        exit (EXIT_CATEGORY_NOT_FOUND);
    } catch (PersonNotFoundException $e) { 
        echo $e->getMessage() . "\n";
        exit (EXIT_PERSON_NOT_FOUND);
    } catch (PlaceNotFoundException $e) { 
        echo $e->getMessage() . "\n";
        exit (EXIT_PLACE_NOT_FOUND);
    } catch (CliNoParentException $e) { 
        echo $e->getMessage() . "\n";
        exit (EXIT_NO_PARENT);
    } catch (CliIllegalDirpatternException $e) { 
        echo $e->getMessage() . "\n";
        exit (EXIT_ILLEGAL_DIRPATTERN);
    } 
    
    
}

/**
 * See if the user specified an instance on the CLI or just pick the first one
 */
function getInstance() {
    global $argv;
    
    if($argv) {
        foreach($argv as $arg) {
            if($arg == "-i" || $arg == "--instance") {
                // we are returning current, not next because foreach has
                // already advanced the pointer
                return(current($argv));
            }
        }
    }
    return null;
}

/**
 * Load the right instance from the INI file
 * @param string Name of the Zoph instance to be used
 */
function loadINI($instance) {
    if(!defined("INI_FILE")) {
        define("INI_FILE", "/etc/zoph.ini");
    }
    if(file_exists(INI_FILE)) {
        $ini=parse_ini_file(INI_FILE, true);
        if(!empty($instance)) {
            if(isset($ini[$instance])) {
                $ini=$ini[$instance];   
            } else {
                throw new CliInstanceNotFoundException("Instance " . $instance . " not found in " . INI_FILE);
            }
        } else {
            // No instance given, taking the first
            $ini=array_shift($ini);
        }
    } else {
        throw new CliININotFoundException(INI_FILE . " not found.");
    }
    set_include_path(get_include_path() . PATH_SEPARATOR . $ini["php_location"]);
    return $ini;
}
?>
