#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/rmcosi.sh 1.5.1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006,2011 
# 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 
# @(#)37    1.5.1.2  src/bos/usr/lib/nim/methods/rmcosi.sh, cmdnim, bos720  7/14/11  18:49:03
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/rmcosi.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
level=""
ARGUMENT=""
IS_CLIENT=""
IS_MASTER=""
LEVEL_ONE=""
LEVEL_TWO=""
LEVEL_THREE=""

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

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

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         ck_nim_env
#
# FUNCTION:     Checks for existing nim environment
#
# PARAMETERS:   None.
#
# RETURNS:      0 - nim environment exist
#               1 - unable to determine environment
# --------------------------------------------------------------------------- #
function ck_nim_env {

        /usr/bin/lslpp -l bos.sysmgt.nim.master >/dev/null 2>&1

        if [[ $? -eq 0 ]]; then
		ODMDIR=/etc/objrepos /usr/bin/odmget -q "name=master" nim_object 2>/dev/null | \
			/usr/bin/grep master > /dev/null
		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CMD_NOT_MASTER} \
				'%1$s: Machine is not configured as a NIM master.\n' "${PROGNAME}"
			return 1
		fi
	else
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CMD_EXEC_MASTER} \
                	'%1$s: This command can only be executed on a NIM master.\n' "${PROGNAME}"
		return 1
        fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         remove
#
# FUNCTION:     Remove common image.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - successful function execution
#               1 - unsuccessful
# --------------------------------------------------------------------------- #
function remove {

	if [[ $(echo $ARGUMENT | wc -w) -gt 2 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ONE_COSI_PER_OP} \
                        '%1$s: Only one common image is allowed per operation.\n' "${PROGNAME}"
	        return 1
	elif [[ -z $ARGUMENT ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_COSI_REQUIRED} \
                        '%1$s: A common image is required.\n' "${PROGNAME}"
		usage
		return 1
	else
		if [[ $(obj_exist "master" $ARGUMENT; echo $?) -eq 0 ]]; then
			object_type=`get_attr_value "master" $ARGUMENT "type"`
			if [[ $object_type != "spot" ]]; then
				/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_BAD_TYPE_FOR} \
					'0042-024 %s: \"%s\" is not a valid type for \"%s\"' \
					"${PROGNAME}" "${object_type}" "${ARGUMENT}"
				echo
				return 1
			fi

			thinserver=`get_cosi_client "master" $ARGUMENT`
        	        if [[ -n $thinserver ]]; then
				/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_TS_USING_COSI} \
					'The following thinserver are using the %1$s common OS image:\n' "${ARGUMENT}"
                        	echo $thinserver | awk 'BEGIN {RS=" "} {print}'

	                        if [[ $force = "yes" ]]; then
	                                for ts in $thinserver
        	                        do
                        	                uninitialize_ts $ts || return 1
                                	done

					rm_nim_obj $ARGUMENT
	                        else
					/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_TRY_FORCE} \
						'Try using the force flag.\n' 
                	                return 1
	                        fi
        	        else
                	        # Remove the image
				rm_nim_obj $ARGUMENT
        	        fi
	        else
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CMD_NOT_EXIST} \
				'%1$s: \"%2$s\" does not exist.\n' "${PROGNAME}" "${ARGUMENT}"
			return 1
	        fi
	fi

	return 0
}

# ---------------------------------- rmcosi --------------------------------- #
#
# NAME: rmcosi
#
# FUNCTION:	 /usr/sbin/rmcosi 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 :fv x
do
	case ${x} in

		f)		# specify to force the removal of the common image
				force="yes"
				;;

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

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

ck_nim_env || exit 1
ck_user    || exit 1

shift $((OPTIND - 1))

ARGUMENT=$*

remove
rc=$?
exit $rc

# end of script