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

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

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

my $label = P4CGI::cgi()->param("LABEL") ;
if(!defined $label) {
    $label = "" ;
}
my $filedesc ;
my $showDiffSelection="Y" ;
if($filespec eq "") {
    $filedesc = " <small>label</small><br><code>$label</code>" ;
    $showDiffSelection= undef ;
}
else {
    $filedesc = "<br><code>$filespec</code>" ;
    if($label ne "") {
	$filedesc .= "<br><small>in label</small><br><code>$label</code>" ;
    }
}
if($label ne "") {
    $label = "\@$label" ;
}

# Add //... if not there
if($filespec !~ /^\/\//) {
    $filespec = "//...$filespec" ;
}				
while($filespec =~ s/\.\.\.\.\.\./\.\.\./) { ; } ;
while($filespec =~ s/\*\*/\*/) { ; } ;

#Check if file exists
my @matches ;
&P4CGI::p4call(\@matches, "files \"${filespec}${label}\"" );

# Set what we searched on
my $search ;
if ( $label )
{
  $label =~ /\@(.*)/ ;
  $search = "$1" ; }
else {
  $search = $filespec ;
}

# Print title and legend
print
    "",
    &P4CGI::start_page("Search Results",
	&P4CGI::ul_list(
	    "<b>Change:</b> to see the complete change description,
		including other files",
	    "<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);

# Print the files
if( $label eq "" ) {
  print
    "",
    "<b>Search on:</b><font color=green> $search</font>" ;
}
else {
  print
    "",
    "<b>Search on label:</b><font color=green> $search</font>" ;
}
if(scalar(@matches) == 0) {
    print
	"",
	"<br>",
	"<b>0</b> files found";
}
else {
    print
	"",
	"<br>",
	"<b>", scalar(@matches),
	"</b> file",
	scalar(@matches)==1 ? "" : "s",
        " found",
	"<hr>",
	&P4CGI::start_table("cellpadding=4"),
	&P4CGI::table_row("-type","th",
			  "-align","left",
			  "Change","Action","Rev","Filename");
	my $f ;
	foreach $f (@matches) {
	$f =~ /([^\#]+)\#(\d+) - (\w+) change (\d+)/ ;
	my ($name,$rev,$act,$change)=($1,$2,$3,$4) ;
	print
	    "",
	    &P4CGI::table_row(&P4CGI::ahref("-url",P4CGI::CHV_URL(),
		"CH=$change", $change),
	    &P4CGI::ahref("-url",P4CGI::FDV_URL(),
		&P4CGI::fixspaces("FSPC=$name"),
		    "REV=$rev",
		    "ACT=$act",
		    $act),
	    &P4CGI::ahref("-url",P4CGI::FV_URL(),
		&P4CGI::fixspaces("FSPC=$name"),
		    "REV=$rev",
		    $rev),
	    &P4CGI::ahref("-url",P4CGI::FLV_URL(),
		&P4CGI::fixspaces("FSPC=$name"),
		    $name)) ;
	}  ;
	print "",&P4CGI::end_table() ;
	my @files ;
	my %filesToFiles ;
	foreach $f (@matches) {
	    $f =~ /([^\#]+)\#(\d+) - (\w+) change (\d+)/ ;
	    my ($name,$rev,$act,$change)=($1,$2,$3,$4) ;
	    if($act ne "delete") {
		push @files,"$name\#$rev" ;
	    }
	}
	if(defined $showDiffSelection and @files > 1) {
	    print
		&P4CGI::cgi()->startform("-action",&P4CGI::FDV_URL(),
					 "-method","GET"),
		&P4CGI::cgi()->hidden("-name","ACT",
				      "-value","NO"),    
		"View diff between:<br>",    
		&P4CGI::cgi()->popup_menu(-name   => "FSPC",
					  -values => \@files);
		shift @files ;
	    print
		"and<br>",
		&P4CGI::cgi()->popup_menu(-name   => "FSPC2",
					  -values => \@files),
		&P4CGI::cgi()->submit("-name", "ignore",
				      "-value"=>"Go"),
		&P4CGI::cgi()->endform() ;
	}
}

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