#!/usr/bin/perl -w 

use strict;

# The script should be installed in $EPRINTS_PATH/archives/<id>/bin/stats/
use FindBin;
use lib "$FindBin::Bin/../../../../perl_lib";

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

use EPrints;

my $do_sets = 0;
my $setup = 0;
my $uninstall=0;
our $verbose=0;
my $datasets='';

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

GetOptions(
	'setup' => \$setup,
	'uninstall' => \$uninstall,
	'verbose' => \$verbose,
	'sets-only' => \$do_sets,
	'datasets=s' => \$datasets,
) || pod2usage( 2 );

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

my $repoid = shift @ARGV;
unless( defined $repoid )
{
	&usage();
	exit;
}

my $session = new EPrints::Session( 1, $repoid );
unless( defined $session )
{
	&usage;
	exit;
}

my $handler = $session->plugin( "Stats::Handler", noise => $verbose );
unless($handler)
{
	print STDERR "FATAL ERROR: Stats handler (Stats::Handler.pm) not available\n";
	$session->terminate();
	exit;
}

if( $uninstall )
{
	my $ok = EPrints::Utils::get_input( '^(yes|no)$', "Uninstall IRStats2?", "yes" );
	unless( $ok eq "yes" )
	{
		$session->terminate;
		exit;
	}

	$handler->uninstall();
	$session->terminate;
	exit;
}

if( $do_sets )
{
	$handler->log( "Doing sets tables only" );
}

$handler->log( "Generating set tables." );
$handler->sets->populate_tables();

if( $do_sets )
{
	$session->terminate;
	exit( 1 );
}

if( $setup )
{
	$handler->log( "Creating internal tables" );
	$handler->create_internal_tables();
}

my %selected_datasets = map { $_ => 1 } split( /,/, $datasets );

my $dataset_ids = $session->config( "irstats2", "datasets" ) || {};

foreach my $dataset_id ( keys %$dataset_ids )
{
	next if( EPrints::Utils::is_set( %selected_datasets ) && !$selected_datasets{$dataset_id} );
	my $conf = $dataset_ids->{$dataset_id};
	next unless( defined $conf );

	$conf->{datasetid} = $dataset_id;
	$conf->{create_tables} = $setup;

	$handler->process_dataset( $dataset_id, $conf );
}

$handler->log( "Leaving now" );

$session->terminate;
exit;

sub usage
{
	print <<USAGE;

$0 ARCHIVE_ID [--setup][--sets-only] [--uninstall] [--verbose]

\t--setup: use this option the first time to run IRStats2, or if you want to re-process all the statistics.
\t--datasets=<dataset1,[dataset2...]>: this will only generate data for the selected datasets.
\t--sets-only: only re-generates the Set tables. Useful if you've changed the Sets configuration.
\t--uninstall: removes all the data generated by IRStats 2. You will still need to manually remove all the installed files.
\t--verbose: displays extra information (useful for debugging).

USAGE
}
