#!/usr/bin/perl -w
# -*- perl -*-
use P4CGI ;
use strict ;
#
#################################################################
#  CONFIGURATION INFORMATION 
#  All config info should be in P4CGI.pm
#
#################################################################
#
#  P4 file log viewer
#
#################################################################

local *P ;

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

&P4CGI::bail("No file spec") unless defined $file ;

# Get file data
my @filelog ;
&P4CGI::p4call(\@filelog,"filelog $file") ;

&P4CGI::bail("No data for file \"$file\"") if @filelog == 0 ;

# Get info about opened status
&P4CGI::p4call(*P,"opened -a $file 2>/dev/null") ;
my %opened ;
while(<P>) {
    chomp ;
    /\w+\#(\d+) - .* by (\w+)\@(\S+)/ or 
	&P4CGI::bail("Can not read info from \"p4 opened\"") ;
    if(exists $opened{$1}) {
	$opened{$1} .= "<br> and " . &P4CGI::ahref( -url => &P4CGI::LU_URL(),
						   "USER=$2",
						   "$2") . "\@$3" ;
    } else {
	$opened{$1} = &P4CGI::ahref( -url => &P4CGI::LU_URL(),
				    "USER=$2",
				    "$2") . "\@$3" ;
    } ;
} ;
close *P ;

# Get list of labels (if $listLabel is set)
my @labels ;
if($listLabel eq "Yes") {
    &P4CGI::p4call(*P,"labels") ;
    while(<P>) {
	/^Label (\S+)/ and do { push @labels,$1 ; } ;
    }
    close P ;
}
				# Create hash containing labes by file name and version
my %fileToLabels ;
if(@labels > 0) {
    my $filelabels = "" ;
    foreach (@labels) {
	$filelabels .= " $file\@$_" ;
    }
    my @filesInLabels ;
    &P4CGI::p4call(\@filesInLabels,"files $filelabels 2>&1") ;
    foreach (@filesInLabels) {
	my $lab = shift @labels ;
	/not in label/ or do {
	    /^(\S+)/ ;
	    if(defined $fileToLabels{$1}) {
		$fileToLabels{$1} .= "<br>$lab" ; 
	    } 
	    else {
		$fileToLabels{$1} = "$lab" ; 
	    }
	}	
    }
} ;


my @legendList ; 
push @legendList,
    "<b>Revision Number</b> -- see the file text",
    "<b>Action</b> -- see the deltas (diffs)",
    "<b>User</b> -- see info about user",
    "<b>Change</b> -- see the complete change description, including other files",
    &P4CGI::ahref("-url",P4CGI::CHB_URL(),
		  "FSPC=$file",
		  "Changes") . "-- see list of all changes for this file" ;

if($listLabel ne "Yes") {
push @legendList,
    &P4CGI::ahref("-url",P4CGI::FLV_URL(),
		  "FSPC=$file",
		  "LISTLAB=Yes",
		  "List labels") . "-- list cross ref. for labels" ;
} ;

print "",&P4CGI::start_page("File log<br>$file",&P4CGI::ul_list(@legendList)) ;

my $labelHead ="";
if($listLabel eq "Yes") {
    $labelHead="In label(s)" ;
} ;

print
    "",
    &P4CGI::start_table(""),
    &P4CGI::table_row("-type","th",
		      "-align","left",
		      "Rev","Act.","Date","User","Change","Desc",$labelHead,"Opened by");

my $log ;
my @revs ;
for($log = shift @filelog ; defined $log ; $log = shift @filelog) {
    $_ = &P4CGI::fixSpecChar($log) ;
    if(/^\.\.\. \#(\d+) \S+ (\d+) (\S+) on (\S+) by (\S*)@(\S*) \((\S*)\) '(.*)'/ )
    {
	my ($rev,$change,$act,$date,$user,$client,$ftype,$desc) =($1,$2,$3,$4,$5,$6,$7,$8) ;
	$desc = &P4CGI::magic($desc) ;
	push @revs,$rev ;
	my $labels = $fileToLabels{"$file\#$rev"} ;
	$labels = "" unless defined $labels ;
	$labels = "<b>$labels</b>" ;
	if ($act eq 'branch') {
	    $_ = &P4CGI::fixSpecChar(shift @filelog) ;
	    my ($fromname,$fromrev) = /^.*branch from (\S+?)\#(\d+).*/;
	    print
		"",
		&P4CGI::table_row(-valign => "top",
				  &P4CGI::ahref("-url",&P4CGI::FV_URL(),
						"FSPC=$file",
						"REV=$rev",
						"$rev"),
				  &P4CGI::ahref("FSPC=$fromname",
						"REV=$fromrev",
						"$act"),
				  "$date",
				  &P4CGI::ahref(-url => &P4CGI::LU_URL(),
						"USER=$user",
						"$user"),
				  &P4CGI::ahref("-url",&P4CGI::CHV_URL(),
						"CH=$change",
						"$change"),
				  "<tt>$desc</tt>",
				  $labels,
				  exists $opened{$rev}?$opened{$rev}:"") ;
	}
	elsif ($act eq 'delete') {
	    print
		"",
		&P4CGI::table_row(-valign => "top",
				  &P4CGI::ahref("-url",&P4CGI::FV_URL(),
						"FSPC=$file",
						"REV=$rev",
						"$rev"),
				  "<strike>delete</strike>",
				  "$date",
				  &P4CGI::ahref(-url => &P4CGI::LU_URL(),
						"USER=$user",
						"$user"),
				  &P4CGI::ahref("-url",&P4CGI::CHV_URL(),
						"CH=$change",
						"$change"),
				  "<tt>$desc</tt>",
				  $labels,
				  exists $opened{$rev}?$opened{$rev}:"") ;
	}
	else {
	    print
		"",
		&P4CGI::table_row(-valign => "top",
				  &P4CGI::ahref("-url",&P4CGI::FV_URL(),
						"FSPC=$file",
						"REV=$rev",
						"$rev"),
				  &P4CGI::ahref("-url",&P4CGI::FDV_URL(),
						"FSPC=$file",
						"REV=$rev",
						"ACT=$act",
						"$act"),				  
				  "$date",
				  &P4CGI::ahref(-url => &P4CGI::LU_URL(),
						"USER=$user",
						"$user"),
				  &P4CGI::ahref("-url",&P4CGI::CHV_URL(),
						"CH=$change",
						"$change"),
				  "<tt>$desc</tt>",
				  $labels,
				  exists $opened{$rev}?$opened{$rev}:"") ;
	}
    }
}

print
    "",
    &P4CGI::end_table("") ;

if(@revs > 2) {
    print
	"<hr>",
	&P4CGI::cgi()->startform("-action",&P4CGI::FDV_URL(),
				 "-method","GET"),
	&P4CGI::cgi()->hidden("-name","FSPC",
			      "-value","$file"),    
	&P4CGI::cgi()->hidden("-name","ACT",
			      "-value","edit"),    
	"\nShow diff between revision: ",    
	&P4CGI::cgi()->popup_menu(-name   =>"REV",
				  "-values" =>\@revs);
    shift @revs ;
    print
	" and ",
	&P4CGI::cgi()->popup_menu(-name   =>"REV2",
				  "-values" =>\@revs),
	"   ",
	&P4CGI::cgi()->submit(-name  =>"Go",
			      -value =>"Go"),
	&P4CGI::cgi()->endform() ;
}

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

#
# That's it folks
#