#!/usr/bin/perl -w -I/opt/ep2stable/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 archive

=head1 SYNOPSIS

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

=head1 DESCRIPTION

This script completely erases the archive 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 archive.

=head1 ARGUMENTS

=over 8

=item B<archiveid> 

The ID of the eprint archive 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: 2.3.14.alpha.4

=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::Database;
use EPrints::Archive;

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 $archive = EPrints::Archive->new_archive_by_id( $ARGV[0], 1 );
unless( defined $archive )
{
	print STDERR "Unknown archive id: $ARGV[0]\n";
	exit 1;
}


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

my $input="";
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 archive: ".$archive->get_id."\n";
	print "Are you sure you want this to happen? (yes/no)\n";
	
	$input = <STDIN>;
	chomp( $input );
}

# We'll only work if they type "yes" exactly, otherwise we just abort.
if( !$force && $input ne "yes" )
{
	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( '^.*$', "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=>$archive->get_conf( "dbsock" ),
			dbport=>$archive->get_conf( "dbport" ),
			dbhost=>$archive->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;

