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

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

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

# Print title and legend
print "",
    &P4CGI::start_page("P4 Branches",
	&P4CGI::ul_list("<b>Owner:</b> to see P4 user info",
	"<b>View left-side mapping:</b> to see changes for path",
	"<b>View right-side mapping:</b> to see changes for branch"),
	$homepage,$backtohome) ;

# Print number of branches
print
    "",
    "<b>",scalar @branches,"</b> branches:<br>" ;

# Print branch info
print
    "",
    &P4CGI::start_table() ;

# Get branch info
foreach (@branches) {
    local *P4 ;
    my $branch=$_ ;
    my $update ;
    my $access ;
    my $owner ;
    my $desc ;
    my @view ;
    &P4CGI::p4call(*P4, "branch -o $_" );
    while(<P4>) {
	chomp ;
	next if /^#/ ;
	next if /^\s*$/ ;
	/^Update:\s(.*)$/ and do { $update=$1 ; } ;
	/^Access:\s(.*)$/ and do { $access=$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 . join("/",@from) ;
	$from = &P4CGI::ahref(-url => &P4CGI::CHB_URL(),
			      "FSPC=$common" . join("/",@from),
			      $from) ;
	$to = $common . join("/",@to) ;
	$to = &P4CGI::ahref(-url => &P4CGI::CHB_URL(), "BRANCH=YES",
			    "FSPC=$common" . join("/",@to), $to) ;
	
	if (defined $view) {
	    $view .= "<br>" ;
	} 
	else {
	    $view .= "" ;
	} ;
	$view .= "$from&nbsp;$to" ;
    } ;

    print 
	"",
	&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=>"Update:"},
			  "$update"),
	&P4CGI::table_row({-type=>"th",
			   -align=>"right",
			   -text=>"Access:"},
			  "$access"),
	&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>"),
	&P4CGI::table_row(undef, "<hr>") ;
    close P4 ;
}

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