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

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

# Get client name
my $client = P4CGI::cgi()->param("CLIENT") ;
&P4CGI::bail("No client specified") unless defined $client ;

# Get client info
local *P4 ;
&P4CGI::p4call(*P4, "client -o $client" );
my $clientname ;
my $update ;
my $access ;
my $owner;
my $desc ;
my $root ;
my $options ;
my $view ;
while(<P4>) {
    chomp ;
    next if /^#/ ;
    next if /^\s*$/ ;
    /^Client:\s(\S+)/ and do { $clientname=$1 ; } ;
    /^Update:\s(.*)$/ and do { $update=$1 ; } ;
    /^Access:\s(.*)$/ and do { $access=$1 ; } ;
    /^Owner:\s(\S+)/ and do
	{ $owner=&P4CGI::ahref(-url => &P4CGI::LU_URL,"USER=$1",$1) ; } ;
    /^Root:\s(.*)$/ and do { $root=$1 ; } ;
    /^Options:\s(.*)$/ and do { $options=$1 ; } ;
}
close P4 ;
&P4CGI::p4call(*P4, "client -o $client" );
while(<P4>) {
    chomp ;
    next if /^#/ ;
    next if /^\s*$/ ;
    next if /^Client:/ ;
    next if /^Update:/ ;
    next if /^Access:/ ;
    next if /^Owner:/ ;
    /^Description:/ and do { next ; } ;
    last if /^Root:/ ;
    /^\s*/ and do {
	$desc = "" unless defined $desc ;
	$desc .= $_ ; 
    } ;
}
close P4 ;
&P4CGI::p4call(*P4, "client -o $client" );
while(<P4>) {
    chomp ;
    last if /^View:/ ;
}
my $viewpath ;
while(<P4>) {
    /^\s+(.*)/ ;
    $view = "" unless defined $view ;
    $view .= "<pre>$1</pre>" ; 
}

close P4 ;

# Print title and legend
print
    "",
    &P4CGI::start_page("P4 Client Information",
    &P4CGI::ul_list(
	"<b>Owner:</b> to see P4 user info"),
    $homepage,$backtohome) ;
    
# Print client info
print 
    &P4CGI::start_table(""),
    &P4CGI::table_row({-align => "right",
		       -type  => "th",
		       -text  => "Client:"},
		      $clientname),
    &P4CGI::table_row({-align => "right",
		       -type  => "th",
		       -text  => "Update:"},
		      $update),
    &P4CGI::table_row({-align => "right",
		       -type  => "th",
		       -text  => "Access:"},
		      $access),
    &P4CGI::table_row({-align => "right",
		       -type  => "th",
		       -text  => "Owner:"},
		      $owner),
    &P4CGI::table_row({-align => "right",
		       -valign=> "top",
		       -type  => "th",
		       -text  => "Description:"},
		      $desc),
    &P4CGI::table_row({-align => "right",
		       -type  => "th",
		       -text  => "Root:"},
		      $root),
    &P4CGI::table_row({-align => "right",
		       -type  => "th",
		       -text  => "Options:"},
		      $options),
    &P4CGI::table_row({-align => "right",
		       -valign=> "top",
		       -type  => "th",
		       -text  => "View:"},
		      $view);

# End the table, end the page
print 
    &P4CGI::end_table(),
    &P4CGI::end_page() ;