#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/nim_clients_setup.sh 1.13.1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2001 
# 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 
# @(#)99 1.13 src/bos/usr/lib/nim/methods/nim_clients_setup.sh, cmdnim, bos61D 9/26/05 15:39:11
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/nim_clients_setup.sh
#
#   ORIGINS: 27
#

NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
export NIMPATH NIM_METHODS
. ${NIM_METHODS}/c_sh_lib

# Set PROGRAM_NAME variable for error reporting.
PROGRAM_NAME="nim_clients_setup"

# --------------------------- module globals
mksysb=""
n_mksysb=""
client_defs=""
mac_grp=""
reboot=""
install_src="mksysb"
# ------------------------------------------------------------------ #

# ------------------------------------- nim_clients_setup
#
# NAME: nim_clients_setup
#
# FUNCTION:	 Defines lpars using /usr/sbin/nimdef command ( -c option )
#            Initializes BOS install operation on defined lpars -OR-
#            (! -c) on the specified NIM client objects; if no objects
#            or given on the command line, then all standalone client 
#            objects in NIM environment are initialized.
#
# NOTES:
#
#   This script allows for the defining of new LPARs as client objects
#   and initializes BOS installs for clients in the NIM environment.
#
#   The following outline gives an overview:
#     1) Export environment variable NIM_LICENSE_ACCEPT
#        - necessary for software license agreement
#     2) Set NSORDER in /etc/environment
#        - necessary for name resolution (if host exists in /etc/hosts)
#     3) Define LPARs from client.defs file :: if (-c) flag specified
#        - User must edit stanzas in /export/nim/client.defs file
#          prior to using nim_clients_setup
#     4) Sets up client objects for install
#        - if (-c) flag used: newly created LPARs are initialized for install
#        - otherwise: specified clients or all existing LPARs 
#          are initialized for install
#     5) Resources created during nim_master_setup script are used as
#        resources for current BOS install
#        - if (-m) flag is used: user may specify an alternate mksysb image
#          to use for restore during BOS install
#        - otherwise: mksysb image created during nim_master_setup will be
#          used for restore during BOS install
#
# RETURNS: (int)
#	0	= SUCCESS
#	1	= FAILURE
#
# --------------------------------------------------------------------------- #
#
# set parameters from command line
while getopts :m:crnv x
do
	case ${x} in

		m)		# validate the mksysb assignment
				if [[ "${OPTARG}" != +(?) ]]; then
				   ${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_BAD_ARG} 'Bad argument assignment : %s\n.' ${OPTARG}
				   exit 1
				fi
				
				# include the mksysb assignment
				mksysb="${OPTARG}"

				# may need to create a mksysb resource object
				/usr/sbin/lsnim $mksysb >/dev/null 2>&1
				if [[ $? -ne 0 ]]; then
					# is image local? exist?
					if [[ -e $mksysb ]]; then
						
						# object name
						n_mksysb="${mksysb##*/}"
						n_mksysb="${n_mksysb%%.*}"

						# try and define n_mksysb object
						/usr/sbin/nim -o define -t mksysb \
						  -a server=master -a location=$mksysb ${n_mksysb}
						[[ $? -ne 0 ]] && exit 1

					else
						${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_EMPTY_LOCATION} 'Location %s is empty or not found.\n' ${mksysb}
						exit 1
					fi
				else
					n_mksysb="${mksysb}"
				fi
				;;

        c)		# define clients using client.defs file
				client_defs="yes"
				;;
		
        r)		# reboot clients for BOS install
				reboot="yes"
				;;
		
        n)		# native (rte) install for clients?
				install_src="rte"
				;;
		
		v)		# verbose mode (for debugging)
				set -x
				for i in $(typeset +f)
				do
					typeset -ft $i
				done
				;;

		\?)		# unknown option
				${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_NIM_CLIENTS_SETUP_USAGE} 'Usage nim_clients_setup: Setup and Initialize BOS install for NIM clients.\n\tnim_clients_setup [-m <mksysb resource>] [-n]\n\t                  [-c] [-r] [-v] <client object(s)>\n\n-m    specify mksysb resource object name -OR- absolute file path.\n-n    peform native install and ignore mksysb resource (rte).\n-c    define client objects from client.defs file.\n-r    reboot client objects for BOS install.\n-v    Enables debug output.\n\nNote: If no client object names are given, then all clients in the\n\tNIM environment are enabled for BOS install; unless clients\n\tare being defined (-c option).\n'
				exit 1
				;;
	esac
done

shift $((OPTIND - 1))
mac_grp="$*"

# Set accept_licenses attribute for 5.1.x.x installs
export NIM_LICENSE_ACCEPT=yes

# Append NSORDER variable for local network defs
if ! /usr/bin/grep NSORDER /etc/environment; then 
	echo "NSORDER=local,bind" >> /etc/environment
fi

# pause for a bit
/usr/bin/sleep 1

# if:   flag set & client.defs file exists 
#       - add LPARs to NIM environment
# else: generate install list from existing client objects

rc=0
if [[ -n $client_defs ]]; then
	if [[ -s /export/nim/client.defs ]]; then

		${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_DEFINE_CLIENTS} 'Defining clients from client.defs file...'

		# remove old lpar_grp object
		/usr/sbin/nim -o remove lpar_grp > /dev/null 2>&1

		# define client objects
		/usr/sbin/nimdef -d -f /export/nim/client.defs > /dev/null 2>&1
		let rc=$rc+$?

		# was LPAR group created?
		/usr/sbin/lsnim lpar_grp > /dev/null 2>&1
		let rc=$rc+$?

	else
		${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_ERR_DEFINE_CLIENTS} '\nError: /export/nim/client.defs file is empty or not found.\n'
		let rc=$rc+1
	fi

else
	if [[ -z $mac_grp ]]; then
		${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_CLIENT_OBJ_LIST} '\nGenerating list of client objects in NIM environment...'
		mac_grp=`/usr/sbin/lsnim -t standalone | /usr/bin/cut -f1 -d" "`
	fi

	# we should have a list of machines by now..
	if [[ -z $mac_grp ]]; then
		let rc=$rc+1
	else
		${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_LOCATE_MACS} '\nLocating %s...' ${mac_grp}
	fi
fi

# pause for a bit
/usr/bin/sleep 1

# resulting status?
if [[ $rc -gt 0 ]]; then
	${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_UNABLE_DEFINE_CLIENTS} '\nUnable to define client list for BOS install - Exiting.\n'
	exit 1
else
	${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n'
fi


# check for resource group - created from nim_master_setup script
${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_CHK_RES_GRP} '\nChecking for resource group %s...' "basic_res_grp"

# pause for a bit
/usr/bin/sleep 1

/usr/sbin/lsnim basic_res_grp > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
	${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_ERR_RES_GRP} '\nThe resource group %s is not defined.\nPlease define using desired resources for install - Exiting.\n' "basic_res_grp"
	exit 1
else
   # lets make it the default_res for members
   /usr/sbin/nim -o change -a default_res=basic_res_grp ${mac_grp:-lpar_grp}
   ${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n'
fi

#--------------------------------------------------------------#
#--------------------------------------------------------------#
#                Install client objects                        #
#--------------------------------------------------------------#

# lets initiate BOS installs for clients -or- lpar_grp members
/usr/sbin/nim -o bos_inst -a source=${install_src} \
	${n_mksysb:+-a mksysb=$n_mksysb} -a boot_client=${reboot:-no} \
	${mac_grp:-lpar_grp} 
#--------------------------------------------------------------#
#--------------------------------------------------------------#
#--------------------------------------------------------------#

# end of script
