#!/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_archive> - Erase all EPrints and rebuilt the eprints SQL tables.

=head1 SYNOPSIS

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

=head1 DESCRIPTION

This script completely erases the EPrints stored in the repository. It 
erases all the documents and completely deleteds then recreates the
SQL tables.

This is similar to erase_archive followed by create_tables, but it
does not effect subjects or users so is useful when tweaking the
metadata fields of the eprints dataset, prior to going public.

This script will also erase the website, so a new generate_static,
etc. will be required.

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

=head1 ARGUMENTS

=over 8

=item B<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 EPrints;

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

my $version = 0;
my $verbose = 0;
my $quiet = 0;
my $force = 0;
my $help = 0;
my $man = 0;
my $rootpass;
my $erasedb = 1;
my $erasefiles = 1;

GetOptions( 
	'help|?' => \$help,
	'man' => \$man,
	'force' => \$force , 
	'rootpass=s', \$rootpass,
	'version' => \$version,
	'verbose+' => \$verbose,
	'silent' => \$quiet,
	'quiet' => \$quiet
) || pod2usage( 2 );
EPrints::Utils::cmd_version( "erase_archive" ) 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, 1 );
exit( 1 ) unless defined $session;

my $repo = $session->get_repository;

my $documents_path = $repo->get_conf( "documents_path" );
my $htdocs_path = $repo->get_conf( "htdocs_path" );
my $database = $repo->get_conf( "dbname" );

unless( $force )
{
	# Write the confirmation prompt
	print "You are about to erase all the eprint records.\n";
	print "From repository: ".$repo->get_id."\n";
	my $sure = EPrints::Utils::get_input_confirm( "Are you sure you want this to happen ");
	unless( $sure )
	{
		print "Aborting then.\n";
		exit( 1 );
	}
}

print "Erasing tables.\n" if( $noise >= 1 );

my $db = $session->get_database;
my @tables = $db->get_tables;
foreach my $table ( @tables )
{
	next unless( $table =~ m/^eprint|inbox|buffer|archive|deletion|document/ );
	print "Erasing table $table\n" if( $noise >= 2 );
	my $sql = "DROP TABLE ".$table;
	$db->do( $sql );
}

if( $noise>=1 ) { print "Erasing eprint files...\n"; }

# Get available directories
opendir DOCSTORE, $documents_path
	or print STDERR "Can't open DOCSTORE\n";

my @doomeddirs;
foreach( readdir DOCSTORE )
{
	next if m/^\.\.?$/; # skip . and ..
	push @doomeddirs,
		$documents_path."/".$_;
}
closedir DOCSTORE;
	
# Remove the contents of each of the directories.

my $dir;
push @doomeddirs, $htdocs_path;
foreach $dir (@doomeddirs)
{
	if( $noise>=2 ) { print "Removing stuff in: $dir\n"; }
	my $cmd = "rm -rf $dir/*";
	if( $noise>=2 ) { print "$cmd\n"; }
	my $rc = CORE::system( $cmd ) & 0xffff;
	print STDERR "Warning: Cleaning $dir didn't go smoothly\n" unless( $rc==0 );
}

if( $noise>=1 ) { print "Creating tables.\n"; }

foreach my $dsid ( qw/ document eprint / )
{
	if( $noise>=2 ) { print "Creating tables for $dsid.\n"; }
	my $dataset = $repo->get_dataset( $dsid );
	$db->create_dataset_tables( $dataset );
}


my $sql;

if( $noise>=1 ) { print "Resetting unique ID counter for eprints.\n"; }
$db->counter_reset( "eprintid" );

if( $noise>=1 ) { print "Erasing history for all eprints.\n"; }
$sql = "DELETE FROM history WHERE datasetid='eprint'";
$db->do( $sql );

if( $noise>=1 ) { print "Erasing index queue for all eprints.\n"; }
$sql = "DELETE FROM indexqueue WHERE field LIKE 'inbox.%' OR field LIKE 'buffer.%' OR field LIKE 'archive.%' OR field LIKE 'deletion.%' OR field LIKE 'eprint.%'  OR field LIKE 'document.%' ";
$db->do( $sql );



$session->terminate;
exit;

