#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/dbts.sh 1.5 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006,2007 
# 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 
# @(#)87    1.5  src/bos/usr/lib/nim/methods/dbts.sh, cmdnim, bos720  4/13/07  11:53:05
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/dbts.sh
#
#   ORIGINS: 27
#

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

# --------------------------- module globals
REQUIRED_ATTRS="pull_request"
OPTIONAL_ATTRS="verbose"
IS_CLIENT=""
IS_MASTER=""
allow_boot=""
cosi=""
time=""
ARGUMENT=""

# --------------------------------------------------------------------------- #
# NAME:         usage
#
# FUNCTION:     Print usage message
#
# PARAMETERS:   None.
#
# RETURNS:      0 - successful function execution
# --------------------------------------------------------------------------- #
function usage {

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DBTS_USAGE} \
        	'%1$s [-v] [ThinServer]\n' "${PROGNAME}"

	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         ck_attr
#
# FUNCTION:     Check for valid attribute.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - valid attribute
#               1 - invalid attribute
# --------------------------------------------------------------------------- #
function ck_attr {

	# Check the following global flag:
	# ARGUMENT
	# source
	# location

	typeset tserver=""

	ck_user || return 1

        if [[ $(/usr/bin/ps -ef -o args | /usr/bin/grep "nim \\-o dbts" >/dev/null 2>&1; echo $?) -eq 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_PLEASE_EXECUTE_CMD} \
                        'Please execute the command from %1$s.\n' "${PROGNAME}"
		return 1
        fi

        if [[ $client_initiated = "yes" ]]; then
                thinservers=$ARGUMENT
        elif [[ $IS_MASTER = "yes" ]]; then
		if [[ -z $ARGUMENT ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_THINSERVER_REQUIRED} \
                        	'%1$s: A thin server is required.\n' "${PROGNAME}"
			usage
			return 1
		else
			# Ensure the clients are diskless/dataless
			for tserver in $ARGUMENT 
			do
				if [[ $(obj_exist "master" $tserver; echo $?) -ne 0 ]]; then
					/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CMD_NOT_EXIST} \
                        			'%1$s: \"%2$s\" does not exist.\n' "${PROGNAME}" "${tserver}"
					return 1
				fi
        	        done

			thinservers=$ARGUMENT
		fi

	elif [[ $IS_CLIENT = "yes" ]]; then
		if [[ -n $ARGUMENT ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_NO_THINSERVER_ARG} \
                        	'%1$s: Thin server argument is not allowed when executed on a thin server.\n' "${PROGNAME}"
			usage
			return 1
		fi
	fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         debug_ts
#
# FUNCTION:     Debug thin server.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - nim environment exist
#               1 - unable to determine environment
# --------------------------------------------------------------------------- #
function debug_ts {

	typeset tserver=$1
	typeset -i count=0
	typeset -i rc=0

        if [[ $client_iniated = "yes" ]]; then
                mkdebug $tserver
		rc=$?
	elif [[ $IS_MASTER = "yes" ]]; then
		for ts in $tserver
		do
			mkdebug $ts
			if [[ $? -ne 0 ]]; then
				rc=1
				break;
			fi
		done
	elif [[ $IS_CLIENT = "yes" ]]; then
                cosi=`get_attr_value "client" $NIM_NAME "spot"`
                /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CHECK_SPOT} \
                        '\n Checking filesets and network boot images for SPOT \"%s\".\n\ This may take several minutes ...\n\n' \
                        "${cosi}"

		/usr/sbin/nimclient -Fo change -a control=$NIM_NAME
		/usr/sbin/nimclient -o dbts 
		rc=$?
	fi

	return $rc
}

# --------------------------------------------------------------------------- #
# NAME:         mkdebug
#
# FUNCTION:     Issue the dkls_init or dtls_init with debug.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - nim environment exist
#               1 - unable to determine environment
# --------------------------------------------------------------------------- #
function mkdebug {

	typeset ts=$1
	typeset argstring=""

	common_image=`get_attr_value "master" $ts "spot"`
	if [[ -z $common_image ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_UNABLE_BOOT} \
                        '%1$s: Unable to boot server %2$s because a common image is not allocated to it.\n' "${PROGNAME}" "${ts}"
		return 1
	fi

	if [[ $(obj_exist "master" "${common_image}-debug"; echo $?) -ne 0 ]]; then
		/usr/sbin/cpcosi -c $common_image "${common_image}-debug"    || return 1
		/usr/sbin/nim -Fo check -a debug=yes "${common_image}-debug" || return 1
	fi

	/usr/sbin/swts -c "${common_image}-debug" $ts || return 1
	/usr/sbin/swts -c $common_image -n $ts        || return 1

	return 0
}

# ----------------------------------- dbts ---------------------------------- #
#
# NAME: dbts
#
# FUNCTION:	 /usr/sbin/dbts command 
#
# NOTES:
#
# RETURNS: (int)
#	0	= SUCCESS
#	1	= FAILURE
#
# --------------------------------------------------------------------------- #
trap cleanup 0
trap err_signal 2 11 15

# NIM initialization
nim_init

# set parameters from command line

while getopts :a:v x
do
	case ${x} in
                a)      # Command initated by nimclient.
                        # validate the attr ass
                        parse_attr_ass "${OPTARG}"

                        # include the assignment for use in this environment
                        eval ${variable}=\"${value}\"

                        if [[ $variable = "pull_request" ]]; then
                                client_initiated=yes
                        fi

                        if [[ $variable = "verbose" ]]; then
                                set -x
                                for i in $(typeset +f)
                                do
                                        typeset -ft $i
                                done
                        fi
			;;

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

		\?)	# unknown option
			usage
			exit 1
			;;
	esac
done

shift $((OPTIND - 1))

ARGUMENT=$*
ck_nim_env_ts  || exit 1
ck_attr	       || exit 1

debug_ts "${thinservers}"
rc=$?

exit $rc

# end of script