#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/rmts.sh 1.7.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 
# @(#)44    1.7.1.2  src/bos/usr/lib/nim/methods/rmts.sh, cmdnim, bos720  7/14/11  19:06:36
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/rmts.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_RMTS_USAGE} \
		'%1$s [-f] [-v] Thinserver\n' "${PROGNAME}"

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         remove
#
# FUNCTION:     Remove thinserver.
#
# 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_TS_PER_OP} \
                	'%1$s: Only one thinserver allowed per operation.\n' "${PROGNAME}"
        	return 1
	elif [[ -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
		if [[ $(obj_exist "master" $ARGUMENT; echo $?) -eq 0 ]]; then
			machine_type=`get_attr_value "master" $ARGUMENT "type"`
			if [[ $machine_type != "diskless" ]] &&
			   [[ $machine_type != "dataless" ]]; 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}" "${machine_type}" "${ARGUMENT}"
				echo
				return 1
			fi

			DUMP_DEV_REMAIN=`/usr/sbin/lsnim -l $ARGUMENT | grep "[ 	]dump[ 	]*=" | awk '{print $3}'`
			if [[ $force = "yes" ]]; then
				uninitialize_ts $ARGUMENT || return 1
				rm_nim_obj $ARGUMENT || return 1
			else
				rm_nim_obj $ARGUMENT
                                if [[ $? -ne 0 ]]; then
					/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_TRY_FORCE} \
						'Try using the force flag.\n' 
					return 1
				fi
			fi
			if [[ -n "$DUMP_DEV_REMAIN" ]]; then
                                alloc_count=`/usr/sbin/lsnim -a alloc_count $DUMP_DEV_REMAIN | tail -1 | awk -F= '{print $2}'`
                                if  [[ $alloc_count -eq 0 ]]; then
                                	rm_nim_obj $DUMP_DEV_REMAIN || return 1
                                fi
                        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
}

# ----------------------------------- rmts ---------------------------------- #
#
# NAME: rmts
#
# FUNCTION:	 /usr/sbin/rmts 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
