#!/usr/local/bin/perl


# Script to add user to branch

# Need branch name
# User name
# Target directory for local clients

use Getopt::Long;

$Getopt::Long::autoabbrev=0;  #make them specify exact option

unless($result=GetOptions("h",
			  "tp=s",
			  "user=s",
			  "userdirname=s",
			  "force",
			  "rev=s",
			  "bname=s")){
    die "Illegal Input Options, -h for help\n";
  }

($thisprog = $0) =~ s,.*/,,;

$HELP="Usage: $thisprog -bname <branch_name> -tp <user_directory> -user <user name>
        <user_directory> must be an absolute path
Builds a new user workspace of <branch_name> under <user_directory>";

unless($opt_user && $opt_tp && $opt_bname && $opt_tp =~ /^\//) {
  print STDERR "$HELP\n";
  exit 1;
}

unless (-d $opt_tp && -w $opt_tp) {
  print STDERR "ERROR: $thisprog: directory $opt_tp does not exist or is not writable\n";
  exit 1;
}

if ($opt_tp =~ /\/$/) {
  print STDERR "WARNING: $thisprog: removing trailing `/' from directory $opt_tp; hope that is what you intended\n";
  $opt_tp =~ s/\/$//;
}


# Objective is to clone the branch clients into the tp area and create
# cds.lib, cds.p4clients and also perform all the function of the old bprep

# Let's start with building the cloned clients

# Source must be the branch cds.lib/cds.p4clients file

# Define the location of the file relative to the tree root (site dependent variable)

$blocation="";

# need to find the root of the branch client to begin

$bclient=$opt_bname."_client";
$cmd="p4 clients | awk '{print \$2}'";

open(CMD,"$cmd|") || die "error $cmd\n";

$found_client = 0;
while(<CMD>) {
  chomp;
  if (/^$bclient$/) {
    $found_client = 1;
  }
}

unless ($found_client) {
  print STDERR "ERROR: $thisprog: branch $opt_bname does not exist\n";
  exit 1;
}

$cmd="p4 client -o $bclient | grep '^Root'";

open(CMD,"$cmd|") || die "error $cmd\n";

while(<CMD>) {
  chomp;
  ($ig,$broot)=split;
}

print "Branch root is $broot\n";

# Read the cds.p4clients file

$cfile=$broot."/".$blocation."/cds.p4client";

open(CFILE, "<$cfile") || die "Couldn't read $cfile\n";

print "Reading $cfile\n";

while(<CFILE>) {
  if(/^\#/) {
    next;
  }
  chop;
  (@cdata)=split;
  $path=$cdata[1];
  $client=$cdata[2];
  (@cinfo)=split(':',$cdata[2]);
  $libname=$cinfo[0];
  $vclient{$client}=$libname;
}


# For each of these clients, clone them with a suffix on the name
# and define a new root
# also write out the cds.p4client file while we are at it

$new_root=$opt_tp."/".$opt_bname;

if($opt_userdirname) {
  $user_new_root=$opt_tp."/".$opt_userdirname;
} else {
  $user_new_root=$new_root;
}

print "Got $opt_userdirname\n";

# make the directory
$cmd="mkdir $user_new_root";
system($cmd);


open(CTFILE,">$user_new_root/cds.p4client") || die "error cds.p4client\n";
open(LIBFILE,">$user_new_root/cds.lib") || die "error cds.lib\n";

# make sure we include any INCLUDE from the brach cds.lib file

$src_cds_lib_file=$broot."/".$blocation."/cds.lib";

open(SRCFILE,"<$src_cds_lib_file") || die "Error source cds.lib\n";


while(<SRCFILE>) {
  if(/^INCLUDE/) {
    # pop them straight in
    print LIBFILE $_;
  }
}

close(SRCFILE);

# also write out the branchrc.il file

open(BRFILE,">$user_new_root/.branchrc.il") || die "error branchrc.il\n";

print BRFILE "setShellEnvVar(\"PROJECT_BRANCH=$opt_bname\")\n";
close(BRFILE);

   
$src_drf_file="$broot/$blocation/display.drf";

if ( -f $src_drf_file ) {
  $cmd="cp $src_drf_file $user_new_root";
  system($cmd);
}


print "New root is $user_new_root\n";

foreach $client (keys %vclient) {
$cmd="p4 -c $client client -o";
$swap=0;
$new_client_name=$client.":".$opt_user;
open(CMD, "$cmd|") || die ;

open(CMDOUT, ">/tmp/cbout.$$") || die ;

while(<CMD>) {
  if(/^Options/) {
    # _tapeout clients should be allwrite
    # they should be bulk mode too
      if( $client =~ /_tapeout/) {
	  s/noallwrite/allwrite/g;
      }

    # Modtime is nice for sorting in lbb
    s/nomodtime/modtime/g;
    print CMDOUT $_;
  }
  
  if(/^Client/) {
    print CMDOUT "Client: $new_client_name\n\n";
  }
  
  if(/^Description/) {
    print CMDOUT "Description:\n\tcds library (shared branch)\n\n";
  }
  
  if(/^Root/) {
    # print in the new root
    

    $swap = s/$broot/$user_new_root/g ;
    if($swap eq 1) {
      print CMDOUT "$_\n";
      s/Root://g;
      chomp;
      print CTFILE "$vclient{$client} $_ $new_client_name\n";
      print LIBFILE "DEFINE $vclient{$client} $_\n";
    }
    else {
      print "Failed to map root for client $client\n$_\n";
      print "$broot to $user_new_root\n";
      exit 1;
    
    }
  }

  if(/^View/) {
    print CMDOUT "$_\n";
  }
  if(/^\t\-{0,1}\/\/depot/) {
    s/$client/$new_client_name/g;
    print CMDOUT "$_\n";
  }
}

close(CMDOUT);
print "Building client $new_client_name\n";
if($opt_force) {
  $opt_force="-f";
  }

if ($opt_rev) {
  print "Synchronizing to revision $opt_rev\n";
}

$cmd="p4 -c $new_client_name client -i < /tmp/cbout.$$ > /dev/null; p4 -c $new_client_name sync $opt_force $opt_rev";
system($cmd);
}
close(CTFILE);

close(LIBFILE);