#!/usr/local/bin/perl5
# -*- perl -*-
use P4CGI ;
use strict ;
#
#####################################################################
##
##  CONFIGURATION INFORMATION 
##  All config info should be in $configFile (see init() in P4CGI.pm)
##
#####################################################################
##
##  CHange Viewer (by number)
##
#####################################################################

# Set back references
my $homepage="index.cgi";
my $backtohome="Back to Home" ;

# Get change number
my $change = P4CGI::cgi()->param("CH") ;
&P4CGI::bail("No change number specified") unless defined $change ;
$change =~ /^\d+$/ or  &P4CGI::bail("\"$change\" is not a positive number");
my @desc ;
my $currlev = &P4CGI::CURRENT_CHANGE_LEVEL() ;
if($change > $currlev or $change < 1) {
    &P4CGI::bail("\"$change\" is not a valid change number<br>(0 < change <= $currlev)");
}

&P4CGI::p4call(\@desc, "describe -s $change");

$_ = &P4CGI::fixSpecChar(shift @desc) ;

# Calculate penultimage change number
my $penultchg = ($change - 1) ;
my $nextchg_ref ;
my $nextchg = ($change + 1) ;
if ( $change == $currlev ) {
  $nextchg_ref = "" ; }
else {
  $nextchg_ref = "Go to change $nextchg <img height=13 border=0 width=13 src=../icons/right.gif>" ; }

# If the change was never submitted
if ( $_ eq "" ) {
    print
	"",
	&P4CGI::start_page("Change $change",
	"",
	$homepage,$backtohome,
	&P4CGI::ahref("-url",&P4CGI::CHV_URL(),
	    "CH=$penultchg",
	    "<img height=13 border=0 width=13 src=../icons/left.gif>
	    Go to change $penultchg"),
	&P4CGI::ahref("-url",&P4CGI::CHV_URL(),
	    "CH=$nextchg",
	    $nextchg_ref)) ;
    print
	"",
	"<font color=red>Change</font> <font color=green>$change</font> <font color=red>unknown.</font>",
	&P4CGI::end_page() ;
    exit 1 ;
}

# If output returned from p4 is wrong, bail
/^Change (\d+) by (\S+)@(\S+) on (\S+) (\d\d:\d\d:\d\d)(.*)$/ or &P4CGI::bail("Illegal syntax on line returned");
my ($chn, $user, $client, $date, $time, $status) = ($1,$2,$3,$4,$5,$6) ;

# Check for status pending
my $statstr = "" ;
my $pending ;
if ( defined $status and $status =~ /pending/) {
    $statstr=" (pending)" ;
    $pending = "yes" ;
} ;

# Print title and legend
print
    "",
    &P4CGI::start_page("Change $change$statstr",
    &P4CGI::ul_list(
	"<b>Author:</b> to see P4 user info",
        "<b>Client:</b> to see P4 client info",
	"<b>Action:</b> to see the deltas (diffs)",
	"<b>Rev:</b> to see the file text",
	"<b>Filename:</b> to see the complete file history"),
    $homepage,$backtohome,
    &P4CGI::ahref("-url",&P4CGI::CHV_URL(),
	"CH=$penultchg",
	"<img height=13 border=0 width=13 src=../icons/left.gif>
	 Go to change $penultchg"),
	&P4CGI::ahref("-url",&P4CGI::CHV_URL(),
	"CH=$nextchg",
	$nextchg_ref)) ;
    
# Set description
my $description="" ;
my $leadDescSpace ;
while(@desc > 0) {
    $_ = &P4CGI::fixSpecChar(shift @desc) ;
    chomp ;
    next if /^\s*$/;
    last if /^Jobs fixed/;
    last if /^Affected files/;
    if($_ !~ /^\s*$/) {
	if(defined $leadDescSpace) {
	    s/^$leadDescSpace// ;
	}
	else {
	    s/^(\s+)// ;
	    $leadDescSpace = $1 ;
	};
	$description .= "\n$_";
    }
}

# Set jobs
my $jobsFixed="-" ;
if( /^Jobs fixed/ )
{
    $jobsFixed = "\n" ;
    shift @desc ;
    while (@desc > 0) {
	$_ = &P4CGI::fixSpecChar(shift @desc) ;
	my( $job, $time, $user, $client );

	while( ( $job, $time, $user ) = 
	      /(\S*) on (\S*) by (\S*)/ )
	{
	    $jobsFixed .=  &P4CGI::ahref("-url","vj.cgi...",
						 "JOB=$job", 
						 $job) . "\n<br><tt>";
	    shift @desc ;
	    while(@desc > 0){
		$_ = &P4CGI::fixSpecChar(shift @desc) ;
		last if /^\S/;
		$jobsFixed .= $_ . "<br>";
	    }	    
	    $jobsFixed .= "</tt>\n";
	}
	
	last if /^Affected files/;
    }

    $jobsFixed .= "\n" ;
}

# Print it all out
print
    "",
    &P4CGI::start_table(""),
    &P4CGI::table_row("-valign","top",{"-type","th", "-align","right", "-text","Author:"},
	&P4CGI::ahref(-url => &P4CGI::LU_URL(), "USER=$user", $user)),
    &P4CGI::table_row("-valign","top",{"-type","th", "-align","right", "-text","Client:"},
	&P4CGI::ahref(-url => &P4CGI::CLV_URL(), "CLIENT=$client", $client)),
    &P4CGI::table_row("-valign","top",{"-type","th", "-align","right", "-text","Date:"},
	"$time $date"),
    &P4CGI::table_row("-valign","top",
	{"-type","th", "-align","right", "-text","Description:"},
	{"-text","<pre>".&P4CGI::magic($description)."</pre>","-bgcolor","white"}) ;
if ( ! defined $pending ) {
    print
	"",
	&P4CGI::table_row("-valign","top",{"-type","th", "-align","right", "-text","Jobs fixed:"},
	    "") ;
}

# End the table
print
    "",
    &P4CGI::end_table(),
    "<hr>";

if(! defined $pending ) {
    print
	"",
	&P4CGI::start_table("cellpadding=4 "),
	&P4CGI::table_row("-type","th",
			  "-align","left",
			  "Action","Rev","Filename") ;

    # Get file info to print
    my $allfiles ;
    my $allrevs ;
    my $allmodes ;
    my $cnt ;
    
    while(@desc > 0) {
	$_ = &P4CGI::fixSpecChar(shift @desc) ;
	if(/^\.\.\. (.*)#(\d*) (\S*)$/)  {
	   my( $file, $rev, $act ) = ($1,$2,$3) ;
	   my $filenosp = &P4CGI::fixspaces($file) ;
	   if(defined $allfiles) {
	       $allfiles .= ",$file" ;
	       $allrevs .= "+$rev" ;
	       $allmodes .= "+$act" ;
	   }
	   else {
	       $allfiles = "$file" ;
	       $allrevs  = "$rev" ;
	       $allmodes = "$act" ;
	   } ;
	   print
		"",
		&P4CGI::table_row(
		    &P4CGI::ahref("-url",P4CGI::FDV_URL(),
			"FSPC=$filenosp", "REV=$rev", "ACT=$act", "$act"),
			&P4CGI::ahref("-url",P4CGI::FV_URL(),
			"FSPC=$filenosp", "REV=$rev","$rev"),
			&P4CGI::ahref("-url",P4CGI::FLV_URL(),
			"FSPC=$filenosp", "$file")) ;
	 $cnt++ ;
       }
    }
    
    print &P4CGI::end_table(),"<P>" ;

    if ( $cnt > 1 ) {
	print
	    "",
	    &P4CGI::start_table( "width=100%" ),
		&P4CGI::table_row("-align","center",
		&P4CGI::ahref("-url",P4CGI::FDV_URL(),
			      "FSPC=$allfiles", 
			      "REV=$allrevs",
			      "ACT=$allmodes",
			      "CH=$change",
			      "<font size=+1>Diff all files in change</font>"));
    }
    print &P4CGI::end_table();
} 
else {
    my @openfiles ;
    &P4CGI::p4call(\@openfiles,"opened -a") ;
    
    my @files ;
    my @revs ;
    my @actions ;
    foreach (@openfiles) {
	if(/(\S+)#(\d+) - (\w+) change $change /) {	   
	   push @files,$1 ;
	   push @revs,$2 ;
	   push @actions,$3 ;
	}
    }

    print
	"",
	"<b>Files in changelist:</b>\n";

    if( scalar(@files) > 0 ) {
    print
	"",
	&P4CGI::start_table("cellpadding=4 "),
	&P4CGI::table_row("-type","th",
			  "-align","left",
			  "Action","Rev","Filename");
    }
    else {
	print
	    "<br>",
	    "&nbsp;&nbsp;<font color=red>None</font>",
	    &P4CGI::end_page() ; exit ;
    }

    while(@files > 0) {
	my $file = shift @files ;
	my $filenosp = &P4CGI::fixspaces($file) ;
	my $rev = shift @revs ;
	my $act = shift @actions ;
	if($act eq "edit") {
	print
	    "",
	    &P4CGI::table_row(
		&P4CGI::ahref("-url",P4CGI::FDV_URL(),
		"FSPC=$filenosp", "REV=$rev", "ACT=$act", "$act"),
		&P4CGI::ahref("-url",P4CGI::FV_URL(),
		"FSPC=$filenosp", "REV=$rev","$rev"),
		&P4CGI::ahref("-url",P4CGI::FLV_URL(),
		"FSPC=$filenosp", "$file")) ;
	}
	else {
	    print
		"",&P4CGI::table_row($act,$rev,"$file") ;
	}
    }
    print &P4CGI::end_table(),"<P>" ;

}

# End the page
print
    "",
    &P4CGI::end_page() ;