#! /usr/bin/perl
# NAME: renamejob.pl
# DESC: This script uses the original jobspec name to make a new jobspec,
# and then deletes the old jobspec.
#
# Command line equivalent
# p4 job -o OLD | sed '/^Job:/ c\Job: NEW' | p4 job -i
# p4 job -d OLD
#
# 1) In P4V, Click on Tools -> Manage Custom Tools. Click the 'New'
# button and select 'Tool'.
#
# 2) Fill in the 'Name' field to identify the operation.
# For example: 'Rename Job'
#
# 3) In P4V, under 'Application', enter in the path where
# 'renamejob.pl' is located. Such as: C:\customtools\renamejob.pl
#
# 4) For the 'Arguments' enter these four in this order:
#
# %j $u $p $D
#
# 5) In P4V: Click the check-boxes for 'Add to applicable context menus';
# 'Run tool in terminal window'.
#
# Also, click the check-boxes for 'Prompt user for arguments' and
# under 'Description' for the prompt you can enter something like
# "Enter new job name:".
#
# 6) Click the 'OK' button and save your changes.
#
#
$jobold = shift ARGV; #The original jobspec name to rename.
$user = shift ARGV; #The current user.
$server = shift ARGV; #ip_address:port of your Perforce server.
$jobnew = shift ARGV; #The new jobspec name to replace original.
$p4 = "p4"; # Absolute path to the p4 cli program.
if ($jobold eq $jobnew)
{
print "\nNew jobspec name must be unique and not already exist.\n\nSelect a jobspec and try again.\n";
exit 1;
}
$cmd = "\"$p4\" -u $user -p $server job -o \"$jobold\" | sed '/^Job:/ c\Job: \"$jobnew\"' | p4 job -i |";
open RENAME, $cmd
or die "Cannot run rename jobspec command.\n\n";
$count = 0;
while (<RENAME>)
{
# Run the second command to delete the original jobspec
$cmd2 = "\"$p4\" -u $user -p $server job -d \"$jobold\" |";
open DELORIG, $cmd2
or die "Cannot run delete for original jobspec command.\n\n";
$count++;
}
if($count ne 0) { print "\nJobspec rename complete.\n\nJobspec renamed from $jobold to $jobnew.\nJobspec $jobold deleted.\n"; }
if($count eq 0) { print "\nNo jobspec rename performed.\n"; }
exit 0;