#!/usr/bin/perl

=head1 NAME

rtx-shredder - Script which wipe out tickets from DB

=head1 SYNOPSIS

rtx-shredder 2004-10-03

=head1 DESCRIPTION

rtx-shredder - is simple script which wipe out deleted tickets
from RT DB. As argument get date and only tickets which was last updated
before that date would be wiped out.

=head1 SEE ALSO

RTx::Shredder.

=cut

use strict;
use lib qw(/opt/rt3/local/lib /opt/rt3/lib);

use RTx::Shredder;
RTx::Shredder::Init();


my $date = shift;
unless( $date =~ /\d\d\d\d-\d\d-\d\d/ ) {
	print <<END;
	usage: $0 YYYY-MM-DD

	see also `perldoc rtx-shredder` for more info.
END
	exit 1;
}

my $tickets = RT::Tickets->new( $RT::SystemUser );
$tickets->{'allow_deleted_search'} = 1;
$tickets->LimitStatus( VALUE => 'deleted' );
$tickets->LimitLastUpdated( OPERATOR => '<', VALUE => "$date 00:00");

unless( $tickets->Count ) {
	print "Tickets list is empty.\n";
	exit(0);
} else {
	print $tickets->Count() ." tickets would be wiped out.\n";
	print "Are you shure? [y/N] ";
	unless( <STDIN> =~ /^y$/i ) {
		exit(0);
	}
}

while( my $t = $tickets->Next ) {
	$t->Wipeout();
}

1;
