#!/usr/bin/perl 
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2019,2020,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/utilities/clsettlingtime.sh 1.5 
#  
# 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 
#
# @(#)  7d4c34b 43haes/usr/sbin/cluster/utilities/clsettlingtime.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM 
#
# This utility will add a settling time to HACMPcluster ODM.
#

use lib '/usr/es/lib/perl/';  #Adds a directory to perl's library path
use libcl_untaint;


#
# Main starts here..
#

#A user other than root user is allowed to perform add/update/delete operation only when the user belongs to the role "ha_admin"

my $user = $ENV{ USER };
my $role = qx("/usr/es/sbin/cluster/utilities/cl_get_role");
chop($role);
$role = untaint($role);
if ($<){
    if ($role eq '') {
        print STDERR `dspmsg -s 129 cspoc.cat 79 "ERROR: %s\n" "Failed to get role information for \"$user\" user or PowerHA role is not assigned to the \"$user\" user."`;
    exit -1;
    }
    if ($ARGV[0] eq "change" && $role ne "ha_admin" ){
          print STDERR `dspmsg utilities.cat 1052 "A user with the '%s' role cannot perform this operation.\n" $role;`;
          exit -1;
    }
}

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 = 0;

    foreach (@cluster)
    {
	    if (/\s+settling_time\s+=\s+(\d+)/)
    	{
         $value = $1;
    	}
    }
    print "#SETTLING_TIME\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;
    }

    if ($newValue < 0)
    {
	    print STDERR `dspmsg scripts.cat 9350 "ERROR: Settling time must be a positive number.\n"`;
    	exit -1;
    }

    $newValue = int ($newValue);

    my $file = "/tmp/ha_settling_time.$$";
    my $ret = open (STANZA, ">$file");
    foreach (@cluster)
    {
	    if (/\s+settling_time\s+=\s+(\d+)/)
    	{
            print `dspmsg scripts.cat 9337 "Changing Settling time:\n"`;
            print `dspmsg scripts.cat 9338 "Old value: $1 Seconds\n" $1;`;
            $newValue = untaint($newValue);

            print `dspmsg scripts.cat 9339 "New value: $newValue Seconds\n" $newValue;`;
            print STANZA "\tsettling_time = $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;
}