#!/usr/bin/perl -w
# -*- perl -*-
use P4CGI ;
use strict ;
#
#################################################################
#  CONFIGURATION INFORMATION 
#  All config info should be in P4CGI.pm
#
#################################################################
#
#  View labels
#
#################################################################

# Get label
my $label = P4CGI::cgi()->param("LABEL") ;
&P4CGI::bail("No label specified") unless defined $label ;
$label = &P4CGI::htmlEncode($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" ;
    } ;
}

# Print header
print &P4CGI::start_page("Label \"$label\"") ;

&P4CGI::signalError("Label $label not in depot") unless $found ;

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

###
### "Sort" other labels after "closeness"
###
{
    my $lab=uc($label) ;
    my $len = length($lab) ;
    my @labs ;
    while($len > 3) {
	my @tmp ;
	my $l ;
	$len-- ;
	$lab = substr($lab,0,$len) ;
	foreach $l (@otherLabels) {
	    if(uc(substr($l,0,$len)) eq $lab) {
		push @labs,$l ;
	    }
	    else {
		push @tmp,$l ;
	    }
	}
	@otherLabels = @tmp ;	
    } ;
    @otherLabels = (@labs,@otherLabels) ;
}


my %values ;
my @fields = &P4CGI::p4readform("label -o '$label'",\%values) ;

				# Fix description field
if(exists $values{"Description"}) {
    $values{"Description"} = 
	&P4CGI::formatDescription($values{"Description"}) ;
}
				# Fix owner field
if (exists $values{"Owner"}) {
    # Get real user names...
    my %userCvt ;
    &P4CGI::p4user2name(\%userCvt) ;
    my $u = $values{"Owner"} ;
    if(exists $userCvt{$u}) {
	$values{"Owner"} = &P4CGI::ahref(-url=>"userView.cgi",
					 "USER=$u",
					 "HELP=View user info",
					 "$u") . " (" . $userCvt{$u} . ")" ; 
    }
    else {
	$values{"Owner"} = "$u (Unknown user)" ;
    }
}
				# Fix view field
my $viewFSPC = $values{"View"} ;
my $view ;
foreach (split("\n",$values{"View"})) {
    my $t = &P4CGI::ahref(-url => "depotTreeBrowser.cgi",
			  "FSPC=$_",
			  "HELP=Browse depot at $_",
			  "<tt>$_</tt>") ;
    if (defined $view) {
	$view .= "<br>$t" ;
    } 
    else {
	$view .= "$t" ;
    } ;
} ;
$values{"View"} = $view ;

    
print 
    "<br>",
    &P4CGI::start_framedTable(""),
    &P4CGI::start_table("") ;

my $f ;
foreach $f (@fields) {
    my %x ;
    $x{class} = "Description" if $f eq "Description"  ;
    print &P4CGI::table_row({-class =>"Prompt",
			     -text  => "$f"},
			    {%x,
			     -text =>  $values{$f}},
			    "") ;
} ;


$viewFSPC =~ s/\n//ig ;
my @b ;
push @b,
    (&P4CGI::buttonLink("changeList.cgi",
			"View changes for label $label",
			"LABEL=$label",
			"Submits..."),
     &P4CGI::buttonLink("fileSearch.cgi",
			"List files in label $label",
			"LABEL=$label",
			"Files"),
     &P4CGI::buttonLink("changeList.cgi",
			"List changes in label view not included in label $label",
			"FSPC=$viewFSPC",
			"EXLABEL=$label",
			"Changes after label")) ;

print    
    &P4CGI::table_row("",undef,     
		      join(" ",@b)) ;

print &P4CGI::end_table() ;

print    
    &P4CGI::table_row(undef,"<br>") ;

my $diffSel = 
    join("\n",
	 ("<table>",
	  "<tr>",
	  &P4CGI::cgi()->startform(-action => "labelDiffView.cgi" ,
				   -method => "GET"),
	  &P4CGI::cgi()->hidden(-name=>"DP",
				-value=>&P4CGI::CURR_DEPOT_NO()),
	  &P4CGI::cgi()->hidden(-name=>"LABEL1",
				-value=>"$label"),
	  "<td>Diff with label:</td>",	
	  "<td>",
	  &P4CGI::cgi()->popup_menu(-name  => "LABEL2",
				    -value => \@otherLabels),
	  "</td>",
	  "<td>Show files that:</td>",
	  "<td>",
	  &P4CGI::cgi()->checkbox(-name    => "SHOWSAME",
				  -value   => "Y",
				  -label   => " are not modified"),
	  "<br>",
	  &P4CGI::cgi()->checkbox(-name    => "SHOWNOTSAME",
				  -checked => "Y",
				  -value   => "Y",
				  -label   => " are modified"),
	  "<br>",
	  &P4CGI::cgi()->checkbox(-name    => "SHOWDIFF",
				  -checked => "Y",
				  -value   => "Y",
				  -label   => " differ"),
	  "</td>",
	  "<td>",
	  &P4CGI::cgi()->submit(-name  => "Go",
				-value => "Go"),
	  &P4CGI::cgi()->endform(),
	  "</td></tr>",
	  "</table>")) ;

print    
    &P4CGI::table_row(undef,undef,$diffSel) ;

print    
    &P4CGI::table_row(undef,"<br>") ;

my $diffcSel = 
    join("\n",
	 ("<table>",
	  "<tr>",
	  &P4CGI::cgi()->startform(-action => "labelDiffView.cgi" ,
				   -method => "GET"),
	  &P4CGI::cgi()->hidden(-name=>"LABEL1",
				-value=>"$label"),
	  &P4CGI::cgi()->hidden(-name=>"DP",
				-value=>&P4CGI::CURR_DEPOT_NO()),
	  "<td>Diff with current detpot files in label view. </td>",	
	  "<td>&nbsp;&nbsp;Show files that:</td>",
	  "<td>",
	  &P4CGI::cgi()->checkbox(-name    => "SHOWSAME",
				  -value   => "Y",
				  -label   => " are not modified"),
	  "<br>",
	  &P4CGI::cgi()->checkbox(-name    => "SHOWNOTSAME",
				  -checked => "Y",
				  -value   => "Y",
				  -label   => " are modified"),
	  "<br>",
	  &P4CGI::cgi()->checkbox(-name    => "SHOWDIFF",
				  -checked => "Y",
				  -value   => "Y",
				  -label   => " differ"),
	  "</td>",
	  "<td>",
	  &P4CGI::cgi()->submit(-name  => "Go",
				-value => "Go"),
	  &P4CGI::cgi()->endform(),
	  "</td></tr>",
	  "</table>")) ;

print
    &P4CGI::table_row(undef,undef,$diffcSel) ;

print "<tr><td colspacing=2>" ;
print "<br>", 
    &P4CGI::cgi()->startform(-action => "changeList.cgi",
			     -method => "GET"), 
    &P4CGI::cgi()->hidden(-name=>"LABEL",
			  -value=>"$label"), "View changes for label $label excluding label:",
    &P4CGI::cgi()->hidden(-name=>"DP",
			  -value=>"&P4CGI::CURR_DEPOT_NO()"),    
    &P4CGI::cgi()->popup_menu(-name => "EXLABEL", -value =>
			      \@otherLabels), &P4CGI::cgi()->submit(-name => "Go", -value =>
								    "Go"), 
    &P4CGI::cgi()->endform() ;

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

print "</td></tr>" ;

print 
    &P4CGI::end_framedTable(),
    &P4CGI::end_page() ;

#
# That's all folks
#