#!perl -w

# Copyright (C) 1997 Capella Computers Ltd.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# :FILE:
# Undo working on a job.

use strict;
use English;
use P4;

# Takes (optional) job id.
sub usage {
    P4::crash("Usage: p4wbu [<job>] [-d]");
}

# Parse command line.
my $job;
if ($#ARGV < 0) {
    $job = P4::current_job();
} elsif ($ARGV[0] eq '-h') {
    usage();
} elsif ($ARGV[0] =~ /^\d+$/) {
    $job = shift(@ARGV);
}
my $del_opt = '';
if ($#ARGV >= 0
 && $ARGV[0] eq '-d') {
    $del_opt = shift(@ARGV);
}
if ($#ARGV >= 0) {
    usage();
}

# Lock job to avoid race conditions.
P4::lock($job, 'p4wbu');

# Obtain the job record.
my %record = P4::job_record($job);

# Verify pre-conditions.
P4::verify_job_status($job, \%record, 'work');
P4::verify_job_user($job, \%record, P4::current_user());
P4::verify_job_development_client($job, \%record, P4::current_client());

# This helps in case something goes wrong.
$record{status} = '_new';
P4::update_job($job, \%record);

# Need to delete the fix to make change deletable.
my $change = $record{dev_chg};
P4::delete_fix($change, $job);

# Revert the state of all files already changed.
$::OUTPUT_AUTOFLUSH = 1;
print "### Revert development files ...\n";
my $retry = $record{retry};
P4::revert_files($change, $del_opt, "//depot/$user/$job-$retry/...");

# Delete the change for working on this branch.
P4::delete_change($change);

# Create a new change for removing the branch.
my $change = P4::create_change($user, "Undo work on $job-$retry by $user.");

# Remove every file in the branch. Since this removes all the files,
# we might as well remove the empty directory left there.
$::OUTPUT_AUTOFLUSH = 1;
print "### Delete development branch ...\n";
P4::delete_files($change, "//depot/$user/$job-$retry/...");
$::OUTPUT_AUTOFLUSH = 1;
print "### Delete development directory ...\n";
P4::remove_dir(P4::view_root() . "/$user/$job-$retry");

# Submit the change to perform the remove.
$change = P4::submit_change($change);

# This enables tracking job history.
P4::create_fix($change, $job);

# Delete the branch for working on this job.
my $branch = "$user-$job-$retry";
P4::delete_branch($branch);

# Update the job record with the change number.
$record{user} = 'nobody';
$record{status} = 'new';
$record{dev_chg} = '-';
$record{dev_client} = '-';
$record{retry}++;
P4::update_job($job, \%record);

# Release obtained locks.
P4::unlock();

# Notify caller.
print "Work on job $job (attempt #$retry) by $user has been undone.\n";

# Be nice.
1;