#!/usr/bin/php4 -q
<?php

/*************************************************************
 
This script will rebuild a site's index from scratch. This is
useful if the index becomes off because the files were edited
directly without going through the bamboo interface or because
a bamboo site is contained within another.

USAGE:
   reindex /var/www/mysite

*************************************************************/

$base = dirname(dirname(__FILE__));

require_once("$base/debug.php");
require_once("$base/Lang.php");
require_once("$base/Page.php");
require_once("$base/PageStore.php");
require_once("$base/Properties.php");
require_once("$base/Search.php");

ini_set('error_log', NULL);

$argv = &$GLOBALS['argv'];
$argc = &$GLOBALS['argc'];

if ($argc != 2)
	die("Usage: reindex [site dir]\n");

$src = $argv[1];
$_SERVER['DOCUMENT_ROOT']=$src;

if (!is_dir($src))
	die("site directory $src does not exist\n");

if (!is_file("$src/b.site")) 
	die("can't find b.site file in $src\n");
	
###############################################

$prop     = new Properties("$src/b.site");

Lang::init( $prop->getGlobal('default-lang'), split(' ',$prop->getGlobal('languages')) );
Lang::set( isset($GLOBALS['language']) ? $GLOBALS['language'] : 'en' );

$root 	  = $prop->getGlobal('siteroot');
$docroot  = $prop->getGlobal('sitedocroot');
$ps       = PageStoreFactory::create($prop->getGlobal('pagebackend'),$prop);
$page     = $ps->getPage('/');
$search   = new Search(IndexFactory::create($prop->getGlobal('indexbackend')));

$search->clear();
$search->indexTree($page);

return;
?>
