#!/usr/bin/perl -w

use strict;
use warnings;

our $VERSION = 3.1;

=head1 NAME

remove_elements

=head1 DESCRIPTION

Removes the specified elements from the topology archive

  --elements = A comma-separated list of element ids of elements to remove from the archive
  --db_dir = The directory of the Sleepycat Database
  --db_file = The filename of the database

=cut

use File::Basename;
use Cwd;

use FindBin qw($Bin);
use lib "$Bin/../lib";

use perfSONAR_PS::DB::TopologyXMLDB;
use Log::Log4perl qw(:easy);
use Getopt::Long;

my %opts;
my $help_needed;
my $DEBUGFLAG;

=head2 usage

Prints out a help message displaying the available options.

=cut

sub usage {
    print "$0: removes the specified element from the topology database.\n";
    print "    [--debug] [--help] --elements=TOPOLOGY_ID[,TOPOLOGY_ID,...] --db_dir=DATABASE_DIRECTORY --db_filename=DATABASE_FILENAME\n";
    return;
}

my $ok = GetOptions(
    'debug'         => \$DEBUGFLAG,
    'elements=s'    => \$opts{ELEMENTS},
    'db_dir=s'      => \$opts{DB_DIR},
    'db_filename=s' => \$opts{DB_FILE},
    'help'          => \$help_needed
);

if ( not $ok or $help_needed ) {
    usage();
    exit( -1 );
}

unless ($opts{ELEMENTS}) {
    print "Error: you must specify the elements to remove\n";
    usage();
    exit( -2 );
}

unless ($opts{DB_DIR} and $opts{DB_FILE}) {
    print "Error: you must specify the Database directory/filename\n";
    usage();
    exit( -3 );
}

my $level;
if ( $DEBUGFLAG ) {
    $level = $DEBUG;
}
else {
    $level = $INFO;
}

Log::Log4perl->easy_init( $level );

my $logger = get_logger( "remove_elements" );

my ( $status, $res );

unless ( -d $opts{DB_DIR} and -e $opts{DB_DIR}."/".$opts{DB_FILE}) {
    print "Database ".$opts{DB_DIR}."/".$opts{DB_FILE}." does not exist";
    exit(-4);
}

my @elements = split(",",$opts{ELEMENTS});

my $client = perfSONAR_PS::DB::TopologyXMLDB->new();
$client->init( { directory => $opts{DB_DIR}, file => $opts{DB_FILE} } );

( $status, $res ) = $client->open;
if ( $status != 0 ) {
    print "Error opening requested database: $res";
    exit( -5 );
}

( $status, $res ) = $client->removeElements( \@elements );
if ( $status != 0 ) {
    print "Error removing elements: $res\n";
    exit( -6 );
}

exit( 0 );

__END__

=head1 SEE ALSO

L<File::Basename>, L<Cwd>, L<FindBin>,
L<perfSONAR_PS::Client::Topology::XMLDB>,
L<Log::Log4perl>, L<Getopt::Long>

To join the 'perfSONAR Users' mailing list, please visit:

  https://mail.internet2.edu/wws/info/perfsonar-user

The perfSONAR-PS subversion repository is located at:

  http://anonsvn.internet2.edu/svn/perfSONAR-PS/trunk

Questions and comments can be directed to the author, or the mailing list.
Bugs, feature requests, and improvements can be directed here:

  http://code.google.com/p/perfsonar-ps/issues/list

=head1 VERSION

$Id$

=head1 AUTHOR

Aaron Brown, aaron@internet2.edu
Jason Zurawski, zurawski@internet2.edu

=head1 LICENSE

You should have received a copy of the Internet2 Intellectual Property Framework
along with this software.  If not, see
<http://www.internet2.edu/membership/ip.html>

=head1 COPYRIGHT

Copyright (c) 2004-2009, University of Delaware and Internet2

All rights reserved.

=cut
