#!/usr/bin/perl -w
#
# DISCLAIMER:
#
# THIS IS ONLY A SAMPLE SCRIPT.
#
# Neither Perforce Software nor the author of the script is
# responsible for any damage incurred by the use of the script.
#
# YOU ARE USING THIS SCRIPT AT YOUR OWN RISK.
#
# PLEASE TEST THE SCRIPT THOROUGHLY AGAINST A TEST SERVER
# BEFORE USING IT AGAINST THE PRODUCTION SERVER
#
# I have tested the script on a Windows XP machine as well as
# a Linux machine (Fedora Core 8), but you may run into a problem
# depending on your environment and you will want to modify
# the script accordingly.
#
# The script was also tested with "p4 counter security 2"
# It seemed to work fine, but you may need to modify
# the script with a "-u <user>" and/or a "-p <port>" flag(s).
#
# Summary:
#
# The script accepts one or two arguments.
#
# The user will need to type:
#
# addLineDelLine.pl -h
#
# at the command prompt to display different flags available for
# this command.
#
# First argument is a flag and the second argument is a name of
# depot. Be careful in using the depot name since a wrong name
# results in truncating the client's specificaition thus wiping
# out the View: field. Although a validation check is made to
# enure that the user uses a valid depot name, the scrpt is
# not tested thoroughly to account for all possible cases.
#
# Whenver the script is run, it creates "clients" directory under
# the directory where the script is run and it saves "temp*.txt" files
# for all clients in the depot. If you are concerned about losing
# a user's client specs, please run
#
# addLineDelLine.pl -s <depot_name>
#
# beforehand so that you have a backup although you may be able to
# recover them if you have spec depot.
use strict;
use Fatal qw/ open /;
my $p4 = "p4";
my $clients = "clients";
my $depot = ""; # needs to be changed to your depot name
my $dirname = "clients";
# my @OS = qw/MSWin32 cygwin linux aix solaris/;
my $flag = "";
my $filename = "";
my $counter = 1; # filename counter
my $argc = @ARGV;
my @depots = `p4 depots`;
my $isDepot = 0; # is not a valid depot name
if($argc > 2 or $argc == 0) { die "Type 'addLineDelLine.pl -h' for help\n"; }
if($ARGV[0] eq "-h") {
print "Usage: addLineDelLine.pl <flag> <depot>\n";
print "\n\tflags:\n";
print "\t\t-h => this help\n";
print "\t\t-s => saves the client specs in the specified depot\n";
print "\t\t-a => add a path to the View: field of client spec\n";
print "\t\t-d => delete a path in the View: field of client spec\n";
print "\t\t-ad => add and delete a path in the View: field of client spec\n\n";
exit 1;
}
if($ARGV[0] eq "-s") {
$flag = "save";
} elsif ($ARGV[0] eq "-a") {
$flag = "add";
} elsif ($ARGV[0] eq "-d") {
$flag = "delete";
} elsif ($ARGV[0] eq "-ad") {
# do nothing
} else {
die "No flag defined: $!\n";
}
if(!defined($ARGV[1])) {
die "No path was defined\n";
} elsif($ARGV[1] =~ /^\/\/.*$/) {
die "Depot name should not contain two forward slashes\n";
} elsif($ARGV[1] =~ /^\/\/.*\/$/) {
die "Depot name should not contain two forward nor a slash at the end of the name\n";
} elsif($ARGV[1] =~ /^.*\/$/) {
die "Depot name should not contain a slash at the end of the name\n";
}
foreach (@depots) {
my ($d1,$depotName) = split / /,$_,3;
if($ARGV[1] eq $depotName) {
$isDepot = 1; # it is a valid depot name
}
}
if ($isDepot == 0) {
die "$ARGV[1] is an invalid depot name\n";
}
$depot = $ARGV[1];
# all the client spec forms will be stored under here
mkdir $dirname or die "The previous \'$dirname\' directory exists\n";
open(LOG,">$dirname/log.txt"); # log of line add and delete to client form
print LOG "$^O\n";
my @clients = `$p4 $clients`;
sub processClients {
my($clientsRef,$counter,$status) = @_;
foreach(@$clientsRef) {
my($d1,$client) = split / /, $_,3; # extracts the client ws
my @clientData = ();
@clientData = `$p4 client -o $client`;
if($status eq "save") {
#do nothing
} else {
pop(@clientData); # removes the empty line at the end
if ($status eq "add") {
my $viewPath = "\t//$depot/...\t//$client/...\n";
push @clientData,$viewPath; #adds the above View: path
} elsif ($status eq "delete") {
pop(@clientData); # removes the last line
}
}
open(TEMP,">$dirname/temp" . $counter . ".txt");
foreach (@clientData) {
print TEMP "$_";
}
close(TEMP);
# updates the client spec form
`$p4 client -i < $dirname/temp$counter.txt`;
$filename = "$dirname/temp$counter.txt";
print LOG "$filename: $client, $status\n";
$counter++;
}
}
# main
# saves the client specs of the users
if ($flag eq "save") {
&processClients(\@clients,$counter,$flag);
}
# adds a line at the end of the client spec form
elsif ($flag eq "add") {
&processClients(\@clients,$counter,$flag);
}
# deletes the line at the end of the client spec form
elsif ($flag eq "delete") {
&processClients(\@clients,$counter,$flag);
} else {
&processClients(\@clients,$counter,"add");
&processClients(\@clients,$counter,"delete");
}
close(LOG);