#!/usr/bin/env php
<?php
/**
 * This script adds data into the sesha inventory.
 *
 * Copyright 2012-2013 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (GPL). If you
 * did not receive this file, see http://www.horde.org/licenses/gpl.
 *
 * @author Ralf Lang <lang@b1-systems.de>
 */

if (file_exists(__DIR__ . '/../../sesha/lib/Application.php')) {
    $baseDir = __DIR__ . '/../';
} else {
    require_once 'PEAR/Config.php';
    $baseDir = PEAR_Config::singleton()
        ->get('horde_dir', null, 'pear.horde.org') . '/sesha/';
}
require_once $baseDir . 'lib/Application.php';
Horde_Registry::appInit('sesha', array('cli' => true));

// Read command line parameters.
if (count($argv) < 4 || count($argv) > 6) {
    $cli->message('Too many or too few parameters.', 'cli.error');
    usage();
}
list($script, $name, $categories, $description, $attributes) = $argv;
/* Currently we only support one category per item added with this script */
if (!is_array($categories)) {
    $categories = array($categories);
}

$driver = $GLOBALS['injector']->getInstance('Sesha_Factory_Driver')->create();

/* Find the categories */
$categoryList = $driver->getCategories(null, $categories);

if ($categoryList->count() == 0) {
    $GLOBALS['cli']->message(_('Could not find the requested categories'), 'cli.error');
    exit;
}

$properties = array();
foreach ($categoryList as $category) {
    foreach ($category->properties as $property) {
        $properties[] = $property;
    }
    print $category->hasRelation('properties', $properties[0]);
}

$cli->message($name, 'cli.success');

function usage()
{
    $GLOBALS['cli']->writeln('Usage: sesha-add-stock "name" "category" "description" "prop1:val1,prop2:val2"');
    exit;
}

