#!/usr/bin/perl -w
# -*- perl -*-
use P4CGI ;
use strict ;
#
#################################################################
#  CONFIGURATION INFORMATION 
#  All config info should be in P4CGI.pm
#
#################################################################
#
#  P4 change browser
#  View list of changes for selected part of depot
#
#################################################################

# Get file spec argument
my $filespec = P4CGI::cgi()->param("FSPC") ;
$filespec = "//..." unless defined $filespec ;

# Get label argument
my $label = P4CGI::cgi()->param("LABEL") ;
if(defined $label and $label eq "-") { $label = undef ; } ;

# Get spec of files to exclude from list of changes
my $exfiles = P4CGI::cgi()->param("EXFSPC") ;
if(defined $exfiles and $exfiles eq "-") { $exfiles = undef ; } ;

# Get label to exclude
my $exlabel = P4CGI::cgi()->param("EXLABEL") ;
if(defined $exlabel and $exlabel eq "-") { $exlabel = undef ; } ;

# Get status
my $status = P4CGI::cgi()->param("STATUS") ;
unless(defined $status) { $status = "submitted" ; } ;

# Get max changes to show
my $maxchanges = P4CGI::cgi()->param("MAXCH") ;
if(defined $maxchanges && $maxchanges =~ /^\d+$/) {
    $maxchanges = "-m $maxchanges" ;
}
else {
    $maxchanges = "" ;
}

				# Assume filespec is a file spec, set title 
my $title = "Changes for $filespec" ;

if(defined $label) {
    if($filespec eq "//...") {
	$title ="Changes for label <code>$label</code>";
	$filespec .= "\@$label" ;
    }
    else {
	$title .= " and label $label" ;
	$filespec =~ s/ /@$label / ;
	$filespec .= "\@$label" ;
    }
}

				# Get list of changes to exclude
my @exclude;

if(defined $exlabel) {
    if(defined $exfiles) {
	$exfiles .= "\@$exlabel" ;
    }
    else {
	$exfiles = "//...\@$exlabel" ;
    }
}

local *P4 ;

if(defined $exfiles) {
    &P4CGI::p4call(*P4,"changes $exfiles") ;
    $title .= " <br>excluding changes in <code>$exfiles</code>" ;
    while(<P4>) {
	/^Change (\d+)/ ;
	    push @exclude,$1 ;
    }
    close P4 ;	
    push @exclude,0 ;
}

if($status eq "pending") {
    $title = "Pending changes" ;
    $filespec = "" ;
} ;
&P4CGI::p4call(*P4,"changes -l $maxchanges -s $status $filespec") ;

print
    "",
    &P4CGI::start_page($title,
		       &P4CGI::ul_list("<b>Change No.</b> -- see details of change")) ;
my $nextToExclude = shift @exclude ;
if(!defined $nextToExclude) { $nextToExclude = 0 ; } ;

my ( $change, $misc ) ;

my $skipped=0 ;
my $skip;
print "<dl>" ;
while (<P4>) {
    $_ = &P4CGI::fixSpecChar($_) ;
    if(/^Change (\d+) (.*)$/) {
	$skip="no" ;
	( $change, $misc ) = ($1,$2) ;
	while($nextToExclude > $change) {
	    if($skipped>0) {
		print 
		    "<dt><font color=green><hr>\n",
		    "$skipped change" , 
		    $skipped > 1 ? "s":"",
		    "<hr></font>\n";
	    };
	    $skipped=0 ;
	    print
		"<dt>",
		&P4CGI::ahref("-url",P4CGI::CHV_URL(),
			      "CH=$nextToExclude",
			      "<font color=red>Change $nextToExclude only in $exfiles</font>"),
		"<dd>  ";
	    $nextToExclude = shift @exclude ;
	}
	if ($change == $nextToExclude) {
	    $nextToExclude = shift @exclude ;
	    $skip="yes" ;
	    $skipped++ ;
	    next ;
	}
#	last if ($fromChange != 0) and ($change <= $fromChange)  ;
	if($skipped>0) {
	    print 
		"<dt><font color=green><hr>\n",
		"$skipped change" , 
		$skipped > 1 ? "s":"",
		"<hr></font>\n";
	}
	$skipped=0 ;
	print 
	    "<dt>",
	    &P4CGI::ahref("-url",P4CGI::CHV_URL(),
			  "CH=$change",
			  "Change $change "),
	    "$misc<dd>\n";
    }
    else {
	next if $skip eq "yes";
	chop;
	print &P4CGI::magic("<tt>$_</tt><br>\n") ;
    }
}

close P4;

print "</dl>\n",&P4CGI::end_page();

#
# That's it folks
#