#!/usr/bin/perl -w
# -*- perl -*-
use P4CGI ;
use strict ;
#
#################################################################
#  CONFIGURATION INFORMATION 
#  All config info should be in P4CGI.pm
#
#################################################################
#
#  View files affected by a set of changes
#
#################################################################

my $err2null = &P4CGI::REDIRECT_ERROR_TO_NULL_DEVICE() ;

####
# Parameters
# FSPC = file spec
#
# WEEKS = restrict to changes newer than specified No. of weeks
# DAYS  = restrict to changes newer than specified No. of days
# HOURS = restrict to changes newer than specified No. of hours
#
# WEEKS, DAYS and HOURS are added to get total time
#

my $FSPC = P4CGI::cgi()->param("FSPC") ;
$FSPC = "//..." unless defined $FSPC ;
$FSPC = &P4CGI::htmlEncode($FSPC) ;
my @FSPC = split(/\s*\+?\s*(?=\/\/)/,$FSPC) ;

my $WEEKS = P4CGI::cgi()->param("WEEKS") ;
if(defined $WEEKS) {
    &P4CGI::bail("Parameter WEEKS non-numeric") unless $WEEKS =~ /^\d+$/ ;
} 
else {
    $WEEKS = 0 ;
}

my $DAYS = P4CGI::cgi()->param("DAYS") ;
if(defined $DAYS) {
    &P4CGI::bail("Parameter DAYS non-numeric") unless $DAYS =~ /^\d+$/ ;
} 
else {
    $DAYS=0 ;
}

my $HOURS = P4CGI::cgi()->param("HOURS") ;
if(defined $HOURS) {
    &P4CGI::bail("Parameter HOURS non-numeric") unless $HOURS =~ /^\d+$/ ;
} 
else {
    $HOURS = 0 ;
}


my $seconds = 3600 * ( $HOURS + (24 * ($DAYS + (7 * $WEEKS)))) ;

#
# get time strings to compare to
#
my $time = time() ;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
my $currentTimeString = sprintf("\@%d/%02.2d/%02.2d:%02.2d:%02.2d:%02.2d",
				1900+$year,$mon+1,$mday,$hour,$min,$sec) ;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time - $seconds);
my $afterTimeString = sprintf("\@%d/%02.2d/%02.2d:%02.2d:%02.2d:%02.2d",
			      1900+$year,$mon+1,$mday,$hour,$min,$sec) ;
my $niceAfterTimeString = sprintf("%d/%02.2d/%02.2d %02.2d:%02.2d",
				  1900+$year,$mon+1,$mday,$hour,$min) ;

&P4CGI::ERRLOG("currentTimeString: $currentTimeString") ;
&P4CGI::ERRLOG("afterTimeString: $afterTimeString") ;

my $title = 
    "Files in <tt>".
    join("<br></tt>or<tt><br>\n",@FSPC).
    "</tt><br> changed after <tt>$niceAfterTimeString</tt>" ;

#
# Start page
#
print 
    &P4CGI::start_page("$title","") ;

#
# Get list of files changed
#
my %toRev ;
my %mode ;
foreach $FSPC (@FSPC) {
    my @files ;
    &P4CGI::p4call(\@files,"files \"${FSPC}${afterTimeString},${currentTimeString}\" $err2null") ;
    map { s/\#(\d+) - (\S+).*$// ; 
	  $toRev{$_}=$1 ; 
	  $mode{$_} =$2 ;   } @files ;
}
my @affectedFiles = sort keys %toRev ;

#
# Get revision at start of interval
#
my %fromRev ;
my @filesToCheck = @affectedFiles ;
while(@filesToCheck > 0) {
    my $files="" ;
    while(length($files) < 1000 and @filesToCheck > 0) {
	$files .= " \"" . shift(@filesToCheck) . $afterTimeString . "\"" ;
    }
    my @res ;
    &P4CGI::p4call(\@res,"files $files $err2null")  ;
    map { s/\#(\d+) - .*// ; $fromRev{$_}=$1 } @res ;
}

if(@affectedFiles == 0) {
    print "<font size=+1 color=red>No files found</font><br>" ;
}
else {
    my $filesFound = scalar @affectedFiles . " files" ;
    
    
    print
	&P4CGI::start_framedTable($filesFound),
	&P4CGI::start_table(""),
	&P4CGI::table_header("From",
			     "",
			     "To",
			     "File",
			     "Change(s)") ;
    
    my $f ;
    foreach $f (@affectedFiles) {
	
	my @tmp ;
	my $changes ;
	&P4CGI::p4call(\@tmp,"changes \"$f${afterTimeString},${currentTimeString}\"") ;
	map {    
	    /^Change (\d+).*$/ ;
	    my $c = &P4CGI::ahref(-url => "changeView.cgi",
				  "CH=$1",
				  "HELP=View change",
				  $1) ;
	    if(defined $changes) {
		$changes .= ", $c" ;
	    }
	    else {
		$changes = "$c" ;
	    } ;
	} @tmp ;
	
	
	my $file = &P4CGI::ahref(-url => "fileLogView.cgi",
				 "FSPC=$f",
				 "HELP=File log",
				 $f) ;
	my $fromRev ;
	my $diff ;
	if(exists $fromRev{$f}) {
	    $fromRev = &P4CGI::ahref(-url => "fileViewer.cgi",
				     "FSPC=$f",
				     "REV=$fromRev{$f}",
				     "HELP=View file",
				     $fromRev{$f}) ;	
	    $diff = &P4CGI::ahref(-url => "fileDiffView.cgi",
				  "FSPC=$f",
				  "REV=$fromRev{$f}",
				  "REV2=$toRev{$f}",
				  "ACT=$mode{$f}",
				  "HELP=View diff",
				  "<font size=1>(diff)</font>") ;	
	}
	else {
	    $fromRev = "" ;
	    $diff = "<font size=-1 color=red>New</font>" ;
	} ;
	my $toRev ;
	if($mode{$f} eq "delete") {
	    $toRev = $toRev{$f} ;
	    $diff = "<font size=-1 color=red>Deleted</font>" ;
	}
	else {
	    $toRev = &P4CGI::ahref(-url => "fileViewer.cgi",
				   "FSPC=$f",
				   "REV=$toRev{$f}",
				   "HELP=View file",
				   $toRev{$f}) ;
	} ;
	print &P4CGI::table_row(-align => "center",
				$fromRev,
				$diff,
				$toRev,
				{-align=>"left",
				 -text => $file},
				{-align=>"left",
				 -text => $changes}) ;
    } ;
} ;
print 
    &P4CGI::end_table(),
    &P4CGI::end_framedTable(),
    "<br>";
    		
print &P4CGI::end_page() ;

#
# That's all folks
#