#!/usr/bin/env php
<?php
/**
 * This script imports VTODO data into Nag tasklists.
 * The data is read from standard input, the tasklist and user name passed as
 * parameters.
 *
 * Copyright 2005-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 Jan Schneider <jan@horde.org>
 */

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

// Read command line parameters.
if (count($argv) != 3) {
    $cli->message('Too many or too few parameters.', 'cli.error');
    usage();
}
$tasklist = $argv[1];
$user = $argv[2];

// Read standard input.
$vtodo = $cli->readStdin();
if (empty($vtodo)) {
    $cli->message('No import data provided.', 'cli.error');
    usage();
}

// Set user.
$registry->setAuth($user, array());

// Import data.
try {
    $result = $registry->tasks->import($vtodo, 'text/calendar', $tasklist);
} catch (Horde_Exception $e) {
    $cli->fatal($result->toString());
}
$cli->message('Imported successfully ' . count($result) . ' tasks', 'cli.success');

function usage()
{
    $GLOBALS['cli']->writeln('Usage: nag-import-vtodos tasklist user');
    exit;
}

