#!/usr/bin/php
<?php

/**
 * This file triggers a plugin to execute
 *
 * This file is called via command line interface (cli)
 *
 * Argument 1 (argv[1]):  Unique identifier of container 
 *                 or
 * Argument 1 (argv[1]):  Name of plugin class (should only be used if there exists only one plugin of this type)
 *
 * Argument 2 (argv[2]): Optionname=Optionvalue (e.g. ExternalAlarm=PowerOff)
 * ... 
 */

//$incPath = ini_get("include_path");
//ini_set("include_path", $incPath . ":/srv/www/htdocs/webinterface");
chdir("/var/www");


require_once 'config.inc.php';


$GLOBALS['TaskPluginDir'] = 'class/tasks/';
$GLOBALS['ClassDir']      = 'class/';
$GLOBALS['LibDir']      = 'libnew/';

require_once $GLOBALS['TaskPluginDir'].'class.container.base.php';
//includePluginDefinitions();
require_once "libnew/class.config.php";
require_once "libnew/class.object.php";
require_once "libnew/class.common.php";
require_once $GLOBALS['TaskPluginDir'].'class.task.sched.php';



function main($argc, &$argv)
{
    $stderr = fopen('php://stderr', 'w');
    if ($argc < 3) {
        fwrite($stderr, "Wrong arguments.\nUsage: ".$argv[0]." UNIQUE_CONTAINER_ID or PLUGIN_CLASSNAME OPTION=VALUE [...]\n");
        exit(1);   // args!
    }
    
    $cid   = intval($argv[1]);
    $cname = NULL;
    if (!$cid) {
        // argv[1] is the name of the plugin class
        $cname = $argv[1];
    }
    
    $argIdx = 2;
    $options = "";
    while (isset($argv[$argIdx])) {        
        if (!is_array($options)) {
            $options = array();
        }
        $option = explode("=", $argv[$argIdx]);
        if (is_array($option) && count($option) == 2) {
            $options[$option[0]] = $option[1];
        } else {
            fwrite($stderr, "Wrong type of arguments (arg".$argIdx.": ".$argv[$argIdx].").\nUsage: ".$argv[0]." UNIQUE_CONTAINER_ID or PLUGIN_CLASSNAME OPTION=VALUE [...]\n");
            exit(1);
        }
        $argIdx++;
    }
    
    if (empty($cid) && empty($cname)) {
        fwrite($stderr, "Wrong type of arguments (arg1: ".$argv[1].").\nUsage: ".$argv[0]." UNIQUE_CONTAINER_ID or PLUGIN_CLASSNAME OPTION=VALUE [...]\n");
        exit(1);   // args!
    } else {
        $db = DBAccess::getSqlObjUI();
        
        if (empty($cid)) {
            $plugins = Plugin_Factory::getSavedContainerList($db);
            if (is_array($plugins)) {
                while (list($id, $class) = each($plugins)) {
                    if ($class == $cname)  {
                        $cid = $id;
                        break;
                    }
                }
            } else {
                fwrite($stderr, "No Plugins or search error\n");
                exit(2);
            }
        }
        
        if (!empty($cid)) {
            $class     =  Plugin_Container::typeOfDbContainer($cid, $db);
            includePluginDefinitions($class);                   // Include class definitions for plugin class        
            
            $container =& Plugin_Factory::createSavedContainer($cid, $db);
            if (is_subclass_of($container, 'Plugin_Container')) {
                // Execute plugin
                $lang = 'en';
                if (isset($container->userdata['user_lang'])) {
                    $lang = $container->userdata['user_lang'];                
                }
                $container->setLanguage($lang);
                $rc = $container->execute($options);
                if ($rc > 0) {
                    fwrite($stderr, "Error on execute: ".$container->plugin->getLastError()."\n");
                } else if ($rc < 0) {
                    fwrite($stderr, "Error on execute: ".$container->getLastError()."\n");
                }
            } else {
                fwrite($stderr, "Container could not be created\n".$container."\n");
            }
            
        } else {
            fwrite($stderr, "Plugin not found\n");
            exit(2);
        }        
    }
    
}



main($argc, $argv);



?>