#!/usr/bin/perl -w
#
# Written by NMa 2007.11.14
# Name: update_client_server_list
# Arguments needed : Path to file containing new serves.
#
# Notes : Do rember bpsetconfig REPLACES the orginal contens with the changes from file.
#
#
# Date       Init   Review  Test  Version Comments
#----------------------------------------------------------------------
# 2007.11.14  NMA   ENAJ    ENAJ   6.5     New Script
# 2008.03.10  NMA   N/A     N/A    6.5     Client list is no longer retrived from Netbackup but relies on input file.

# Check the number of arguments.

## Check number of arguments
##
$num_args = $#ARGV+1;
if ( $num_args != 2 ) {
 print "\n";
 print "Only two argument are expected - That's the input file of the new server list and the file containing clients to be updated with new server list. \n";
 print "Number of Arguments: ", $num_args, "\n";
 exit 1;
}

## Read in the new serverlst file specifyed on command line and strip off unwanted text (LF-CR). Array @input_serverliste contain the new server list.
##
open INPUT_FILE, $ARGV[0] or die "Can't open server file inputfile $ARGV[0], STOPPED ";
 chop(@input_mylist = <INPUT_FILE>);
close INPUT_FILE;

# Get rid of SERVER =  text so we have a clean list of Netbackup servers.
@input_serverlist = grep {/^SERVER =/} @input_mylist;
foreach $i (@input_serverlist) {
 $i =~ s/SERVER = //;
}


## Read list of "SERVER =" in bp.conf. We don't want to update those for security reasons.
##
open BPCONF, "/usr/openv/netbackup/bp.conf" or die "Can't open bp.conf : $!\n";
 chop(@mylist = <BPCONF>);
close BPCONF;

@serverlist = grep {/^SERVER =/} @mylist;
foreach $j (@serverlist) {
 $j =~ s/SERVER = //;
}

# Let's check the first entry in bp.conf matches the first in the input file.
# WE MUST HAVE A MATCH, else we may update all clinets with a invalid master server entry
# and a manual job of updating all client (Doh!).

if ( $input_serverlist[0] ne $serverlist[0] )  {
   print "\n";
   print "The first SERVER entry in bp.conf does not match the one in the input list!!!!!\n";
   print "The bp.conf entry say $serverlist[0] but the input lists $input_serverlist[0] \n\n";
   exit 1;
}

# Read client input file, we read it from argument 2 on the command line. Remove any new lines and populate data into hash allclients
open CLIENT_LIST, $ARGV[1] or die "Can't open client inputfile $ARGV[1], STOPPED \n";
while (<CLIENT_LIST>) {
 chomp;
 # We do not assign a value to each hash value.
 $allclients{$_} = 0;
 }
close CLIENT_LIST;

# Now remove NBU server from allclients
foreach $i (@serverlist) {
 delete $allclients{$i};
 }

# Update clients now. This is the main program. We call bpsetconfig and check the exit status

print "\n";
@list_of_keys = keys(%allclients);
foreach $i (@list_of_keys) {
  print "Starting update of client $i: ";
  if ( system("/usr/openv/netbackup/bin/admincmd/bpsetconfig -h $i $ARGV[0]") != 0) { print "Failed updating $i \n";
  } else {
  print "Succefully updated\n";
  }
}


