#!/usr/bin/php
<?php
/**
 * @package Newscoop
 *
 * @author Holman Romero <holman.romero@sourcefabric.org>
 * @author Mugur Rus <mugur.rus@sourcefabric.org>
 * @copyright 2007 MDLF, Inc.
 * @license http://www.gnu.org/licenses/gpl.txt
 * @version $Revision$
 * @link http://www.campware.org
 */


function out_msg($p_msg, $p_ifVerboseSet = false, $p_newLine = true)
{
    global $g_silent, $g_verbose;
    if (!$g_silent && ($g_verbose || !$p_ifVerboseSet)) {
        echo "$p_msg" . ($p_newLine ? "\n" : '');
    }
}


function exit_error($p_errMsg, $p_ifVerboseSet = false)
{
    out_msg($p_errMsg, $p_ifVerboseSet);
    exit(1);
}


function usage()
{
    out_msg("Usage: newscoop-indexer [options]\n"
            ."Options:\n"
            ."\t--conf_dir=[configuration_dir]: Host name of the MySQL Server.\n"
            ."\t--silent: The script will not display any message\n"
            ."\t\t(has precedence over --verbose).\n"
            ."\t--verbose: The script will display detailed information.\n"
            ."\t--time-limit: Interrupt the indexing after the specified number\n"
            ."\t\tof seconds.\n"
            ."\t--help: Display this information.");
} // fn usage


$g_silent = false;
$g_verbose = false;
$g_time_limit = null;
$conf_dir = '';

// gets the arguments from command line, if any
for ($i = 1; isset($GLOBALS['argv'][$i]); $i++) {
    $option = explode('=', $GLOBALS['argv'][$i]);
    switch ($option[0]) {
    	case '--conf_dir':
    		$conf_dir = !empty($option[1]) ? rtrim($option[1], '/') : '';
    		if (empty($conf_dir)) {
    			out_msg("Configuration directory not specified!");
    			usage();
    			exit(1);
    		}
    		break;
    	case '--silent':
    		$g_silent = true;
    		break;
    	case '--verbose':
    		$g_verbose = true;
    		break;
    	case '--help':
    		usage();
    		exit(0);
        case '--time-limit':
            $g_time_limit = !empty($option[1]) ? (int)$option[1] : null;
            if (empty($g_time_limit)) {
                out_msg("Time limit value not specified!");
                usage();
                exit(1);
            }
            break;
    	default:
    		out_msg("Invalid option '" . $option[0] . "'!");
    		usage();
    		exit(1);
    }
}
$g_verbose = !$g_silent && $g_verbose;
if (empty($conf_dir)) {
    $bin_dir = dirname(__FILE__);
    $conf_dir = preg_replace('/bin$/', 'conf', $bin_dir);
}
if (!is_dir($conf_dir)) {
    exit_error('Invalid configuration directory ' . $conf_dir);
}

$GLOBALS['g_campsiteDir'] = dirname($conf_dir);

require_once($GLOBALS['g_campsiteDir'].'/include/campsite_init.php');
require_once($GLOBALS['g_campsiteDir'].'/classes/ArticleIndex.php');

$res = ArticleIndex::RunIndexer($g_time_limit);
if (PEAR::isError($res)) {
    if (!$g_silent) {
        out_msg($res->getMessage());
    }
    exit(1);
}

if (!$g_silent) {
    out_msg($res['articles'] . ' out of ' . $res['total articles']
    . ' articles were indexed with a total of '
    . $res['words'] . " words.\nTotal index time was "
    . sprintf("%.3f",  $res['total time'])
    . " seconds.\nAverage article index time was "
    . sprintf("%.3f", $res['article time']) . ' seconds.'
    );
}

?>
