#! /usr/bin/perl
#
# NAME: renameclient.pl
# DESC: This script uses the original clientspec name to make a new clientspec,
# and then deletes the old clientspec.
# Command line equivalent
# p4 client -o OLD | sed '/^Client:/ c\Client: NEW' | p4 client -i
# OR
# p4 client -t OLD NEW
# OR
# p4 client -o OLD | sed 's/OLD/NEW/g' | p4 client -i
# p4 client -d OLD
#
# Other Command line workaround:
# p4 client -o OLD | sed 's/OLD/NEW/' | p4 client -i
# p4 client -d OLD
#
# * Note: Set the P4CLIENT variable to the NEW name
#
$user = shift ARGV; #The current user.
$server = shift ARGV; #ip_address:port of your Perforce server.
$clientold = shift ARGV; #The original clientspec name to rename.
$clientnew = shift ARGV; #The new clientspec name to replace original.
$p4 = "p4"; # Absolute path to the p4 cli program.
if ($clientold eq $clientnew)
{
print "\nNew clientspec name must be unique and not already exist.\n\nSelect a clientspec and try again.\n";
exit 1;
}
# Start with the rename process. Will replace all occurrences in the file thus mapping lines updated also.
$cmd = "\"$p4\" -u $user -p $server client -o \"$clientold\" | sed 's/$clientold/$clientnew/g' | p4 client -i |";
open RENAME, $cmd
or die "\nCannot run rename clientspec command.\n\n";
$count = 0;
while (<RENAME>)
{
# Run the command to delete the original clientspec
$cmd3 = "\"$p4\" -u $user -p $server client -d \"$clientold\" |";
open CMD3, $cmd3
or die "\nCannot perform a delete for original clientspec.\n\n";
while (<CMD3>)
{
# Run the command to refresh the clientspec list for the user
$cmd5 = "\"$p4\" -u $user -p $server clients -m 100 -u $user |";
open CMD5, $cmd5
or die "\nCannot run the clientspec list command.\n\n";
}
$count++;
}
if($count ne 0) { print "\nClientspec rename complete.\n\nClientspec renamed from $clientold to $clientnew.\nClientspec $clientold deleted.\n\nPlease make sure the P4CLIENT variable is set to the new name: $clientnew"; }
if($count eq 0) { print "\nNo clientspec rename performed.\n"; }
exit 0;