#!/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 database tables and files associated with an EPrint repository

=head1 SYNOPSIS

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

=head1 DESCRIPTION

This script completely erases the repository contents, including all
database tables and eprint document files and the web site. After running this, the metadata configuration can be safely updated and the creation scripts run again.

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

After this script you will need to run B<create_tables> before you can use the repository.

=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.

=item B<--noerasedb>

Don't delete the database tables.

=item B<--noerasefiles>

Don't erase the document and website files. 

=item B<--rootpass> I<password>

This option allows you to specify the MySQL root password, which will skip the bit where you get asked for it. It's not very secure to put a root password on the command line. Consider yourself warned...

=back   


=head1 BUGS

This script assumes that the root mysql user is called root.

You may run into problems if your mysql install is not on the same machine as EPrints. If this happens use B<--noerasefiles> and delete and recreate the database yourself.

You can specify B<--noerasedb> and B<--noerasefiles> at the same time, but that's really stupid, as then nothing will happen.

=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 , 
	'erasedb!' => \$erasedb , 
	'erasefiles!' => \$erasefiles , 
	'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 $repo = EPrints::Repository->new( $ARGV[0], 1 );
unless( defined $repo )
{
	print STDERR "Unknown repository id: $ARGV[0]\n";
	exit 1;
}


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:\n";
	if( $erasedb ) { print "- all database tables\n"; }
	if( $erasefiles ) { print "- all eprint files\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 );
	}
}


if( $erasedb )
{
	# First erase the database tables. We now do this the dramatic way
	# of dropping and recreating the database.
	# This makes it possible to erase a really broken setup.

	if( !defined $rootpass )
	{
		if( $noise>=1 ) {
			print "Dropping and recreating database (requires database root password)\n";
			print "You will need to enter the MySQL root user password.\n";
		}
	
		$rootpass = EPrints::Utils::get_input_hidden( '^.*$', "MySQL Root Password" );
		print "\n";
	}
	
	if( $noise>=1 ) { print "Connecting to mysql...\n"; }
	
	my $dbh = DBI->connect(
		EPrints::Database::build_connection_string(
			dbname=>"mysql",
			dbsock=>$repo->get_conf( "dbsock" ),
			dbport=>$repo->get_conf( "dbport" ),
			dbhost=>$repo->get_conf( "dbhost" ) ),
		"root",
		$rootpass );

	if( !defined $dbh )
	{
		print STDERR "\n\nFailed to connect to database. Aborting.\n\n";
		exit( 1 );
	}
	if( $noise>=1 ) { print "Dropping database \"$database\"\n"; }
	$dbh->do( "drop database $database" );
	if( $noise>=1 ) { print "Re-creating database \"$database\"\n"; }
	$dbh->do( "create database $database" );

	$dbh->disconnect;
	if( $noise>=1 ) { print "Done recreating database\n"; }
}

if( $erasefiles )
{
	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 "...done erasing eprint files.\n"; }
}

if( $noise>=2) { print "Exiting normally.\n"; }
exit;

