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

# @(#)16 1.51  src/bos/usr/lib/nim/methods/c_initiate_bootp.sh, cmdnim, bos72L, l2018_13B0  3/2/18  10:14:33 

#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/c_initiate_bootp.sh
#		
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1993, 1995, 1996
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

# include common NIM shell defines/functions
NIMPATH=${0%/*}
NIMPATH=${NIMPATH%/*}
[[ ${NIMPATH} = ${0} ]] && NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
. ${NIM_METHODS}/c_sh_lib || exit 99 

#---------------------------- local defines     --------------------------------
#---------------------------- globals    --------------------------------
REQUIRED_ATTRS="adpt_name"
OPTIONAL_ATTRS="mode ha gw sa ip sm no_client_boot platform speed duplex file vlan_tag vlan_pri"
BOOTLIST_OPTS="ha gw sa ip speed duplex file vlan_tag vlan_pri"

adpt_name=""
mode="normal"
ha=""
gw=""
sa=""
ip=""
bootlist_attrs=""
speed=""
duplex=""
host_adpt=""
vlan_tag=""
vlan_pri=""

#-------------------------------------------------------------------------------

# signal processing
trap cleanup 0
trap err_signal 1 2 11 15

# NIM initialization
nim_init

# set parameters from command line
while getopts :a:qv c
do
	case ${c} in

	a)		# validate the attr ass
			parse_attr_ass "${OPTARG}"

			# include the assignment for use in this environment
			eval ${variable}=\"${value}\" 2>${ERR} || \
				err_from_cmd eval
			;;

	q)		# show attr info
			cmd_what
			exit 0
			;;

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

	\?)	# unknown option
			error ${ERR_BAD_OPT} ${OPTARG}
			;;
	esac
done

#
# KVM systems should not set bootlist/reboot
#
on_kvm=`${PRTCONF} -t | ${GREP} KVM`

if [ -n "$on_kvm" ]; then
	exit 0
fi

# check for missing attrs
ck_attrs

# initialize bootlist attr information
# this information is returned via the "bootlist_attrs" global variable
init_bootlist_attrs "${ip##*=}" "${sa##*=}" "${gw##*=}" "${file##*=}"

# was specific adapter name given?
if [[ ${adpt_name} != +([!0-9])+([0-9]) ]]
then

	# no specific adpter given - try all adapters of this type
	# generate the list from devices in CuDv
	list=$(	${ODMGET} -q"name like '${adpt_name}*'" CuDv | \
				${AWK} '$1=="name"{gsub(/"/,"");print $3}' )

else

	# just use specified adapter
	list=${adpt_name}

fi

# get the short hostname - resolve device name
nim_hostname="${NIM_HOSTNAME%%.*}"

# interfaces may have similar strings in their address
host_adpt=`/usr/bin/netstat -if inet | ${AWK} -v cmp=${nim_hostname} '{
   split($4,ar,".")
   if ( cmp == ar[1] ) {
     print $1
   }
} '`

# check all adapters specified in the list
adpt_list=""
for i in ${list}
do

	# add adpt to the list
	adpt_name=${i##*/}

	# set speed / duplex for appropriate adapter
	if [[ ${adpt_name} = "ent${host_adpt#e[nt]*}" ]]
	then

		# if we have speed/duplex values
		# -  get them from parms passed
		# -  otherwise, get values from CuAt
		media_speed=`${LSATTR} -El ${adpt_name} | ${AWK} '/^media_speed/ {x=tolower($2); print x}'`

		# if media_speed is auto_negotiation, then
		# set speed/duplex to auto
		if [[ ${media_speed} = "auto_negotiation" || ${media_speed%%_*} -ge 1000 ]]
		then
			# Open Firmware may not support card setting (do nothing)
			CuAt_speed=""
			CuAt_duplex=""
		else
			CuAt_speed="${media_speed%%_*}"
			CuAt_duplex=`${ECHO} ${media_speed} | ${CUT} -f2 -d"_"`
		fi

		# set speed/duplex values
		echo ${speed:=$CuAt_speed} >/dev/null 2>&1
		echo ${duplex:=$CuAt_duplex} >/dev/null 2>&1

		adpt_list="${adpt_list} \
		${adpt_name} ${speed:+speed=$speed} ${duplex:+duplex=$duplex} ${bootlist_attrs} ${vlan_tag:+vlan_tag=$vlan_tag} ${vlan_pri:+vlan_pri=$vlan_pri} "

	else
		adpt_list="${adpt_list} ${adpt_name} ${bootlist_attrs} "
	fi

done


#
# Build the command line 
#
cmdline="-m ${mode} ${adpt_list}"

#
# Everything should be tickity boo Now lets set the bootlist. 
#
blvdisk=`${BOOTLIST} -m normal -o`

${BOOTLIST} ${cmdline} ${blvdisk} 2>${ERR} || err_from_cmd ${BOOTLIST}

if [[ "${no_client_boot}" != "yes" ]]
then
	#
	# Wall the users... 
	#

	tmpfile="/tmp/msg.$$"
    ${C_ERRMSG} ${MSG_REBOOT_WARNING} ${C_ERRMSG_MSG} "" "" "" "" > $tmpfile 2>&1
    ${CAT} $tmpfile | ${WALL} 
    #
    ${SLEEP} 4
    #
    ${CAT} $tmpfile | ${WALL} 
    #
    ${RM} $tmpfile 

    # Set platform if ${platform} is null
    [[ -z ${platform} ]] && platform=`${BOOTINFO} -T 2>/dev/null`

    # If the machine model is "4C", which is a PCI bus POWERPC machine
    # (model 43P), then we must use "shutdown" instead of "reboot" because
    # of a hardware limitation that causes "reboot" to not complete.
    # The shutdown must be scheduled, otherwise the rsh which runs this
    # method will be killed and the shutdown will fail.
    # Adding rs6k model 42T to the shutdown list.
    MAC_MODEL=`${UNAME} -m 2>/dev/null | ${CUT} -c 9-10 2>/dev/null`
    if [[ (${MAC_MODEL} = "91" && ${platform} = "rs6k") || (${MAC_MODEL} = "4C") ]]
    then
        echo "/usr/sbin/shutdown -Fr &" > /tmp/__NIM_shutdown
        ${CHMOD} 755 /tmp/__NIM_shutdown
		# lets verify that the cron daemon is running
		# - start it
		cron_up=`${PS} -u root | ${GREP} -q cron; echo $?`
		if [ -z "$cron_up" ] || [ "$cron_up" != "0" ]
		then
		  /usr/sbin/cron >/dev/null 2>&1 &
		fi
        ${AT} now + 1 minute < /tmp/__NIM_shutdown > /dev/null 2>&1
    else
        ${NIMCLIENT} -S shutdown 2>${ERR} || warning_from_cmd ${NIMCLIENT}
        ${REBOOT} 2>${ERR} || err_from_cmd ${REBOOT}
    fi

fi

#
exit 0
