use Config;

my $PVOBTAG;
my $tempfile;
my $exit_value;


if ($Config{'osname'} eq 'MSWin32') {
	$PVOBTAG = '\cc5-pvob1';
	$tempfile = $ENV{TMP}."\\expreop.".$ENV{"CLEARCASE_PPID"}.".txt";
}
else {
	$PVOBTAG = '';
}

my $STREAM = "stream:".$ENV{"CLEARCASE_STREAM"};
my $ATTYPE = "attype:deliver_in_progress\@$PVOBTAG";

print $STREAM."\n";
print $ATTYPE."\n";

my $cmdline;
my $cmdoutput;

# Test to see if the deliver in progress attribute is already 
# applied to the target stream. Needed because we can't test 
# for errors from GUI-Deliver-Fired triggers (yet).

$msg = `cleartool describe -fmt "%a" $STREAM`;

print $msg."\n";

if ($ENV{"CLEARCASE_CMDLINE"} eq "") {
	open(TEMPFH, "> $tempfile");
	print TEMPFH $msg."\n";
	close(TEMPFH);
	$cmdline = "clearprompt text -outfile $tempfile -multi_line -dfile \"$tempfile\" -prompt \"Describe Results\"";
	$cmdoutput = `$cmdline`;
}

if (index($msg, "deliver_in_progress") >=0) {
	print "***\n";
	print "*** A deliver operation is already in progress. Please try again later.\n";
	print "***\n";
	exit 1;
}

# When Fired from the GUI Project explorer, the below 
# command DOES NOT EXECUTE AT ALL. The needed attribute type is not applied.

$cmdline = "cleartool mkattr -default $ATTYPE $STREAM";
print $cmdline."\n";

$msg = `$cmdline`;
$exit_value = $? >> 8;

if ($ENV{"CLEARCASE_CMDLINE"} eq "") {
	open(TEMPFH, "> $tempfile");
	print TEMPFH $msg."\n";
	close(TEMPFH);
	$cmdline = "clearprompt text -outfile $tempfile -multi_line -dfile \"$tempfile\" -prompt \"mkattr Results\"";
	$cmdoutput = `$cmdline`;
}

if (!($exit_value eq 0)) {
	print "***\n";
	print "*** A deliver operation was started just before yours.\n";
        print "*** That deliver operation is already in progress. Please try again later.\n";
	print "***\n";
	exit 1;
}


exit 0;