#!/usr/bin/perl -w

use FindBin;
use lib "$FindBin::Bin/../perl_lib";

######################################################################
#
#
######################################################################

=pod

=head1 NAME

B<toolbox> - EPrints Toolbox

=head1 SYNOPSIS

B<toolbox> I<repository_id> [B<options>] getEprint --eprint B<eprintid>

B<toolbox> I<repository_id> [B<options>] getEprintField --eprint B<eprintid> --field B<fieldname>

B<toolbox> I<repository_id> [B<options>] createEprint < B<data>

B<toolbox> I<repository_id> [B<options>] modifyEprint --eprint B<eprintid> < B<data>

B<toolbox> I<repository_id> [B<options>] removeEprint --eprint B<eprintid>

B<toolbox> I<repository_id> [B<options>] addDocument --eprint B<eprintid> < B<data>

B<toolbox> I<repository_id> [B<options>] modifyDocument --document B<documentid> < B<data>

B<toolbox> I<repository_id> [B<options>] removeDocument --document B<documentid>

B<toolbox> I<repository_id> [B<options>] getFile --document B<documentid> --filename B<filename>

B<toolbox> I<repository_id> [B<options>] addFile --document B<documentid> --filename B<filename> < B<data>

B<toolbox> I<repository_id> [B<options>] removeFile --document B<documentid> --filename B<filename>

B<toolbox> I<repository_id> [B<options>] idSearchEprints < B<data>

B<toolbox> I<repository_id> [B<options>] xmlSearchEprints < B<data>

=head1 DESCRIPTION

This command provides some remote access to read and write to the EPrints repository. Be careful, there are no safety checks!

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

Shows why a plugin is disabled.

=item B<--version>

Output version information and exit.

=back   



=cut


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

use EPrints;
use EPrints::Toolbox;

my $version = 0;
my $verbose = 0;
my $quiet = 0;
my $help = 0;
my $man = 0;

my $eprintid = undef;
my $docid = undef;
my $filename = undef;
my $field = undef;
my $cuserid = undef;
my $format = undef;

Getopt::Long::Configure("permute");

GetOptions( 
	'help|?' => \$help,
	'man' => \$man,
	'version' => \$version,
	'verbose+' => \$verbose,
	'silent' => \$quiet,
	'quiet' => \$quiet,

	'eprint=i' => \$eprintid,
	'document=i' => \$docid,
	'filename=s' => \$filename,
	'field=s' => \$field,
	'cuser=s' => \$cuserid,
	'format=s' => \$format,
) || pod2usage( 2 );
EPrints::Utils::cmd_version( "export_xml" ) if $version;
pod2usage( 1 ) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage( 2 ) if( scalar @ARGV < 2 );

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

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

my $repoid = shift @ARGV;
my $cmd = shift @ARGV;

my $session = new EPrints::Session( 1, $repoid, $noise );
if( !defined $session )
{
	print STDERR "Failed to load repository: $repoid\n";
	exit 1;
}

$session->get_repository->{config}->{enable_web_imports} = 1;
$session->get_repository->{config}->{enable_file_imports} = 1;

my %opts = ();

if( defined $cuserid )
{
	$session->{current_user} = EPrints::DataObj::User->new( $session, $cuserid );
	if( !$session->{current_user} )
	{
		toolbox_fail( "Could not find user $cuserid" );
	}
}

$opts{format} = $format;

$opts{filename} = $filename;

$opts{field} = $field;

if( defined $eprintid )
{
	$opts{eprint} = EPrints::DataObj::EPrint->new( $session, $eprintid );
	if( !defined $opts{eprint} )
	{
		toolbox_fail( "Could not find eprint $eprintid" );
	}
}

if( defined $docid )
{
	$opts{document} = EPrints::DataObj::Document->new( $session, $docid );
	if( !defined $opts{document} )
	{
		toolbox_fail( "Could not find document $docid" );
	}
}

$opts{session} = $session;

my $fn_name = "EPrints::Toolbox::tool_".$cmd;

my( $rc, $output ) = eval "$fn_name( \%opts )";
if( $@ )
{
	toolbox_fail( "Command '$cmd' failed.", [$@] );
}
if( $rc ) 
{
	toolbox_fail( "Command Failed", $output );
}

print $output;

$session->terminate();
exit;

sub toolbox_fail
{
	my( $msg, $problems ) = @_;

	print STDERR "ERROR: $msg\n";
	if( defined $problems )
	{
		foreach my $problem ( @{$problems} )
		{
			print STDERR "* $problem\n";
		}
	}
	exit 1;
}


=head1 COPYRIGHT

=for COPYRIGHT BEGIN

Copyright 2000-2011 University of Southampton.

=for COPYRIGHT END

=for LICENSE BEGIN

This file is part of EPrints L<http://www.eprints.org/>.

EPrints 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 3 of the License, or
(at your option) any later version.

EPrints 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.  If not, see L<http://www.gnu.org/licenses/>.

=for LICENSE END

