#!/usr/bin/perl

use strict;

use lib ("/usr/share/request-tracker3.6/lib", 
		 "/usr/local/share/request-tracker3.6/lib");

use RT::Interface::CLI qw(CleanEnv GetCurrentUser);
use RT::Group;
use RT::Queue;

use Getopt::Std;

my %opts;
getopts('t:', \%opts);

exit unless ($opts{'t'});

# clean out all the nasties from the environment..
CleanEnv();

# load the config file..
RT::LoadConfig();

# connect to the database and get RT::SystemUser and RT::Nobody loaded..
RT::Init();

# reset the RT die signal handler, we don't need the extra info..
$SIG{__DIE__} = "";

# drop setgid permissions, which is version 3.0 specific
eval { RT::DropSetGIDPermissions() };

# get the current user all loaded..
my $CurrentUser = GetCurrentUser();
unless ($CurrentUser->Id) {
    print(STDERR "ERROR: RT user not found\n");
    exit(1);
}

my $transaction = new RT::Transaction($RT::SystemUser);

$transaction->Load($opts{'t'}); 

unless ($transaction->ObjectType eq 'RT::Ticket') {
	print STDERR "Transaction is not a ticket transaction\n";
	exit;
}
unless (($transaction->Type eq 'Correspond') || ($transaction->Type eq 'Comment')) {
	print STDERR "Transaction is not correspondance\n";
	exit;
}

my $scrips = new RT::Scrips($RT::SystemUser);

$scrips->Prepare(
			Stage       => 'TransactionCreate',
			Type        => $transaction->Type,
			Ticket      => $transaction->ObjectId,
			Transaction => $transaction->id
		);

$scrips->Commit();
