Removed /usr/local from CDPATH
[clearscm.git] / cc / mknusers
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         $RCSfile: mknusers,v $
5 # Revision:     $Revision: 1.3 $
6 # Description:  This script will add a user to the nusers list for a lock
7 # Author:       Andrew@DeFaria.com
8 # Created:      Mon Feb 13 10:35:34 PST 2006
9 # Modified:     $Date: 2011/08/31 21:57:06 $
10 # Language:     Perl
11 #
12 # (c) Copyright 2006, Andrew@DeFaria.com, all rights reserved.
13 #
14 ################################################################################
15 use strict;
16 use warnings;
17
18 use FindBin;
19 use lib "$FindBin::Bin/../lib";
20
21 use Getopt::Long;
22
23 use OSDep;
24 use Display;
25 use Utils;
26
27 my $me = $FindBin::Script;
28
29 # Pick up from the environment if the user specifies pvob
30 my $pvob = $ENV{pvob};
31
32 my @pvob_related_objects = (
33   "activity",
34   "stream",
35 );
36
37 sub Usage {
38   my $msg = shift;
39
40   display "Usage: $me: <object_selector> <username> [<username>]";
41
42   if (defined $msg) {
43     error "$msg", 1;
44   } # if
45
46   exit 0;
47 } # Usage
48
49 sub AddNuser {
50   my $object    = shift;
51   my $username  = shift;
52   my @users     = @_;
53
54   my $cmd = "cleartool lock -replace -nusers ";
55
56   foreach (@users) {
57     $cmd .= "$_,";
58   } # foreach
59
60   $cmd .= "$username $object";
61
62   my @output = `$cmd`;
63
64   return $?;
65 } # AddNuser
66
67 my $object      = shift;
68 my @users       = @ARGV;
69
70 Usage "Must specify an object selector" if !defined $object;
71 Usage "Must specify an username"        if scalar @users eq 0;
72
73 my $object_type = $object;
74 my $full_object;
75
76 $object_type =~ s/:.*//;
77
78 if ($object =~ m/(.*)\@(.*)/) {
79   $object       = $1;
80   $pvob         = $2;
81 } # if
82
83 Usage "Must specify pvob or set pvob in your environment" if !$pvob;
84
85 if (InArray $object_type, @pvob_related_objects) {
86   # Need to add additional "\\" because Windows will eat them up when executing a ``;
87   if ($arch eq "windows" or $arch eq "cygwin") {
88     $full_object = "$object\@\\$pvob";
89   } else {
90     $full_object = "$object\@$pvob";
91   } # if
92 } else {
93   $full_object = $object;
94
95   # Handle oddity with windows using \ for vob tags
96   if ($full_object =~ /vob:\\(.*)/) {
97     $full_object = "vob:\\\\" . $1;
98   } # if
99 } # if
100
101 foreach (@users) {
102   my $cmd = "cleartool lslock $full_object 2>&1";
103   my @output    = `$cmd`;
104   my $status    = $?;
105
106   if ($status ne 0) {
107     display "$object does not exist";
108     exit 1;
109   } # if
110
111   my @current_users;
112
113   foreach (@output) {
114     if (/\"Locked except for users: (.*)\"/) {
115       @current_users = split " ", $1;
116       last;
117     } # if
118   } # foreach
119
120   if (InArray $_, @current_users) {
121     error "User $_ is already on the nusers list for $object", 1;
122   } else {
123     if (AddNuser $full_object, $_, @current_users) {
124       error "Unable to add $_ to nusers list for $object", 2;
125     } else {
126       display "User $_ added to the list of nusers for $object";
127     } # if
128   } # if
129 } # foreach