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

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

# Get client 
my $clientspec = P4CGI::cgi()->param("CLIENT") ;

# Get clients, for form
my @allclients ;
my @p4clients ;
&P4CGI::p4call(\@allclients, "clients" );
while ( @allclients > 0 )
{
    my $clienttemp = shift @allclients ;
    $clienttemp =~ /^Client (\S+) / ;
    my $clientname = $1 ;
    push( @p4clients, "$clientname" ) ;
}

# Print title and legend
print
    "",
    &P4CGI::start_page("Opened Files",
	&P4CGI::ul_list("<b>Change:</b> to see the details of the (pending) change",
			"<b>Rev:</b> to see the file text",
			"<b>User:</b> to see the P4 user info (including opened files)",
			"<b>Client:</b> to see the P4 client info",
			"<b>Filename:</b> to see the complete file history"),
	$homepage,$backtohome) ;

# Print form
print
    "",
    &P4CGI::cgi()->startform("-action",&P4CGI::LOF_URL(),
			     "-method","GET"),
    "<b><i>View opened files only for client: </i></b>",	
    &P4CGI::cgi()->popup_menu(-name  => "CLIENT",
			     "-values" =>\@p4clients),
    &P4CGI::cgi()->submit(-name  => "Go",
			  -value => "Go"),
    &P4CGI::cgi()->endform(),
    "<hr>" ;


# Print output
my $printed=0 ;
my $openfiles ;
my $user ;
&P4CGI::p4call( *P4, "opened -a" ) ;
while( <P4> ) {
    my ( $file,$rev,$action,$change,$client ) ;
    my ( $chgno,$revno,$uname,$cname,$fname,) ;
    chomp ;
    /(^\/\/\S+)\s+.*/ and do { $file = $1 ; $file =~ s/(.*)\#.*/\1/ ; } ;
    /.*\#(\d+)\s+.*/ and do { $rev = $1 } ;
    /.*\s+-\s+(\S+)\s+.*/ and do { $action = $1 } ;
    /.*\s+(default change)\s+.*/ and do { $change = "default" } ;
    /.*\s+change\s+([0-9]+)\s+.*/ and do { $change = $1 } ;
    /.*\s+(\S+)@.*/ and do { $user = $1 } ;
    /.*@(\S+)$/ and do { $client = $1 } ;
    $fname = &P4CGI::ahref( -url => &P4CGI::FLV_URL(),
				   &P4CGI::fixspaces("FSPC=$file"),
				   $file ) ;
    if( $action ne "add" ) {
	$revno = &P4CGI::ahref( -url => &P4CGI::FV_URL(),
				&P4CGI::fixspaces("FSPC=$file"),
				"REV=$rev",
				$rev ) ;
    }
    else { $revno = $rev ; }
    if( $change ne "default" ) {
	$chgno = &P4CGI::ahref( -url => &P4CGI::CHV_URL(),
				"CH=$change",
				$change ) ;
    }
    else { $chgno = "(default)" ; }
    $uname = &P4CGI::ahref( -url => &P4CGI::LU_URL(),
			    "USER=$user",
			    $user ) ;
    $cname = &P4CGI::ahref( -url => &P4CGI::CLV_URL(),
			    "CLIENT=$client",
			    $client ) ;
    if ( $clientspec ) {
      if( $clientspec eq $client ) {
	if( $printed == 0 ) {
	  print 
	    &P4CGI::start_table(""),
	    &P4CGI::table_row("-type","th",
		"-align","left",
		"Change","Action","Rev","User","Client","Filename") ;
	  $printed = 1 ;
	}
	print
	    &P4CGI::table_row(-valign => "top",
		"$chgno","$action","$revno","$uname","$cname","$fname") ;
      }
    }
    else {
      if( $printed == 0 ) {
	print 
	    &P4CGI::start_table(""),
	    &P4CGI::table_row("-type","th",
		"-align","left",
		"Change","Action","Rev","User","Client","Filename") ;
	$printed = 1 ;
      }
      print
	&P4CGI::table_row(-valign => "top",
	"$chgno","$action","$revno","$uname","$cname","$fname") ;
    }
}
if( $printed == 0 ) {
    print
	&P4CGI::table_row(-valign => "top",
	"<font color=red><b>None.</b></font>") ;
}

# End the table
print
    "",
    &P4CGI::end_table() ;

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