#!/usr/bin/perl -w


use strict;
use Carp;
use Getopt::Long;

use lib '/opt/rt3/lib';
use lib '/opt/rt3/etc';

use RT::Interface::CLI  qw(CleanEnv GetCurrentUser GetMessageContent loc);
CleanEnv(); 

use RT; 
use RT::Ticket;
use RT::Tickets;

#Load RT's config file
RT::LoadConfig();

# Connect to the database. set up loggign
RT::Init();

#Get the current user all loaded
my $requestor = shift || '--help';

if ($requestor eq '--help') {
  print <<END;
delete-by-requestor <requestor email>
END
  exit(0);
}


my $Tickets = new RT::Tickets($RT::SystemUser);
$Tickets->LimitWatcher(TYPE => 'Requestor', VALUE => $requestor);
$Tickets->LimitStatus(VALUE => 'Deleted', OPERATOR => '!=');

unless ($Tickets->Count) {
	print "tickets not found with requestor $requestor\n";
}
print "Amount of tickets requested by $requestor is ".$Tickets->Count."\n";

while (my $t=$Tickets->Next) {
	my ($val,$msg) = $t->Delete();
	if ($val) {
		print "OK: $msg\n";
	} else {
		die "$msg";
	}
}

$RT::Handle->Disconnect();

