#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/cpcosi.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006 
# 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 
# @(#)33    1.3  src/bos/usr/lib/nim/methods/cpcosi.sh, cmdnim, bos720  3/9/06  18:30:37
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/cpcosi.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
devicetype=""
level=""
ARGUMENT=""
FILESYSTEM=""

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

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CPCOSI_USAGE} \
		'%1$s -c cosi [-l Location] [-S Server] [-v] COSI\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
	# cosi
	# server
	# location

	ck_user || return 1

	if [[ -z $ARGUMENT ]] || [[ -z $cosi ]]; then
		usage
		return 1
	fi

	if [[ $(obj_exist "master" $ARGUMENT; echo $?) -eq 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CMD_EXISTS} \
			'%1$s: \"%2$s\" already exists.\n' "${PROGNAME}" "${ARGUMENT}"
	        return 1
	fi

	if [[ -z $server ]]; then
		server=`get_attr_value "master" $cosi "server"`
	else
		if [[ $(obj_exist "master" $server; echo $?) -ne 0 ]]; then
			server=`ck_nim_server $server` 
			if [[ $? -ne 0 ]]; then
				/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_NOT_MACHINE} \
					'%1$s: Server \"%2$s\" is not a NIM machine object.\n' "${PROGNAME}" "${server}"
				return 1
			fi
		fi
	fi

	if [[ -z $location ]]; then
		location=`get_attr_value "master" $cosi "location"`
		location="${location%%/`/usr/bin/basename $location`}"
		location="${location%%/`/usr/bin/basename $location`}"
	fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         copy_cosi
#
# FUNCTION:     Copy the common image.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - nim environment exist
#               1 - unable to determine environment
# --------------------------------------------------------------------------- #
function copy_cosi {

	typeset name=$1
	typeset cosi=$2
	typeset server=$3
	typeset location=$4

        /usr/sbin/nim -o define -t spot -a server=$server -a location=$location -a source=$cosi $name

        if [[ $(obj_exist "master" $name; echo $?) -ne 0 ]]; then
                return 1
        fi

	return 0
}

# ---------------------------------- cpcosi --------------------------------- #
#
# NAME: cpcosi
#
# FUNCTION:	 /usr/sbin/cpcosi command 
#
# NOTES:
#
# RETURNS: (int)
#	0	= SUCCESS
#	1	= FAILURE
#
# --------------------------------------------------------------------------- #
trap cleanup 0

# NIM initialization
nim_init

# set parameters from command line
while getopts :S:c:l:v x
do
	case ${x} in

		S)	# Specify the server to store the common image.
			server=${OPTARG}
			;;

		c)	# Specify the common image to copy
			cosi=${OPTARG}
			;;

		l)	# Specify the path location to store the common image.
			location=${OPTARG}
			;;

		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  || exit 1
ck_attr	    || exit 1

copy_cosi "${ARGUMENT}" $cosi $server $location  || exit 1
rc=$?
exit $rc

# end of script