#!/usr/bin/perl -w

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

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

=pod

=head1 NAME

B<hefce_update_exceptions> - Find EPrint records which need their exceptions mapping over to new values and recommit them.

=head1 SYNOPSIS

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

=head1 DESCRIPTION

This script looks for any EPrint records which contain old values for REF CC exceptions and then recommits them.
Recommitting the records, calls the EP_TRIGGER_BEFORE_COMMIT trigger in zz_hefce_oa.pl.

The exceptions are mapped as follows:

Deposit exception (g) --> Further exception (b)
Deposit exception (f) --> Gold OA (hoa_gold)
Other exception --> Further exception (a)
 
This script should be called upon updating your REF CC plugin to v1.5.

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

=back   


=cut

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

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

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

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

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

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

#get a dataset of eprints
my $ds = $session->get_repository->get_dataset("eprint");

my $search_exp = EPrints::Search->new(
	session => $session,
	satisfy_all => 0,
	dataset => $ds,
);

$search_exp->add_field(
        fields => [ $ds->field( 'hoa_ex_dep' ) ],
        value => 'f g',
        match => "IN",
);

$search_exp->add_field(
        fields => [ $ds->field( 'hoa_ex_oth' ) ],
        value => 'TRUE',
        match => "EQ",
);

my $list = $search_exp->perform_search;
my $count = 0;
$list->map(sub{
        my($session, $dataset, $eprint) = @_;

	print "Recommitting EPrint #" . $eprint->id . "\n" if( $noise > 0);
	
	$count++;
	$eprint->commit( 1 );
});

print "Finished updating REF CC exceptions.\n" if( $noise > 0 );
if( $noise > 1 )
{
	print "Recommitted $count EPrints.\n";
}

$session->terminate();
exit;
