#!/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/cl_getmajor.sh 1.4 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1999,2004 
# 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/cl_getmajor.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#

#
#   Input from cllspvids, or another file containing similar output
#   from 'clgetpvid'
#

# Check if RBAC is enabled
typeset is_rbac_enabled=""
is_rbac_enabled=$(/usr/es/sbin/cluster/utilities/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=${1:-/var/hacmp/tmp/cllspvids.out_$user_name} # default input file
else
    CL_DATFILE=${1:-/var/hacmp/tmp/cllspvids.out} # default input file
fi

if [[ ! -s $CL_DATFILE ]]	    # if file isn't there
then
    exit 2			    # note a failure
fi

#
#   Find the largest free major number, as reported by cllspvids.  The
#   records of major numbers are of the form:
#
#	nodename: FREEMAJORS:44..46,48,50,52..54,56...
#
#   In this example, major numbers 56 and higher are free.  Hence, the largest
#   such last number from each node must be free, also.
#
integer MAXMAJOR
MAXMAJOR=$(grep -w FREEMAJORS $CL_DATFILE | \
	    # get the lists of major numbers in use on each node
	   sed 's/.*[^0-9]\([1-9][0-9]*\)[^0-9]*/\1/' | \
	    # pick up the last (largest) number in the list
	    # the last is followed by things that aren't numbers, 
	    # and preceeded by at least one thing that isn't a number
	   sort -u -n | \
	    # largest across the cluster is last
	   tail -1)	
	    # get that one

#
#   And put out that free major number
#
if [[ -n $MAXMAJOR ]]		    # if found a maximum major number
then
    echo $(( MAXMAJOR ))	    # its free across the checked nodes
else
    exit 1			    # else note not found
fi
