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


# Get list of all brances
my @branches ;
&P4CGI::p4call(\@branches, "branches" );
foreach (@branches) {
    $_ =~ s/^Branch (\S+).*$/$1/ ;
}

# Print header
print "",
    &P4CGI::start_page("List of branches",
		       &P4CGI::ul_list("<b>owner</b> -- view user info",
				       "<b>view</b> -- View changes for view")) ;

print "",
    scalar @branches," branches" ;

print "",
    &P4CGI::start_table("") ;
#    &P4CGI::table_row("-type","th",
#		      "-align","left",
#		      "Branch","Date","Owner","Description","View");

foreach (@branches) {

# Get branch info
    local *P4 ;
    my $branch=$_ ;
    my $date ;
    my $owner ;
    my $desc ;
    my @view ;
    &P4CGI::p4call(*P4, "branch -o $_" );
    while(<P4>) {
	chomp ;
	next if /^#/ ;
	next if /^\s*$/ ;
	/^Date:\s(.*)$/ and do { $date=$1 ; } ;
	/^Owner:\s(.*)$/ and do { $owner=$1 ; } ;
	last if /^Description:/ ;
    }
    my $descWhiteSpace ;
    while(<P4>) {
	chomp ;
	last if /^View:/ ;
	next if /^\s*$/ ;
	unless(defined $descWhiteSpace) {
	    /^(\s*)/ ;
	    $descWhiteSpace = $1 ;	    
	} ;
	s/^$descWhiteSpace// ;
	if(defined $desc) {
	    $desc .= "<br>$_" ;
	}
	else {
	    $desc .= $_ ;
	}
    } ;
    
    while(<P4>) {
	next if /^\s*$/ ;
	push @view,$_ ;
    }

# Fix up data
    $owner = &P4CGI::ahref(-url => &P4CGI::LU_URL(),
			   "USER=$owner",
			   $owner) ;
    my $view ;
    foreach (@view) {
	s/^\s*(\S+\s+\S+)\s*/$1/ ;
	my ($from,$to) = split /\s+/ ;
	$from =~ s/^\/\/// ;
	$to =~ s/^\/\/// ;
	my @from = split /\//,$from ;
	my @to = split /\//,$to ;
	my $common = "//" ;
	while($from[0] eq $to[0]) {
	    $common .= shift @from ;
	    $common .= "/" ;
	    shift @to ;
	}
	$from = $common . "<FONT COLOR=red>" . join("/",@from) . "</FONT>" ;
	$from = &P4CGI::ahref(-url => &P4CGI::CHB_URL(),
			      "FSPC=$common" . join("/",@from),
			      $from) ;
	$to = $common . "<FONT COLOR=red>" . join("/",@to) . "</FONT>" ;
	$to = &P4CGI::ahref(-url => &P4CGI::CHB_URL(),
			    "FSPC=$common" . join("/",@to),
			    $to) ;
	
	if (defined $view) {
	    $view .= "<br>" ;
	} 
	else {
	    $view .= "" ;
	} ;
	$view .= "$from&nbsp;$to" ;
    } ;

    print 
	"",
	&P4CGI::table_row(undef,"<hr>"),
	&P4CGI::table_row({-type=>"th",
			   -align=>"right",
			   -valign=>"center",
			   -text=>"Branch:"},
			  "<font size=+2><b>$branch<b></font>"),
	&P4CGI::table_row({-type=>"th",
			   -align=>"right",
			   -text=>"Date:"},
			  "$date"),
	&P4CGI::table_row({-type=>"th",
			   -align=>"right",
			   -text=>"Owner:"},
			  "$owner"),
	&P4CGI::table_row({-type=>"th",
			   -align=>"right",
			   -valign=>"top",
			   -text=>"Description:"},
			  {-bgcolor => "white",
			   -text    => "<tt>$desc</tt>"}),
	&P4CGI::table_row({-type=>"th",
			   -align=>"right",
			   -valign=>"top",
			   -text=>"View:"},
			  "<tt>$view</tt>") ;
    close P4 ;
}

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

#
# That's it folks
#