#!/usr/bin/perl -w -I /opt/eprints3/perl_lib

=pod

=head1 NAME

B<touch_transfers> - Updates all Archivematica records which are not pending a transfer and sets them so they are.

=head1 SYNOPSIS

B<touch_transfers> I<repository_id>

=head1 DESCRIPTION

=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<--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 $force = 0;
my $datasetid = 'eprint'; # default to eprint
my $dataobjid = '';
my $help = 0;
my $man = 0;
my $limit=0;
my $unset = 0;
my $delete = 0;

Getopt::Long::Configure("permute");

GetOptions(
        'help|?' => \$help,
        'version' => \$version,
        'verbose+' => \$verbose,
        'datasetid=s' => \$datasetid,
        'dataobjid=s' => \$dataobjid,
		'limit=s' => \$limit,
		'unset' => \$unset,
		'delete' => \$delete,
        'force' => \$force,
) || pod2usage( 2 );
EPrints::Utils::cmd_version( "touch_transfers" ) if $version;
pod2usage( 1 ) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage( 2 ) if( @ARGV < 1 );

my $noise = 1;
$noise = 1+$verbose if( $verbose );

#Look for is_dirty FALSE and set to TRUE by default
my $setSearchFlag = 'FALSE';
my $setNeedUpdateTo = 'TRUE';
#Look for is_dirty TRUE and set to FALSE if unset parameter
$setSearchFlag = 'TRUE' if ($unset);
$setNeedUpdateTo = 'FALSE' if ($unset);

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;
}

my $ds = $session->dataset( "archivematica" );
my $searchexp = new EPrints::Search( session=>$session, dataset=>$ds );
$searchexp->add_field( $ds->get_field( "is_dirty" ), $setSearchFlag, "EQ" ) unless $force;

if( $datasetid && $dataobjid )
{
	print "Limiting to $datasetid/$dataobjid\n" if $verbose;
	$searchexp->add_field( $ds->get_field( "datasetid" ), $datasetid, "EQ" );
	$searchexp->add_field( $ds->get_field( "dataobjid" ), $dataobjid, "EQ" );
}

my $plugin = $session->plugin( "Export::Archivematica::EPrint" );

my $list = $searchexp->perform_search;

print "Found " . $list->count() . " records to process.\n" if $verbose;
my $processed = 0;

$list->map( sub {
	my( $session, $dataset, $am ) = @_;
	
	if ($limit != 0) {return if $processed >= $limit;}

	print "Processing Archivematica ID: " . $am->id . "\n" if $verbose;
	
	if ($delete){
		my $ok=$am->delete();
		if ($ok) {
			print "Deleted transfer record." 
		}
		else{
			print "Attempted, but failed deletion of transfer record."
		}
	}
	else{
		$am->set_value( "is_dirty", $setNeedUpdateTo );
		$am->add_to_record_log( "info", "trigger new transfer", "success" ) if (!$unset);
		$am->add_to_record_log( "info", "unset transfer", "success" ) if ($unset);
		$am->commit();
	}
	
	
	$processed++;
});
my $action = "Processed";
$action = "Deleted" if $delete;
print "$action $processed records\n" if $verbose;
$session->terminate();
