#!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:
# Get list of job file operations.

use strict;
use English;
use P4;

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

# 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);
}
if ($#ARGV >= 0) {
    usage();
}

# No need to lock since this is a harmless read-only operation.

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

# We have to do different things, depending on the status of the job.
# In all cases, we fill the 'job_files' hash with the job's file operations.
my %job_files;
my $status = $record{status};
if ($status eq 'work') {
    # Get the list of files opened for this job.
    P4::verify_job_development_client($job, \%record, P4::current_client());
    %job_files = P4::opened_files($job, \%record);
    # Add files in any old submit.
    P4::old_opened_files($job, \%record, \%job_files);
} elsif ($status eq 'review'
      || $status eq 'pass'
      || $status eq 'fail') {
    # Here there's only "old" changes.
    P4::old_opened_files($job, \%record, \%job_files);
} elsif ($status eq 'integ') {
    # Here we get the opened files in the _integration_ directory.
    # Note that this only works on the integration machine.
    P4::opened_files($job, \%record);
} elsif ($status eq 'clear'
      || $status eq 'done') {
    %job_files = P4::change_files($record{int_chg});
} else {
    P4::crash("p4jf is not available for jobs in status '$status'.");
}

# Print column titles.
print "Op.\tFile\n";
print "---\t----\n";

# Print all files, sorted by name.
my $file;
foreach $file (sort(keys(%job_files))) {
    my $op = $job_files{$file};
    # Print a nice line.
    print $op, "\t";
    print $file, "\n";
}

# Be nice.
1;