#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_sync_client.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2012,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 
# @(#)36 1.3 src/bos/usr/lib/nim/methods/c_sync_client.sh, cmdnim, bos720, 1440A_720 9/18/14 11:51:40

# 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

#---------------------------- local defines     --------------------------------
NIMINIT="/usr/sbin/niminit"
NIMCLIENT="/usr/sbin/nimclient"

#---------------------------- module globals    --------------------------------
OPTIONAL_ATTRS="force"

#----------------------------------- c_sync_client ----------------------------------
#
# NAME: c_sync_client
#
# FUNCTION:
#		Reinitialize the client to pick up new alternated master added
#               to the NIM environment.  The method is executed by m_sync when
#               the master executes "nim -o sync <alternate_master>".
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#		calls error on failure
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#		0	= success
#		1	= failure
#
# OUTPUT:
#-------------------------------------------------------------------------------

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

# NIM initialization
nim_init

# initialize local variables
typeset c=""

# 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}\"
				;;

		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

# Save value we may need
# Don't take the value from /etc/niminfo, the value might be stale
NIMSHELL=`$NIMCLIENT -l -a connect $NIM_NAME | $GREP "connect =" | $CUT -d= -f2`

# Backup the niminfo file first so we can restore in case of failure
$CP /etc/niminfo /etc/niminfo.sync

# remove the /etc/niminfo to pick up NIM_MASTER_HOSTNAME_LIST
# niminit will also add the alternate_master to /.rhosts if connect=shell
$RM /etc/niminfo >/dev/null 2>&1

RC=1
if [[ $NIMSHELL = *nimsh* ]]; then
	$NIMINIT -a restart_nimsh=no -a name=$NIM_NAME -a connect=nimsh -a master=$NIM_MASTER_HOSTNAME >/dev/null 2>&1
	RC=$?
	if [[ $NIMSHELL = *secure* ]] && [[ $RC -eq 0 ]]; then
		$NIMCLIENT -c >/dev/null 2>&1
		for alternate_master in $(echo $NIM_MASTER_HOSTNAME_LIST | $CUT -d= -f2)
		do
			if [[ $alternate_master != $NIM_MASTER_HOSTNAME ]]; then
				$NIMCLIENT -o get_cert -a master_name=$alternate_master >/dev/null 2>&1
			fi
		done
	fi
else
	$NIMINIT -a name=$NIM_NAME -a master=$NIM_MASTER_HOSTNAME >/dev/null 2>&1
	RC=$?
fi

if [[ $RC -ne 0 ]]; then
	# If the operation fail, restore the old niminfo file
	$MV /etc/niminfo.sync /etc/niminfo >/dev/null 2>&1
else
	$RM /etc/niminfo.sync >/dev/null 2>&1
fi

# Add the alternate_master hostname (reported by the ATTR_IF attribute) to rhost
if [[ $NIMSHELL = *shell* ]]; then
	altmstr_name=`$NIMCLIENT -l -t alternate_master 2>/dev/null | $AWK '{print $1}'`
	if [[ -n $altmstr_name ]]; then
		altmstr_host=`$NIMCLIENT -l -a if $altmstr_name 2>/dev/null | $TAIL +2 | $AWK '{print $4}'`
		for host_name in $altmstr_host; do
			$GREP "$host_name root" $RHOSTS >/dev/null 2>&1
			if [[ $? -ne 0 ]]; then
				echo "$host_name root" >> $RHOSTS
			fi
		done
	fi
fi

# finished
exit 0
