#!/usr/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
#                                                      
# @(#)  7d4c34b 43haes/usr/sbin/cluster/utilities/cl_get_role.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM 
#
# This utility aims at finding the role of a user.
# The utility may or may not take arguments. The argument passed is the user name of the
# for which we want to know the role. If no arguments are passed it finds the role of the 
# real user of the process.

#====================================================
# Get the location of the clutils.log file
#====================================================
export PATH=$(/usr/es/sbin/cluster/utilities/cl_get_path all)
CLUTIL_LOG_DIR=$(clodmget -q "name = clutils.log" -n -f value HACMPlogs)
if [[ -z $CLUTIL_LOG_DIR ]]
then
        CLUTIL_LOG_DIR="/var/hacmp/log"
fi
CLUTIL_LOG="$CLUTIL_LOG_DIR/clutils.log"

if [[ -z $1 ]]
then
    [[ -z "$LOGIN" ]] && user_name=$(id -nu 2>/dev/null) || user_name="$LOGIN"
    if [[ $? == 0 ]];then
        echo "Current user is : $user_name" >> $CLUTIL_LOG
    else
        echo "Failed to get current user" >> $CLUTIL_LOG
        exit 1
    fi
else
    user_name="$1"
fi

typeset role=""
tmp_role=$(lsuser -f $user_name|grep -w roles|cut -d '=' -f2 2>/dev/null)
if echo $tmp_role |grep -wq "ha_admin"; then
    role="ha_admin"
elif echo $tmp_role |grep -wq "ha_op"; then
    role="ha_op"
elif echo $tmp_role |grep -wq "ha_mon"; then
    role="ha_mon"
elif echo $tmp_role |grep -wq "ha_view"; then
    role="ha_view"
fi

if [[ -z $role ]]
then
    echo "Failed to get role information for \"$user_name\" user or PowerHA role is not assigned to the \"$user_name\" user." >>$CLUTIL_LOG
    exit 1
fi

echo $role
echo "Role of \"$user_name\" user is : $role" >>$CLUTIL_LOG

exit 0