#!/usr/atria/bin/Perl
#
# Licensed Materials - Property of IBM
# Rational ClearCase
# (C) Copyright IBM Corp. 2001, 2004 All Rights Reserved
# US Government Users Restricted Rights
# Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
# cqcc_launch: This file will launch the CQCC integration
# after deciding whether to use ccperl or cqperl (preferred)
# and setting up include directories, etc. 

use strict;
require Config;

##########################
# ABS BLOCK CHANGE START #
##########################
#if ($ENV{ATRIA_WEB_GUI}) {
#   print "ERROR: The Base ClearCase/ClearQuest Integration is not currently \n" .
#         "supported under ClearCase Web Servers\n";
#   # Checkouts aren't stopped so allow users to uncheckout files without error
#   if ($ENV{CLEARCASE_OP_KIND} eq "uncheckout") {
#       exit 0;
#   } 
#   exit 2;
#}
########################
# ABS BLOCK CHANGE END #
########################

# Determine OS-specific variables for filename handling and executables

my $windowsOS = ($Config::Config{osname} eq "MSWin32") ? 1 : 0;
my ($cqperl, $perl, $SEP, $QUOTE, $PATH_SEP, $TEMP_DIR);
if ($windowsOS) {
    $perl = "ccperl";
    $cqperl = "cqperl.EXE";
    $SEP = "\\";   # Should print as \
    $QUOTE = "\"";
    $PATH_SEP = ";";
    $TEMP_DIR = $ENV{'TMPDIR'} ? $ENV{'TMPDIR'} : 
                (-w "c:\\TEMP" ? "c:\\TEMP" : ".");
} else {
    $perl = "/usr/atria/bin/Perl";
    $cqperl = "cqperl";
    $SEP = "/";
    $QUOTE = "";
    $PATH_SEP = ":";
    $TEMP_DIR = $ENV{'TMPDIR'} ? $ENV{'TMPDIR'} :
                (-w "/tmp" ? "/tmp" : ".");
}

my $path;
my $centralDir = "";

# Get the pathname argument if it is provided. 

if (defined $ARGV[0] && substr($ARGV[0],0,1) ne "-") {
    $path = shift @ARGV;
}

# Filter out launch-script only arguments
# Avoids sensitivity to argument pair order

my @triggerArgs;
my $launchCmd="";

while (@ARGV) {
    my $arg = shift @ARGV;

    if ($arg eq "-central") {
       $centralDir = shift @ARGV;
       if (defined $path) {
           $path = $centralDir . $SEP . $path;
       } else {
           $path = $centralDir . $SEP . "config.pl";
       }
       if ($ENV{"CQCC_LAUNCH_DEBUG"} > 0) {
           print "\nReading config file and triggers from $centralDir\n";
       }

    # -vob flag tells script to get basic command line arguments
    # from the checkout trigger in the current vob which will provide 
    # the correct paths to cqcc_launch, the config file, and trigger source.

    } elsif ($arg eq "-vob") {
       my $trigger = `cleartool describe trtype:cq_co_trigger`;
       if (defined $ENV{"CLEARCASE_OP_KIND"}) {
           print "The -vob option can not be used inside a trigger\n";
           exit 1;
       } elsif ($trigger =~ /cqcc_launch/) {
           my $plat = $windowsOS ? "win" : "unix";
           # The checkout trigger has all the necessary centralization args
           if ($trigger =~ /\-exec$plat (.*)/) {
               my $platTrigger = $1;
               if ($platTrigger =~ /cqcc_launch/) {
                   $launchCmd = $platTrigger;
               }
           }
           if ($launchCmd eq "") {
               print "Unable to find checkout trigger using cqcc_launch\n";
               print $trigger;
               exit 1;
           }
        }
    } else {
       push @triggerArgs, $arg;
    }
}

my $arglist = join(" ", @triggerArgs);
my $cqcc_incdir = "";
my $perl_switch = $ENV{CQCC_LAUNCH_DEBUG} > 1 ? "-d" : "";
my $temp;
my $cmd;

if ($launchCmd ne "") {
    $cmd = "$launchCmd $arglist";
} else {
   # Catch case where no path is provided. 
   if (! defined $path) {
       $path = $windowsOS ? "CQCC\\config.pl" : "CQCC/config.pl";
       if ($ENV{"CQCC_LAUNCH_DEBUG"} > 0) {
          print "No config file provided, assuming $path\n";
       }
   } 

# CQCC_LAUNCH_CCPERL is used to force ccperl testing
if (! defined $ENV{CQCC_LAUNCH_CCPERL} ||
        $ENV{CQCC_LAUNCH_CCPERL} ne "1" ) {
    for $temp (split($PATH_SEP,$ENV{PATH})) {
        if (-x $temp . $SEP . $cqperl) {
            $perl=$cqperl;
            last;
        }
    }
}

# Expect CQCC to reside in Clearcase\lib\perl by default.
# Once found pass this directory on to cqperl it that is being used

if ($centralDir ne "") {
   $cqcc_incdir = "-I$centralDir" . $SEP . "..";
} else {
   for $temp (@INC) {
       # CQCC resides in old clearcase lib area - cover for RatlPerl @INC path
       if ($temp =~ /(.+)common(.+)lib(.+)perl5(.+)/i) { 
           $temp = "$1" . "clearcase" . $SEP . "lib" . $SEP . "perl5"; 
       }   
       if (-e $temp . $SEP . "CQCCTrigger") {
           $cqcc_incdir = "-I$QUOTE$temp" . $SEP . "CQCCTrigger$QUOTE";
           if (substr($path,0,4) eq "CQCC") {
              $path = $temp . $SEP . "CQCCTrigger" . $SEP . $path;
           }
          last;
       }
   }
}

    $cmd= "$perl $perl_switch $cqcc_incdir " .
    "$QUOTE$path$QUOTE -path $QUOTE$path$QUOTE $arglist";
}

my $ret = 1;
if ($ENV{"CQCC_LAUNCH_DEBUG"} > 0) {
   print "\nCMD: $cmd\n\n";
}

# Work around CQPerl return code problem, can't use backticks
my $retFile = $TEMP_DIR . $SEP . "cqcc_launch.$ENV{CLEARCASE_PPID}";

unlink $retFile;

$ret = system $cmd;

# Note: The -vob option will not return an accurate exit status 
# beyond zero and non-zero(error).

if (-e $retFile) { 
    open(INFILE, $retFile);
    $ret = <INFILE>;
    close(INFILE);
    unlink $retFile;
    if ($ENV{"CQCC_LAUNCH_DEBUG"} > 0) {
       print "\nReading result from $retFile\n";
    }
} 

if ($ENV{"CQCC_LAUNCH_DEBUG"} > 0) {
   print "\nReturn: $ret\n";
}

exit $ret;
