#!/usr/bin/perl -w -I/opt/eprints3/perl_lib

=pod

=head1 NAME

B<remove_field> - remove the database tables for a configured field

=head1 SYNOPSIS

B<remove_field> - I<repository_id> I<dataset> I<field_name> [B<options>]

=head1 DESCRIPTION

This utility tool will remove the database tables for a configured eprint field, prior to removing the field's configuration.

=head1 ARGUMENTS

=over 8

=item B<repository_id> 

The ID of the EPrint repository to modify.

=item B<dataset>

One of eprint, document or user.

=item B<field_name> 

The name of the field to remove.

=back

=head1 OPTIONS

=over 8

=item B<--force>

Don't confirm changes.

=item B<--help>

Print a brief help message and exit.

=item B<--man>

Print the full manual page and then exit.

=item B<--quiet>

This option does not do anything.

=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<--remove-dataset>

Remove all the fields for a dataset.

=back

=cut

use strict;
use warnings;

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

my $version = 0;
my $verbose = 0;
my $quiet = 0;
my $help = 0;
my $man = 0;
my $dryrun = 0;
my $force = 0;
my $remove_dataset = 0;

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

GetOptions( 
	'help|?' => \$help,
	'man' => \$man,
	'version' => \$version,
	'verbose+' => \$verbose,
	'silent' => \$quiet,
	'quiet' => \$quiet,
	'force' => \$force,
	'remove-dataset' => \$remove_dataset,
	'dry-run' => \$dryrun
) || pod2usage( 2 );
EPrints::Utils::cmd_version( "lift_embargos" ) if $version;
pod2usage( 1 ) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage( 2 ) unless( scalar @ARGV == 3 or ($remove_dataset and scalar @ARGV == 2) ); 

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

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

my( $repoid, $datasetid, $fieldid ) = @ARGV;

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

my $database = $session->{ database };

my $fields = $session->get_repository->get_conf( "fields" );

if( !$force && !exists( $fields->{ $datasetid } ) )
{
	pod2usage( "$datasetid is not a valid dataset id or has no user-configured fields, should be one of: " . join(',', keys %$fields) );
}

my $dataset = $session->get_repository->get_dataset( $datasetid );

if( $remove_dataset )
{
	remove_dataset( $dataset );
}
else
{
	remove_field( $dataset, $fieldid );
}

$session->terminate();

sub remove_dataset
{
	$database->drop_dataset_tables( $dataset );
}

sub remove_field
{
	if( !$dataset->has_field( $fieldid ) )
	{
		pod2usage( "$fieldid is not a configured field in the $datasetid dataset - you need to execute this script *before* removing it from your configuration" );
	}

	my $system_field = 1;
	for(@{$fields->{ $datasetid }})
	{
		if( $_->{ name } eq $fieldid )
		{
			$system_field = 0;
		}
	}

	if( !$force && $system_field )
	{
		print "$fieldid is a system field. You should hide this field in the workflow, rather than try to remove it.\n";
		exit(1);
	}

	my $field = $dataset->get_field( $fieldid );

	unless( $force )
	{
		my $confirm = EPrints::Utils::get_input_confirm( "Are you sure you want to remove the database tables for " . $field->get_name . " (this can NOT be undone!)?" );
		exit(0) unless $confirm;
	}

	$database->remove_field( $dataset, $field );
}
