#!/usr/bin/perl 
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/utilities/clsetdistpolicy.sh 1.4 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2003,2011 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)19	1.4 src/43haes/usr/sbin/cluster/utilities/clsetdistpolicy.sh, hacmp.utils, 61haes_r714 11/28/11 15:13:51
#
# This utility adds/modifies the global distribution policy
#


$path=$ENV{'PATH'};
$path="/bin/:$path";
$ENV{'PATH'}= $path;

#
# Main starts here..
#

list () if ($ARGV[0] eq "list");
change ($ARGV[1]) if ($ARGV[0] eq "change");

print ("Invalid Command line option\n"); # User will not see this.

sub list
{
    my @cluster = qx (odmget HACMPcluster);
    if ($#cluster == -1)
    {
	    print STDERR `dspmsg scripts.cat 9336 "PowerHA SystemMirror cluster not found\n"`;
    	exit -1;
    }

    my $value;

    foreach (@cluster)
    {
	    if (/\s+rg_distribution_policy\s+=\s+\"(\w+)\"/)
    	{
         $value = $1;
    	}
    }
    print "#DISTRIBUTION_POLICY\n";
    print "$value\n";
    exit 0;
}


sub change
{
    my ($newValue) = @_;

    my @cluster = qx (odmget HACMPcluster);
    if ($#cluster == -1)
    {
	    print STDERR `dspmsg scripts.cat 9336 "PowerHA SystemMirror cluster not found\n"`;
    	exit -1;
    }

    $newValue =~ tr/A-Z/a-z/;

    if ($newValue ne "node" && $newValue ne "network")
    {
	    print STDERR `dspmsg scripts.cat 9688 "ERROR: Invalid Distribution Policy. \nThe distribution policy should be either 'node' or 'network'.\nWARNING: The 'network' distribution policy is deprecated and will be removed from the product in future.\n"`;
    	exit -1;
    }

    if ($newValue eq 'network')
    {
        print `dspmsg scripts.cat 9689 "\nWARNING: The 'network' Resource Group Distribution Policy is \ndeprecated and will be removed from the product in future.\n"`;
    }

    my $file = "/tmp/ha_distribution_policy.$$";
    my $ret = open (STANZA, ">$file");
    foreach (@cluster)
    {
	    if (/\s+rg_distribution_policy\s+=\s+\"(\w+)\"/)
    	{
            print `dspmsg scripts.cat 9690 "Changing the global Resource Group Distribution Policy..\n"`;
            print `dspmsg scripts.cat 9691 "Old Policy: $1\n" $1`;
            print `dspmsg scripts.cat 9692 "New Policy: $newValue\n" $newValue\n`;
            print STANZA "\trg_distribution_policy = \"$newValue\" \n";
            next;
    	}

        # User must synchronize the cluster
	    if (/\s+handle\s+=\s+(\d+)/)
    	{
            print STANZA "\thandle = 0\n";
            next;
    	}
        print STANZA $_;
    }

    qx (odmdelete -o HACMPcluster);
    qx (odmadd $file);
    qx (/bin/rm -f $file);
    exit 0;
}


