Removed /usr/local from CDPATH
[clearscm.git] / cc / rmnusers
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         $RCSfile: rmnusers,v $
5 # Revision:     $Revision: 1.3 $
6 # Description:  This script will remove 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 RemoveNuser {
50   my $object    = shift;
51   my $username  = shift;
52   my @users     = @_;
53
54   my $cmd = "cleartool lock ";
55
56   $cmd .= "-replace ";
57
58   if (scalar @users gt 1) {
59     $cmd .= "-nusers ";
60
61     my $first = $true;
62
63     foreach (@users) {
64       next if $_ eq $username;
65       if ($first) {
66         $first = $false;
67         $cmd .= $_;
68       } else {
69         $cmd .= ",$_";
70       } # if
71     } # foreach
72   } # if
73
74   $cmd .= " $object";
75
76   my @output = `$cmd`;
77
78   return $?;
79 } # RemoveNuser
80
81 my $object      = shift;
82 my @users       = @ARGV;
83
84 Usage "Must specify an object selector" if !defined $object;
85 Usage "Must specify an username"        if scalar @users eq 0;
86
87 my $object_type = $object;
88 my $full_object;
89
90 $object_type =~ s/:.*//;
91
92 if ($object =~ m/(.*)\@(.*)/) {
93   $object       = $1;
94   $pvob         = $2;
95 } # if
96
97 Usage "Must specify pvob or set pvob in your environment" if !$pvob;
98
99 if (InArray $object_type, @pvob_related_objects) {
100   # Need to add additional "\\" because Windows will eat them up when executing a ``;
101   if ($arch eq "windows" or $arch eq "cygwin") {
102     $full_object = "$object\@\\$pvob";
103   } else {
104     $full_object = "$object\@$pvob";
105   } # if
106 } else {
107   $full_object = $object;
108
109   # Handle oddity with windows using \ for vob tags
110   if ($full_object =~ /vob:\\(.*)/) {
111     $full_object = "vob:\\\\" . $1;
112   } # if
113 } # if
114
115 foreach (@users) {
116   my $cmd = "cleartool lslock $full_object 2>&1";
117   my @output    = `$cmd`;
118   my $status    = $?;
119
120   if ($status eq 0) {
121     if (scalar @output eq 0) {
122       display "$object is not locked";
123       exit 0;
124     } # if
125   } else {
126     display "$object does not exist";
127     exit 1;
128   } # if
129
130   my @current_users;
131
132   foreach (@output) {
133     if (/\"Locked except for users: (.*)\"/) {
134       @current_users = split " ", $1;
135       last;
136     } # if
137   } # foreach
138
139   if (InArray $_, @current_users) {
140     if (RemoveNuser $full_object, $_, @current_users) {
141       error "Unable to remove $_ from nusers for $object";
142     } else {
143       display "User $_ removed from the list of nusers for $object";
144     } # if
145   } else {
146     error "User $_ is not on the nusers list for $object";
147   } # if
148 } # foreach