#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2019,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/cspoc/utilities/cli_mkvg.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2003,2014 
# 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/cspoc/utilities/cli_mkvg.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#
###############################################################################
#
#   COMPONENT_NAME: CSPOC Command Line Interface
#
# Name:
#   cli_mkvg
#
# Description:
#   Use C-SPOC to run the mkvg command with the given parameters and make the
#   volume group definition known on all cluster nodes
#
# Arguments:
#
#   Same as for the mkvg command; assumed validated by caller
#
#   Physical volume names are assumed valid on the node this command is run
#   on; C-SPOC will handle the case where the disk names are different on
#   different nodes
#
#   If '-n' (no autovaryon) is not supplied, it will be added
#
#   Setting the environment variable _DEBUG to 1 -> 9 turns on levels of
#   C-SPOC debugging
#
# Return Values:
#   As set by cl_mkvg
#
# Notes:
#   The '-f' flag is passed on cl_mkvg to surpress unnecessary checking.  As a
#   consequenc, the operation will proceed even if not all nodes are
#   accessable.
#
################################################################################


typeset DEBUG
typeset other_nodes
typeset have_major
typeset no_auto_varyon
typeset cl_mkvg_args

#
#   Variables needed by cl_mkvg
#	_REFNODE - the node on which the hdisk names are chosen, since they
#			may differ across the cluster
#	_CSPOC_MODE - working with shared (as opposed to concurrent) volume
#			groups
#	_CSPOC_CALLED_FROM_SMIT - can skip checks on input
#
export _REFNODE=$(/usr/es/sbin/cluster/utilities/get_local_nodename)
export _CSPOC_MODE="shared"
export _CSPOC_CALLED_FROM_SMIT="true"

#
#   If a _DEBUG value has been set, pass it through
#
if [[ -n $_DEBUG ]] ; then

    #
    #	The debug level is a number, 1 through 9.  Those values are passed
    #	through.  Anything else is turned into '1'
    #
    integer dbg_level=$_DEBUG
    if (( $dbg_level < 10 && $dbg_level >= 1 )) ; then
	DEBUG="-d $_DEBUG"
    else
	DEBUG="-d 1"
    fi
fi

#
#   Construct the node list so that the local node shows up as first in the
#   list, since the physical volumes processed by cl_mkvg are relative to the
#   local node.  This is a comma delimited, quoted list of the nodes on which
#   the change is made.  It is set to all nodes in the cluster.
#
all_nodes=$(odmget -q "object = VERBOSE_LOGGING" HACMPnode | grep 'name =' | cut -f2 -d'"')
for node in $all_nodes ; do
    if [[ $node != $_REFNODE ]] ; then
	other_nodes=${other_nodes:+"${other_nodes},"}$node
    fi
done
export _NODE_LIST="$_REFNODE,$other_nodes"

# Check if RBAC is enabled
typeset is_rbac_enabled=""
is_rbac_enabled=$(clodmget -nq "group=LDAPClient and name=RBACConfig" -f value HACMPLDAP 2>/dev/null)

# For a non root user, append the user name to the output file.
[[ -z "$LOGIN" ]] && user_name=$(id -nu 2>/dev/null) || user_name="$LOGIN"
if [[ $user_name != "root" && $is_rbac_enabled == "YES" ]];then
    CL_DATFILE="/var/hacmp/tmp/cllspvids.out"_$user_name
else
    CL_DATFILE="/var/hacmp/tmp/cllspvids.out"
fi

#
#   Turn the hdisk names passed on the command into PVIDs, since cl_mkvg
#   expects that to have been done by SMIT.
#
while (( $# != 0 )) ; do
    #
    #	This requires parsing the arguements passed to this command, since
    #	there's no other straight forward way to pick off the hdisk names at
    #	the end of the list
    #
    case $1 in
	
	#   Arguments without operands just get passed through
	-B | -C | -G | -f | -i | -c | -x )
	    cl_mkvg_args=${cl_mkvg_args:+"${cl_mkvg_args} "}$1
	    ;;

	#   If a major number was passed, note that fact, so that it does not
	#   have to be generated later
	-V )	
	    have_major=true
	    cl_mkvg_args=${cl_mkvg_args:+"${cl_mkvg_args} "}$1
	    shift
	    cl_mkvg_args=${cl_mkvg_args:+"${cl_mkvg_args} "}$1
	    ;;

	#   If no auto varyon was specifed, note that fact so we don't have to
	#   supply it later
	-n) 
	    cl_mkvg_args=${cl_mkvg_args:+"${cl_mkvg_args} "}$1
	    no_auto_varyon=true
	    ;;

	#   Arguments with operands get passed through along with their
	#   operands
	-d | -L | -m | -y )
	    cl_mkvg_args=${cl_mkvg_args:+"${cl_mkvg_args} "}$1
	    shift
	    cl_mkvg_args=${cl_mkvg_args:+"${cl_mkvg_args} "}$1
	    ;;

	#   Anything else should be an hdisk name.  Turn it into a PVID.
	* ) 
	    lspv | grep -w "^$1" | read disk pvid vg
	    if [[ -n $pvid ]] ; then
		cl_mkvg_args=${cl_mkvg_args:+"${cl_mkvg_args} "}$pvid
	    else
		#   If, for some reason, the passed argument doesn't show up
		#   in the lspv output, pass it through unchanged
		cl_mkvg_args=${cl_mkvg_args:+"${cl_mkvg_args} "}$1
	    fi
	    ;;
    esac
    shift
done

#
:   Go find all free disks across the given set of nodes
:   Returned file is in the form:
:   node: hdisk:pvid:concurrent_capable:ECM_only:size
:   FREEMAJORS:
#
/usr/es/sbin/cluster/sbin/cl_lspvids -cspoc "-f -n $_NODE_LIST" > $CL_DATFILE 

# Change the ownership of output file, for a non root user.
[[ $user_name != "root" ]] && chown $user_name $CL_DATFILE

#   If a major number wasn't supplied, provide one that will work across the
#   cluster.
if [[ -z $have_major ]] ; then
    major=$(/usr/es/sbin/cluster/cspoc/cl_getmajor)
    cl_mkvg_args="-V $major $cl_mkvg_args"
fi

#   If no auto varyon was not specified, force it
if [[ -z $no_auto_varyon ]] ; then
    cl_mkvg_args="-n $cl_mkvg_args"
fi

#
#   Invoke the cspoc code to create the volume group and make it known on all
#   cluster nodes.  
#
/usr/es/sbin/cluster/sbin/cl_mkvg -cspoc "-f $DEBUG -n $_NODE_LIST" $cl_mkvg_args
return $?