#!/s/std/bin/perl

# Slurp a ticket from RUST to CAE's RT system.
# Written by Jeff Ballard <ballard@engr.wisc.edu>, May 14, 2001.

$whoami =  getpwuid($>);

if ($#ARGV < 1) {
	print "$0:  Usage:\n";
	die "		$0 queue_name ticket_number\n";
}

$queue = $ARGV[0];
$ticket_num = $ARGV[1];
$query = "/p/condor/home/bin/query --queue $queue";
$action = "/p/condor/home/bin/action --queue $queue";

print "Doing $queue $ticket_num\n";

open (GET_TICKET, "$query --show $ticket_num |") or die "Cannot find RUST query command?";

while(<GET_TICKET>) {
	if (!defined($ticket) && /^Couldn\'t find ticket/) {
		die $_;
	}
	if (!defined($ticket) && /^Invalid queue name/) {
		die $_;
	}
	$ticket .= $_;
	if (/^From: (.+)$/ && !defined($user)) {
		$user = $_;
	}
	if (/^Subject: (.+)$/ && !defined($subject)) {
		$subject = "Subject: $1 [was $queue ticket number $ticket_num]\n";
	}
}

close (GET_TICKET);

print $ticket;

print "Is this the correct ticket to forward to CAE?\n";

$yn = getc;

if ($yn =~ /[Yy]/) {
	print "Yes.  Forwarding away.\n";
} else {
	die "NOT forwarding this ticket to CAE.  Have a nice day.\n";
}


open (MAIL, "| /usr/lib/sendmail helpdesk\@cae.wisc.edu") or die "Cannot start sendmail\n";
print MAIL "$user";
print MAIL "$subject";
print MAIL "To: helpdesk\@cae.wisc.edu\n";
print MAIL "Resent-by: $whoami\@cs.wisc.edu\n\n";
print MAIL <<EOHEAD
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

This ticket has been forwarded into the CAE Ticket tracking system
(helpdesk\@cae.wisc.edu) because this appears to be a CAE-related problem. 
If it is not, please let us know at htcondor-admin\@cs.wisc.edu and
we will take it from there.

Thanks,

-Condor Team

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
THE ENTIRE $queue TICKET NUMBER $ticket_num IS THE REST OF THIS MESSAGE.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

EOHEAD
;
print MAIL "$ticket\n";
close (MAIL) or die;

print "Everything appeared to work.\n";

print "Attempting to add comments to this ticket in RUST\n";

open (RUST, "| $action --num $ticket_num --comment > /dev/null") 
|| die "Cannot find RUST action command?";

print RUST "This ticket is the responsibility of CAE, not CS.\n";
print RUST "It has been fowarded to helpdesk\@cae.wisc.edu.\n";
print RUST "It is now being resolved from RUST at CS.\n";

close (RUST) || die "Can't close RUST action command to add comment: $!\n";

print "Attempting to resolve this ticket from RUST\n";

open (RESOLVE, "$action --num $ticket_num --set status --value resolved |" );
while( <RESOLVE> ) {
       print;
}
close (RESOLVE) || die "Can't close RUST action command to resolve: $!\n";

