#!/usr/bin/perl -w -I/opt/eprints/perl_lib

######################################################################
#
#  This file is part of GNU EPrints 2.
#  
#  Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 2 is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  EPrints 2 is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with EPrints 2; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

=pod

=head1 NAME

B<erase_fulltext_cache> - erase the cached full text search index files. 

=head1 SYNOPSIS

B<erase_fulltext_cache> I<repository_id> [B<options>]

=head1 DESCRIPTION

This erases all the .words and .indexcodes cache files from your repository, forcing the indexer to rerun the tools used to extract full text from your documents.

This is useful if you only setup the fulltext indexing after your repository is already live, or if you discover there has been a problem.

Without the B<--force> option, this script asks for confirmation before actually erasing anything.

=head1 ARGUMENTS

=over 8

=item I<repository_id> 

The ID of the EPrint repository to use.

=back

=head1 OPTIONS

=over 8

=item B<--help>

Print a brief help message and exit.

=item B<--man>

Print the full manual page and then exit.

=item B<--quiet>

Be vewwy vewwy quiet. This option will supress all output unless an error occurs.

=item B<--verbose>

Explain in detail what is going on.
May be repeated for greater effect.

=item B<--version>

Output version information and exit.

=item B<--force>

Don't ask before making the changes.

=back   

=head1 AUTHOR

This is part of this EPrints 2 system. EPrints 2 is developed by Christopher Gutteridge.

=head1 VERSION

EPrints Version: eprints-2-cvs-2006-06-27

=head1 CONTACT

For more information goto B<http://www.eprints.org/> which give information on mailing lists and the like.

Chris Gutteridge may be contacted at B<support@eprints.org>

Should you need a real world address for some reason, EPrints can be contacted in the real world at

 EPrints c/o Christopher Gutteridge
 Department of Electronics and Computer Science
 University of Southampton
 SO17 1BJ
 United Kingdom

=head1 COPYRIGHT

This file is part of GNU EPrints 2.

Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.

EPrints 2 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

EPrints 2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with EPrints 2; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


=cut



use Getopt::Long;
use Pod::Usage;
use strict;

use EPrints;

my $xml = 0;
my $version = 0;
my $verbose = 0;
my $quiet = 0;
my $force = 0;
my $purge = 1;
my $help = 0;
my $man = 0;

GetOptions( 
	'help|?' => \$help,
	'man' => \$man,
	'xml' => \$xml , 
	'force' => \$force , 
	'version' => \$version,
	'verbose+' => \$verbose,
	'silent' => \$quiet,
	'quiet' => \$quiet,
	'purge!' => \$purge
) || pod2usage( 2 );
EPrints::Utils::cmd_version( "erase_fulltext_cache" ) if $version;
pod2usage( 1 ) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage( 2 ) if( scalar @ARGV != 1 ); 

my $noise = 1;
$noise = 0 if( $quiet );
$noise = 1+$verbose if( $verbose );

# Set STDOUT to auto flush (without needing a \n)
$|=1;

my $session = new EPrints::Session( 1, $ARGV[0], $noise );
exit( 1 ) unless defined $session;
my $filename = $ARGV[1];

unless( $force )
{
	if( $noise > 0 )
	{
		print "You are about to erase all the fulltext cache files from\n";
		print "the $ARGV[0] repository.\n";
		print "This is not that dangerous, but will require the indexer\n";
		print "to rebuild all the caches, which could take a some time.\n";
	}
	my $sure = EPrints::Utils::get_input_confirm( "Continue" );
	unless( $sure )
	{
		print "Aborting then.\n\n";
		$session->terminate();
		exit;
	}
}

my $ds = $session->get_repository->get_dataset( "document" );
print "Stating to erase caches\n" if( $noise >= 1 );
my $info= {};
$info->{noise} = $noise;
$ds->map( $session, \&deal, $info );
print "Done erasing\n" if( $noise >= 1 );

$session->terminate();
print "Exiting normally.\n" if( $noise >= 2 );
exit;

sub deal 
{
	my( $session , $dataset , $doc, $info ) = @_;

	if( $info->{noise} >= 2 )
	{
		print "Removing cache for: ".$doc->get_id."\n";
	}
	
	my @files = ( $doc->words_file, $doc->indexcodes_file );
	foreach my $file ( @files )
	{
		next unless( -e $file );
		if( $info->{noise} >= 2 )
		{
			print "Erasing: $file\n";
		}
		unlink( $file );
	}

}
