9696b946683f9f41847a85c875f81b47c1e19021
[clearscm.git] / clients / Ameriquest / bin / tagit.pl
1 #!/usr/bin/perl -w
2 #################################################################################
3 #
4 # File:         tagit
5 # Description:  Script to tag views or vobs into the current region. The main
6 #               motivation for this script is to be able to tag things quickly
7 #               and easily. As such we employ heuristics to find these objects.
8 # Author:       Andrew@DeFaria.com
9 # Created:      Fri Apr  9 12:19:04 PDT 2004
10 # Language:     Perl
11 #
12 # (c) Copyright 2004, Andrew@DeFaria.com, all rights reserved.
13 #
14 ################################################################################
15 use strict;
16 use File::Spec;
17
18 my $me;
19
20 BEGIN {
21   # Extract relative path and basename from script name.
22   $0  =~ /.*[\/\\](.*)/;
23   $me = (!defined $1) ? $0  : $1;
24 } # BEGIN
25
26 # Check to see if we are running on Windows
27 my $windows     = ($^O =~ /MSWin/) ? "yes" : "no";
28 my $null        = $windows eq "yes" ? "NUL" : "/dev/null";
29 my $backslashes = $windows eq "yes" ? "\\"  : "\\\\";
30
31 sub Usage {
32   print "Usage $me: [[ -view ] <view_tag> ] [ -vob <vob_tag> ]\n";
33   print "\nWhere:\n";
34   print "\t-view\tView tag to tag in current region\n";
35   print "\t-vob\tVob tag to tag in current region\n";
36   exit 1;
37 } # Usage
38
39 sub GetCurrentRegion {
40   my @lines = `cleartool hostinfo -l`;
41
42   foreach (@lines) {
43     chomp; chop $_ if /\r/;
44
45     if (/Registry region: (\S+)/) {
46       return $1;
47     } # if
48   } # foreach
49
50   return undef;
51 } # GetCurrentRegion
52
53 my $current_region = GetCurrentRegion;
54 my @regions = `cleartool lsregion`;
55
56 sub FindView {
57   my $view_tag = shift;
58
59   foreach (@regions) {
60     chomp; chop if /\r/;
61
62     my $output = `cleartool lsview -region $_ $view_tag 2> $null`;
63     chomp $output; chop $output if /\r/;
64
65     if ($output =~ /$view_tag\s*(.*)/) {
66       my $view_storage = $1;
67       $view_storage =~ tr /\\/\/\//;
68       return ($view_storage, $_);
69     } # if
70   } # foreach
71
72   return;
73 } # FindView
74
75 sub FindVob {
76   my $vob_tag = shift;
77
78   foreach (@regions) {
79     chomp; chop if /\r/;
80
81     my $output = `cleartool lsvob -region $_ $backslashes$vob_tag 2> $null`;
82     chomp $output; chop $output if /\r/;
83
84     if ($output =~ /$vob_tag\s*(\S*)/) {
85       my $vob_storage = $1;
86       $vob_storage =~ tr /\\/\/\//;
87       return ($vob_storage, $_);
88     } # if
89   } # foreach
90
91   return;
92 } # FindVob
93
94 sub MkViewTag {
95   my $view_tag          = shift;
96   my $view_storage      = shift;
97
98   # Check to see if view tag already exists
99   if (!system "cleartool lsview $view_tag > $null 2>&1") {
100     print "$view_tag already exists in $current_region\n";
101     return 0;
102   } else {
103     if (system "cleartool mktag -view -tag $view_tag $view_storage") {
104       die "Unable to make view tag: $view_tag in $current_region\n";
105     } # if
106     return 1;
107   } # if
108 } # MkViewTag
109
110 sub MkVobTag {
111   my $vob_tag           = shift;
112   my $vob_storage       = shift;
113
114   # Check to see if vob tag already exists
115   if (!system "cleartool lsvob $backslashes$vob_tag > $null 2>&1") {
116     print "$vob_tag already exists in $current_region\n";
117     return 1;
118   } else {
119     print "cleartool mktag -vob -tag $backslashes$vob_tag $vob_storage\n";
120     if (system "cleartool mktag -vob -tag $backslashes$vob_tag $vob_storage") {
121       die "Unable to make vob tag: $vob_tag in $current_region\n";
122     } # if
123     return 0;
124   } # if
125 } # MkVobTag
126
127 my $view_tag;
128 my $vob_tag;
129
130 # Get parms
131 while ($#ARGV >= 0) {
132   if ($ARGV [0] eq "-u" or $ARGV [0] eq "-usage") {
133     Usage;
134   } # if
135
136   if ($ARGV [0] eq "-vob") {
137     shift;
138     $vob_tag = $ARGV [0];
139     # Script all backslashes - we'll re-add them when needed...
140     $vob_tag =~ s/\\//;
141     shift;
142     next;
143   } # if
144
145   if ($ARGV [0] eq "-view") {
146     shift;
147     $view_tag = $ARGV [0];
148     next;
149   } # if
150 } # while
151
152 if (!(defined $view_tag or defined $vob_tag)) {
153   Usage "Must specify a view or a vob to tag!";
154 } # if
155
156 if (defined $view_tag) {
157   my ($view_storage, $view_region) = FindView $view_tag;
158
159   die "Unable to find view $view_tag in any region!\n"  if !defined $view_region;
160   die "Unable to find storage area for $view_tag\n"     if !defined $view_storage;
161
162   if ($view_region eq $current_region) {
163     print "$view_tag already exists in $current_region\n";
164   } else {
165     print "$view_tag, from $view_region region, added to $current_region region\n" if MkViewTag $view_tag, $view_storage;;
166   } # if
167 } # if
168
169 if (defined $vob_tag) {
170   my ($vob_storage, $vob_region) = FindVob $vob_tag;
171
172   die "Unable to find vob $vob_tag in any region!\n"    if !defined $vob_region;
173   die "Unable to find storage area for $vob_tag\n"      if !defined $vob_storage;
174
175   if ($vob_region eq $current_region) {
176     print "$vob_tag already exists in $current_region\n";
177   } else {
178     print "$vob_tag, from $vob_region, added to $current_region region\n" if MkVobTag $vob_tag, $vob_storage;;
179   } # if
180 } # if
181
182 exit 0;
183