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

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

sub equalChars($$ ) ;

# Get label
my $label = P4CGI::cgi()->param("LABEL") ;
&P4CGI::bail("No label specified") unless defined $label ;

my $found ;
# Get list of all labels and also check that supplied label exists
my @labels ;
&P4CGI::p4call(\@labels, "labels" );
foreach (@labels) {
    $_ =~ s/^Label (\S+).*$/$1/ ;
    if($_ eq $label) {
	$found = "Yes" ;
    } ;
}
&P4CGI::bail("Label $label not in depot") unless $found ;

# Print title and legend
print
    "",
    &P4CGI::start_page("Label Information",
	&P4CGI::ul_list("<b>Owner:</b> to see P4 user info",
	    "<b>View:</b> to see changes for view",
	    "<hr>",
	    &P4CGI::ahref( -url => &P4CGI::CHB_URL(),
		"FSPC=//...",
		"LABEL=$label",
		"View changes for label"),
	    &P4CGI::ahref( -url => &P4CGI::SFF_URL(),
		"LABEL=$label",
		"List files in label")),
	$homepage,$backtohome) ;

my @allOtherLabels ;
foreach (@labels) {    
    next if ($_ eq $label) ;
    push @allOtherLabels,$_ ;
} ;

my @otherLabels ;
my @different ;
my $firstword ;
($firstword = $label) =~ s/(\w+).*/$1/ ;
while(@allOtherLabels > 0) {
    my $l = shift @allOtherLabels ;
    my $w = $l ;
    $w =~ s/(\w+).*/$1/ ;
    
    if($w eq $firstword) {
	push @otherLabels,$l ;
    }
    else {
	push @different,$l ;
    } ;
} ;
push @otherLabels,@different ;
    
# Get label info
print 
    "",
    &P4CGI::start_table("") ;


local *P4 ;
my $date ;
my $owner ;
my $desc ;
my $options ;
my @view ;
&P4CGI::p4call(*P4, "label -o $label" );
while(<P4>) {
    chomp ;
    next if /^#/ ;
    next if /^\s*$/ ;
    /^Date:\s(.*)$/ and do { $date=$1 ; } ;
    /^Owner:\s(.*)$/ and do { $owner=$1 ; } ;
    last if /^Description:/ ;
}
my $descWhiteSpace ;
while(<P4>) {
    chomp ;
    next if /^\s*$/ ;
    if(/^Options:\s+(.*)/) {
	$options = $1 ;
	next ;
    } ;
    last if /^View:/ ;
    unless(defined $descWhiteSpace) {
	/^(\s*)/ ;
	$descWhiteSpace = $1 ;	    
    } ;
    s/^$descWhiteSpace// ;
    if(defined $desc) {
	$desc .= "<br>$_" ;
    }
    else {
	$desc .= $_ ;
    }
} ;

while(<P4>) {
    chomp ;
    next if /^\s*$/ ;
    push @view,$_ ;
}
close P4 ;

# Fix up data
$owner = &P4CGI::ahref(-url => &P4CGI::LU_URL(),
		       "USER=$owner",
		       $owner) ;
my $view ;
foreach (@view) {
    s/^\s*(\S+\s+\S+)\s*/$1/ ;
    my $t = &P4CGI::ahref(-url => &P4CGI::CHB_URL(),
			  &P4CGI::fixspaces("FSPC=$_"),
			  $_) ;
    if (defined $view) {
	$view .= "<br>$t" ;
    } 
    else {
	$view .= "$t" ;
    } ;
} ;

print 
    "",
    &P4CGI::table_row({-type=>"th",
		       -align=>"right",
		       -valign=>"bottom",
		       -text=>"Label:"},
		      "<font color=green>$label</font>"),
    &P4CGI::table_row({-type=>"th",
		       -align=>"right",
		       -text=>"Date:"},
		      "$date"),
    &P4CGI::table_row({-type=>"th",
		       -align=>"right",
		       -text=>"Owner:"},
		      "$owner"),
    &P4CGI::table_row({-type=>"th",
		       -align=>"right",
		       -valign=>"top",
		       -text=>"Description:"},
		      {-bgcolor => "white",
		       -text    => "<tt>$desc</tt>"}),
    &P4CGI::table_row({-type=>"th",
		       -align=>"right",
		       -text=>"Options:"},
		      "$options"),
    &P4CGI::table_row({-type=>"th",
		       -align=>"right",
		       -valign=>"top",
		       -text=>"View:"},
		      "<tt>$view</tt>"),
    &P4CGI::end_table() ;

print 
    "<hr>",
    &P4CGI::cgi()->startform(-action => &P4CGI::LDV_URL(),
			     -method => "GET"),
    &P4CGI::cgi()->hidden(-name=>"LABEL1",
			  -value=>"$label"),
    "Diff with label: ",	
    &P4CGI::cgi()->popup_menu(-name  => "LABEL2",
			      -value => \@otherLabels),
    "<br>Show files that: ",
    &P4CGI::cgi()->checkbox(-name    => "SHOWSAME",
			    -value   => "Y",
			    -label   => " are not modified"),
    &P4CGI::cgi()->checkbox(-name    => "SHOWNOTSAME",
			    -checked => "Y",
			    -value   => "Y",
			    -label   => " are modified"),
    &P4CGI::cgi()->checkbox(-name    => "SHOWDIFF",
			    -checked => "Y",
			    -value   => "Y",
			    -label   => " differ"),
    &P4CGI::cgi()->submit(-name  => "Go",
			  -value => "Go"),
    &P4CGI::cgi()->endform() ;


print
    "<hr>",
    &P4CGI::cgi()->startform(-action => &P4CGI::CHB_URL(),
			     -method => "GET"),	
    &P4CGI::cgi()->hidden(-name=>"LABEL",
			  -value=>"$label"),
    "View changes for label <font color=green>$label</font> excluding changes also found in label: ",	
    &P4CGI::cgi()->popup_menu(-name  => "EXLABEL",
			      -value => \@otherLabels),
    &P4CGI::cgi()->submit(-name  => "Go",
			  -value => "Go"),
    &P4CGI::cgi()->endform() ;

print
    "<hr>",
    &P4CGI::cgi()->startform(-action => &P4CGI::SFF_URL(),
			     -method => "GET"),
    &P4CGI::cgi()->hidden(-name=>"LABEL",
			  -value=>"$label"),
    &P4CGI::cgi()->submit(-name  => "ignore",
			  -value => "Search in label for:"),
    &P4CGI::cgi()->textfield(-name      => "FSPC",
			     -default   => "//...",
			     -size      => 50,
			     -maxlength => 256),    
    &P4CGI::cgi()->endform() ;

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


sub equalChars($$ ) 
{
    my $a = shift @_ ;
    my $b = shift @_ ;
    my $res = 0 ;
    while(substr($a,$res,1) eq substr($b,$res,1)) {
	$res++ ;
    }
    return $res ;
}