#!/usr/bin/perl

#
# Shred all tickets in the SPAM-Shredder queue older than 7 days
# g.booth@lboro.ac.uk

use strict;
use lib "/opt/rt3/lib";

use RT;
use RT::Interface::CLI qw (CleanEnv GetCurrentUser);
#for testing
use Data::Dumper;

#load rt
CleanEnv();
RT::LoadConfig();
RT::Init();
use RT::Shredder;
use POSIX qw(strftime);

   my $today= strftime("%Y%m%d", localtime); #todays date

   RT::Shredder::Init( force => 1 );
   my $shredder = RT::Shredder->new();
   my $deleted = RT::Tickets->new( $RT::SystemUser );
   $deleted->{'allow_deleted_search'} = 1;
   $deleted->LimitQueue( VALUE => 'SPAM-Shredder' );
   while( my $t = $deleted->Next ) {
        my $last = $t->LastUpdated;
        #split the time off
        my @date = split(/ /, $last);
        #remove the "-"
                my @newdate = split(/-/, $date[0]);
                #munge it back together
                my $datetest = $newdate[0].$newdate[1].$newdate[2];
                        if ($today -6 > $datetest){
                                $shredder->PutObject( Object => $t );
                                }
                        else    {
                                next;
                                }
   }
$shredder->WipeoutAll;
exit