Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / bin / cqc
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         cqc: Clearquest client
5 # Description:  This script is a test client for cqd.
6 # Author:       Andrew@DeFaria.com
7 # Created:      Fri May 31 15:34:50  2002
8 # Modified:
9 # Language:     Perl
10 #
11 # (c) Copyright 2002, Salira Optical Network Systems, all rights reserved.
12 #
13 ################################################################################
14 use strict;
15
16 BEGIN {
17   # Add the appropriate path to our modules to @INC array. We use ipconfig to
18   # get the current host's IP address then determine whether we are in the US
19   # or China.
20   my @ipconfig = grep (/IP Address/, `ipconfig`);
21   my ($ipaddr) = ($ipconfig[0] =~ /(\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3})/);
22
23   # US is in the subnets of 192 and 172 while China is in the subnet of 10
24   if ($ipaddr =~ /^192|^172/) {
25     unshift (@INC, "//sons-clearcase/Views/official/Tools/lib");
26   } elsif ($ipaddr =~ /^10/) {
27     unshift (@INC, "//sons-cc/Views/official/Tools/lib");
28   } else {
29     die "Internal Error: Unable to find our modules!\n"
30   } # if
31 } # BEGIN
32
33 use cqc;
34
35 %cqc::fields;
36 $cqc::command;
37
38 my $len;
39 my $key;
40 my $value;
41 my $servername = $ENV {CQDSERVER};
42 my $bugid;
43 my @query_fields;
44 my $result;
45
46 sub Usage {
47   print "Usage: cqc [ -s servername ] bugid [ fieldname... ]\n";
48   exit 1;
49 } # Usage
50
51 $bugid = "";
52 @query_fields = ();
53
54 sub GetParms {
55   my $i = 0;
56
57   if ($ARGV [0] && $ARGV [0] eq "-s") {
58     shift (@ARGV);
59     if (!$ARGV [0]) {
60       Usage;
61     } else {
62       $servername = shift (@ARGV);
63     } # if
64   } # if
65
66   if ($ARGV [0]) {
67     $bugid = shift (@ARGV);
68   } # if
69
70   @query_fields = @ARGV;
71
72   # Downshift any query_fields
73   foreach (@query_fields) {
74     $query_fields [$i++] = lc $_;
75   } # foreach
76 } # GetParms
77
78 sub fix_bugid {
79   my $bugid = shift;
80
81   if ($bugid =~ /^\d+$/) {
82     if (length ($bugid) < 13) {
83       $len = 13 - length ($bugid);
84       if ($len < 5) {
85         # Can't even prepend "BUGS2"!
86         print "Invalid bug id \"$bugid\" encountered!\n";
87         exit 1;
88       } else {
89         $bugid = "BUGS2" . "0" x ($len - 5) . $bugid;
90       } # if
91     } # if
92   } # if
93
94   return $bugid;
95 } # fix_bugid
96
97 # Main code
98 GetParms;
99
100 if (defined ($servername)) {
101   die "Unable to connect to $servername\n" if cqc::Connect ($servername) < 0;
102 } # if
103
104 if ($bugid) {
105   $result = cqc::GetBugRecord (fix_bugid ($bugid), %fields);
106   die "Unable to connect to server\n" if $result < 0;
107   if ($result) {
108     print "Bug ID $bugid was not found\n";
109   } else {
110     if (@query_fields) {
111       foreach (@query_fields) {
112         if (@query_fields > 1) {
113           print "$_: $cqc::fields{$_}\n";
114         } else {
115           print "$cqc::fields{$_}\n";
116         } # if
117       } # foreach
118     } else {
119       while (($key, $value) = each (%fields)) {
120         $value =~ s/\r/\r\n/g;
121         print "$key: $value\n";
122       } # while
123     } # if
124   } # if
125 } else {
126   print "Enter bug ID:";
127
128   while ($command = <STDIN>) {
129     chomp $command;
130     last if $command =~ m/exit|quit|shutdown/;
131
132     $bugid = fix_bugid ($command);
133     $result = cqc::GetBugRecord ($bugid, %fields);
134     die "Unable to connect to server\n" if $result < 0;
135     if ($result) {
136       print "Bug ID $bugid was not found\n";
137     } else {
138       while (($key, $value) = each (%fields)) {
139         $value =~ s/\r/\r\n/g;
140         print "$key: $value\n";
141       } # while
142     } # if
143
144     print "Enter bug ID:";
145   } # while
146 } # if