#!/usr/bin/perl -w
# -*- perl -*-
use P4CGI ;
use strict ;
#
#################################################################
#  CONFIGURATION INFORMATION 
#  All config info should be in P4CGI.pm
#
#################################################################
#
#  List p4 clients
#
#################################################################

sub weeksago($$$ ) {
# Take Year, month and day of month as parameter and return the number
# of week that has passed since that date
    my ($y,$m,$d) = @_ ;
    $y -= 1900 ;
    $m-- ;
    my $_now = time() ;
    my $_then = $_now ;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
	localtime($_then);
    if(($y > $year) or
       (($y == $year) and ($m > $mon)) or
       (($y == $year) and ($m == $mon) and ($d > $mday))) {
	return 0 ;
    }
				# The algorithm is not very robust, take current date and
				# remove one day at the time until the date match the requested
				# date. Can fail miserably for a number of combinations of
				# illegal input....
    while(1) {
	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
	    localtime($_then);
	if(($y == $year) and ($m == $mon) and ($d == $mday)) {
	    return int(( $_now - $_then)/(3600*24*7)) ;
	}
	$_then -= 3600*24 ;
    } ;	
}

my $user = P4CGI::cgi()->param("USER") ;
my $mode = P4CGI::cgi()->param("MODE") ;
$mode = "Brief" unless (defined $mode) and ($mode eq "Complete") ;
if (defined $user) {
    $mode = "Complete" ;
    $user = &P4CGI::htmlEncode($user) ;
} ;

my $WARNING_LEVEL = &P4CGI::UNUSEDCLIWL() ;
$WARNING_LEVEL = 10 unless $WARNING_LEVEL =~ /^\d+$/ ;

# Get clients
my %clientInfo  = &P4CGI::p4clients();
my @clients = keys %clientInfo ;

my $clients = @clients ;
my $warnings = 0 ;

my $title = "P4 clients" ;
if(defined $user) {
    $title .= "<br>for user $user" ;
}

$| = 1 ;

my @legend ;

my $lastaccess = undef ;
my $owner = undef ;
if($mode eq "Brief") {
    push @legend,&P4CGI::buttonCell("",
				    "Show owner and access info in table (slow)",
				    "MODE=Complete",
				    "Show owner and access info") ;
}
else {
    $lastaccess = "Last access" ;
    $owner = "Owner" ;
} ;
if(defined $user) {
    push @legend,&P4CGI::buttonCell("clientList.cgi",
				    "Show all clients on depot",
				    "Show all clients") ;    
}



print "",
    &P4CGI::start_page("List clients",
		       @legend) ;
unless(defined $user) {
    print &P4CGI::start_framedTable("$clients clients") ;
}
else {
    print &P4CGI::start_framedTable("$title") ;
}
print "",
    &P4CGI::start_table(" cellpadding=1"),
    &P4CGI::table_header("Client","",$owner,
			 "Updated",$lastaccess);


# Get users
my %users ;
&P4CGI::p4user2name(\%users);
&P4CGI::ERRLOG("Mode: $mode" );

if($mode ne "Brief") {
    my $client ;
    foreach $client (sort { uc($a) cmp uc($b) } @clients)
    {
	my %values ;
	my @fields = &P4CGI::p4readform("client -o '$client'",\%values) ;
	my $warning = "" ;
	if(exists $values{"Date"}) {
	    $values{"Update"} = $values{"Date"} ;
	    $values{"Access"} = "---" ;
	    delete $values{"Date"} ;
	}
	else {
	    if($values{"Access"} =~ /(\d\d\d\d)\/(\d\d)\/(\d\d)/) {
		my $weeksOld = weeksago($1,$2,$3) ;
		if($weeksOld >= $WARNING_LEVEL) {
		    if($warning ne "") { $warning .= "<br>\n" ; } ;
		    $warning .= "Not used for $weeksOld weeks!" ;		    
		}
	    }
	}
	if(exists $values{"Owner"}) {
	    $owner = $values{"Owner"} ;
	    $values{"OwnerName"} = $owner ;
	    if(exists $users{$owner}) {
		$values{"Owner"} = &P4CGI::ahref(-url => "userView.cgi" ,
						 "USER=$owner",
						 "HELP=View user info",
						 $owner),
	    }
	    else {		
		if($warning ne "") { $warning .= "<br>\n" ; } ;
		$warning .= "Owner does not exist!" ;
	    }	    
	} ;
	unless((defined $user) and ( uc($user) ne uc($owner))) {
	    $values{"Warnings"} = $warning ;    
	    $clientInfo{$client} = { %{$clientInfo{$client}},%values} ;
	    if($warning ne "") { $warnings++ ; } ;
	}
    } ;
}

my $client ;
foreach $client (sort { uc($a) cmp uc($b) } @clients)
{        
    my %info = %{$clientInfo{$client}} ;

    $info{"Warnings"} = "" unless defined $info{"Warnings"} ;

    if((!defined $user) or (uc($user) eq uc($info{"OwnerName"}))) {	
	print join("\n",(&P4CGI::table_row(-valign=>"top",
					   undef,
					   &P4CGI::ahref(-url => "clientView.cgi",
							 "CLIENT=$client",
							 "HELP=View client info",
							 $client),
					   $info{"Owner"},
					   $info{"Update"},
					   $info{"Access"},
					   "<font color=red><b>$info{Warnings}</b></font>").
			 &P4CGI::table_row({-text=>"",
					    -style=>"width: 30pt"},
					   undef,
					   undef,
					   undef,
					   undef,
					   {
					       -class=>"Description",
					       -text => &P4CGI::formatDescription($info{"Description"}),
					   }))) ;
    }
}
print &P4CGI::end_table(),
    &P4CGI::end_framedTable() ;
    
if($warnings > 0) {
    my $s = "" ;
    $s = "s" if $warnings != 1 ;
    $warnings = "<font color=red>($warnings warning$s)</font>" ;
}
else {
    $warnings = "" ;    
}

print
    " $warnings<br>",
    &P4CGI::end_page() ;

#
# That's all folks
#