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

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

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

# Get branch path, put together list of branch-name/path
my $listbranch ;
my $path ;
my @listbranches ;
foreach $branch (@branches) {
    my $listed ;
    local *P4 ;
    &P4CGI::p4call(*P4, "branch -o $branch" );
    while(<P4>) {
	chomp ;
	if( $listed == 1 ) { $listed=0 ; next ; }
	next unless /^\s+\/\// ;
	/^\s*\/\/\S+\s(\/\/.*)/ ;
	$path = $1 ; $listed = 1 ;
	$listbranch = $branch . " " . $path ;
	push( @listbranches, $listbranch ) ;
    }
    close P4 ;
}

# Print title and legend
print
    "",
    &P4CGI::start_page("Changes by Branch",
	&P4CGI::ul_list("<b>Branch:</b> to see changes for branch"),
	$homepage,$backtohome) ;

# Print the page
if( scalar(@branches) == 0 ) {
print
    "",
    "<font color=red>No branches for this depot.</font>",
    "<br>",
    &P4CGI::end_page() ;
    exit 1 ;
}
else {
  my ($tbranch, $tpath) ;
  print
    "",
    "<b>",
    &P4CGI::start_table("width=100%"),
    &P4CGI::table_row(-type  => "th",
		      -align => "left",
		      "Branch","Path"),
    "</b>";
  foreach (@listbranches) {
    ($tbranch,$tpath) = split /\s/ ;
    print
      "",
      &P4CGI::table_row(-valign => "top",
          &P4CGI::ahref(-url => &P4CGI::CHB_URL(),
			"FSPC=$tpath", "BRANCH=YES", "BR=$tbranch",
			"$tbranch"),
	  "$tpath",
	  "<br>"),
  }

}

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