#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72L src/bos/usr/lib/nim/methods/populate_nodeinfo.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2018 
# 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 

#---------------------------- module globals    --------------------------------
#-------------------------------------------------------------------------------
PATH=.:/usr/sbin:/usr/bin:/bin
export PATH=$PATH

pid=$$
tmpfile="/var/tmp/nodeinfo.$pid"
nodeinfo="/etc/ibm/sysmgt/dsm/nodeinfo"

#---------------------------- function defines    ------------------------------
#-------------------------------------------------------------------------------
function cleanup {

    # remove temp nodeinfo file
    rm -f $tmpfile
    rm -f ${tmpfile}.nodes
}

function err_cleanup {

    trap "" 1 2 11 15
    cleanup
    exit 1
}

function pop_nodeinfo {

[[ -f $nodeinfo ]] && cp $nodeinfo ${tmpfile}.nodes || >${tmpfile}.nodes

lsnim -a mgmt_profile1 | while read line ; do
    #if the line conatains only the node name
     echo $line | grep : 1>/dev/null
    [[ $? -eq 0 ]] && name=`echo $line | awk -F':' '{print $1}'` && continue

    # else line contains node info
    hmc=`echo $line | awk '{print $3}'`
    type=`lsnim $hmc 2>/dev/null | awk '{print $3}'`
    lpar_id=`echo $line | awk '{print $4}'`
    cec_name=`echo $line | awk '{print $5}'`
    password=`lsnim -a passwd_file $hmc 2>/dev/null | awk '/passwd_file =/{print $3}'`
    # give error message if password file is missing.
    if [[ ! -s $password ]]; then
        echo "Password file has not been set properly. Please create and set password file attribute using:\nnim -o change -a passwd_file=file_name $hmc"
        echo ""
        continue
    fi

    # skip over an entry if the info is incomplete
    if [[ -z $hmc ]] || [[ -z $type ]] || [[ -z $lpar_id ]] || [[ -z $cec_name ]]; then
        echo "error processing nim resource $name"
        echo ""
        continue
    fi

    cec_serial=`lsnim -a serial $cec_name | awk '/serial =/{ print $3 }' | awk -F'*' '{ print $2}'`
    cec_model=`lsnim -a serial $cec_name | awk '/serial =/{ print $3 }' | awk -F'*' '{ print $1}'`
     # skip over the entry if it's info is incomplete
    if [[ -z $cec_serial ]] || [[ -z $cec_model ]]; then
        echo "error processing serial and model number of cec $cec_name"
        echo ""
        continue
    fi

    grep -v $name ${tmpfile}.nodes  > $tmpfile
    mv $tmpfile ${tmpfile}.nodes
    echo "$name|$type|$hmc|TargetHWTypeModel=$cec_model:TargetHWSerialNum=$cec_serial:TargetLPARID=$lpar_id|$password" >> ${tmpfile}.nodes

done # while read line

cp ${tmpfile}.nodes $nodeinfo

}

#-------------------------------  MAIN  ---------------------------------------
#------------------------------------------------------------------------------

#signal catch
trap cleanup 0
trap err_cleanup 1 2 11 15

#set parameters from command line
while getopts x c; do
	case ${c} in

		x)	# debug mode (for debugging)
			set -x
			for i in $(typeset +f)
			do
				typeset -ft $i
			done
			;;

		\?)	# unknown option
			echo "\nUsage:\tpopulate_nodeinfo"
			exit 1
			;;
	esac
done
shift $((OPTIND - 1))
pop_nodeinfo
exit 0
