#!/usr/bin/php
<?php
/*
  vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
  Codificación: UTF-8
  +----------------------------------------------------------------------+
  | Issabel version 4.0                                                  |
  | http://www.issabel.org                                               |
  +----------------------------------------------------------------------+
  | Copyright (c) 2006 Palosanto Solutions S. A.                         |
  +----------------------------------------------------------------------+
  | The contents of this file are subject to the General Public License  |
  | (GPL) Version 2 (the "License"); you may not use this file except in |
  | compliance with the License. You may obtain a copy of the License at |
  | http://www.opensource.org/licenses/gpl-license.php                   |
  |                                                                      |
  | Software distributed under the License is distributed on an "AS IS"  |
  | basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See  |
  | the License for the specific language governing rights and           |
  | limitations under the License.                                       |
  +----------------------------------------------------------------------+
  | The Initial Developer of the Original Code is PaloSanto Solutions    |
  +----------------------------------------------------------------------+
*/

require_once 'Console/Getopt.php';

if (!($documentRoot = getenv('ISSABEL_ROOT'))) $documentRoot="/var/www/html";
ini_set('include_path', $documentRoot.':'.ini_get('include_path'));
require_once("$documentRoot/libs/misc.lib.php");
require_once("$documentRoot/configs/default.conf.php");
require_once("$documentRoot/libs/paloSantoNotification.class.php");

//global variables framework
global $arrConf;

$opt = Console_Getopt::getopt($argv, '', array(
    'user=',        // (optional) User name or ID that receives notification
    'level=',       // Message level
    'resource=',    // (optional) Resource name or ID responsible for notification
    'message=',     // Message text
));

if (PEAR::isError($opt)) error_exit($opt->getMessage()."\n");

exit(insert_notification($opt) ? 0 : 1);

function error_exit($sMsg, $errorcode = 1)
{
    fwrite(STDERR, $sMsg);
    exit($errorcode);
}

function insert_notification($opt)
{
    $user = NULL;
    $level = 'info';
    $resource = NULL;
    $message = NULL;

    foreach ($opt[0] as $option) switch ($option[0]) {
        case '--user':
            $user = $option[1];
            break;
        case '--level':
            $level = $option[1];
            break;
        case '--resource':
            $resource = $option[1];
            break;
        case '--message':
            $message = $option[1];
            break;
    }

    if (is_null($message)) $message = stream_get_contents(STDIN);

    global $arrConf;
    $pNoti = new paloNotification($arrConf['issabel_dsn']['acl']);
    if (!empty($pNoti->errMsg)) error_exit("ERR: {$pNoti->errMsg}\n");

    if (!$pNoti->insertNotification($level, $message, $user, $resource)) {
        error_exit("ERR: {$pNoti->errMsg}\n");
        return FALSE;
    }
    return TRUE;
}
?>