#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/lscosi.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 
# @(#)49    1.3  src/bos/usr/lib/nim/methods/lscosi.sh, cmdnim, bos720  3/9/06  18:36:20
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/lscosi.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_LSCOSI_USAGE} \
'%1$s [[-l{1|2|3}] . . .] [-v] [COSI]\n\
1\tSummary listing.\n\
2\tDisplay common image software content.\n\
3\tDisplay common image installation log, error report.\n' "${PROGNAME}"

	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         display_thin_server
#
# FUNCTION:     Display thin server information
#
# PARAMETERS:   None.
#
# RETURNS:      0 - successful
#               1 - failure
# --------------------------------------------------------------------------- #
function display_thin_server {

	typeset cosi=$1
	typeset output
	typeset line
	typeset client
	typeset -i count=0
	typeset print_heading

	if [[ -z $cosi ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CMD_NOT_EXIST} \
			'%1$s: \"%2$s\" does not exist.\n' "${PROGNAME}" "<>"
		return 1
	fi

	if [[ $IS_MASTER = "yes" ]]; then
		output=`/usr/sbin/lsnim -a spot`
	elif [[ $IS_CLIENT = "yes" ]]; then
		output=`/usr/sbin/nimclient -l -a spot`
	fi

	count=0
	for line in $output
	do
		if [[ $count -eq 0 ]]; then
			client=$line
		fi

		count=$count+1
		if [[ $count -eq 4 ]]; then
			if [[ "${cosi}" = "$line" ]]; then
				if [[ -z $print_heading ]]; then
			                echo
					/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_THINSERVER} \
						'Thin Server Using The Common OS Image:\n'

					print_heading=1
				fi

				if [[ $client != "master:" ]]; then
					echo "$client" | /usr/bin/cut -f1 -d: 
				fi
			fi
			count=0
		fi
	done

	echo 

	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         display_level_one
#
# FUNCTION:     Display level one output
#
# PARAMETERS:   None.
#
# RETURNS:      0 - successful
#               1 - unsuccessful
# --------------------------------------------------------------------------- #
function display_level_one {

	typeset cosi=$1

	if [[ -z $cosi ]]; then
		return 1
	else
		if [[ $IS_MASTER = "yes" ]]; then
			/usr/sbin/lsnim -l $cosi
		elif [[ $IS_CLIENT = "yes" ]]; then
			/usr/sbin/nimclient -l -l $cosi
		fi
		display_thin_server $cosi
	fi	

	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         display_level_two
#
# FUNCTION:     Display level two output
#
# PARAMETERS:   None.
#
# RETURNS:      0 - successful
#               1 - unsuccessful
# --------------------------------------------------------------------------- #
function display_level_two {

	typeset cosi=$1

	if [[ -z $cosi ]]; then
		return 1
	else
		echo $cosi
		if [[ $IS_MASTER = "yes" ]]; then
			/usr/sbin/nim -o lslpp -a lslpp_flags=-L $cosi
		elif [[ $IS_CLIENT = "yes" ]]; then
			/usr/bin/lslpp -L
		fi
	fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         display_level_three
#
# FUNCTION:     Display level three output
#
# PARAMETERS:   None.
#
# RETURNS:      0 - successful
#               1 - unsuccessful
# --------------------------------------------------------------------------- #
function display_level_three {

	typeset cosi=$1

	if [[ -z $cosi ]]; then
		return 1
	else
		echo $cosi
		if [[ $IS_MASTER = "yes" ]]; then
			/usr/sbin/nim -o showlog -a log_type=niminst $cosi 2>/dev/null
			/usr/sbin/nim -o showlog -a log_type=lppchk $cosi 2>/dev/null
			/usr/sbin/nim -o showlog -a log_type=nimerr $cosi 2>/dev/null
		elif [[ $IS_CLIENT = "yes" ]]; then
			errpt -a
		fi
	fi
	
        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:
	# level

	ck_user || return 1

	if [[ -n "${level}" ]]; then
		# put hyphens in front of all flags
		level=$( ck_installp_flags "${level}" )
	fi

	/usr/bin/getopt "123" ${level} > /dev/null 2>&1
	if [[ $? -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_INVALID_LEVEL} \
			'%1$s: Invalid level.\n' "${PROGNAME}"
		return 1
	fi

	if [[ -z $level ]] || [[ $level = *1* ]]; then
		LEVEL_ONE="yes"
	fi

	if [[ $level = *2* ]]; then
		LEVEL_TWO="yes"
	fi

	if [[ $level = *3* ]]; then
		LEVEL_THREE="yes"
	fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         list_cosi
#
# FUNCTION:     List common image information.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - successful
#               1 - unsuccessful
# --------------------------------------------------------------------------- #
function list_cosi {

	if [[ -z $ARGUMENT ]] && [[ $IS_MASTER = "yes" ]]; then
        	/usr/sbin/lsnim -t spot | /usr/bin/awk '{print $1}' | /usr/bin/sort
	else
	        if [[ -z $ARGUMENT ]]; then
        	        if [[ $IS_MASTER = "yes" ]]; then
                	        common_images=`/usr/sbin/lsnim -t spot | /usr/bin/awk '{print $1}' | /usr/bin/sort`
	                elif [[ $IS_CLIENT = "yes" ]]; then
				common_images=`get_attr_value "client" $NIM_NAME "spot"`
                	fi

	                for image in $common_images
        	        do
                	        [[ "${LEVEL_ONE}"   = "yes" ]] && display_level_one $image
	                        [[ "${LEVEL_TWO}"   = "yes" ]] && display_level_two $image
	                        [[ "${LEVEL_THREE}" = "yes" ]] && display_level_three $image
	                done
        	else
	                if [[ $IS_CLIENT = "yes" ]]; then
        	                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
				else
			                if [[ `get_attr_value "client" "$ARGUMENT" "type"` != "spot" ]]; then
			                        /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_NOT_COSI} \
			                                '%1$s: \"%2$s\" is not a common OS image.\n' "${PROGNAME}" "${ARGUMENT}"
		                        	return 1
                			fi
        	                fi
                	fi

	                for image in $ARGUMENT
        	        do
                	        if [[ $IS_MASTER = "yes" ]]; then
                        	        if [[ $(obj_exist "master" $image; 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}" "${image}"
						return 1
	                                fi
        	                elif [[ $IS_CLIENT = "yes" ]]; then
                	                if [[ $(obj_exist "client" $image; 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}" "${image}"
                                	        return 1
	                                fi
        	                fi

	                        [[ "${LEVEL_ONE}"   = "yes" ]] && display_level_one $image
	                        [[ "${LEVEL_TWO}"   = "yes" ]] && display_level_two $image
				[[ "${LEVEL_THREE}" = "yes" ]] && display_level_three $image
	                done
	        fi
	fi

	return 0
}

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

		l)		# specify the level of detail information to report
				level=${level}${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

ck_nim_env_ts || exit 1
ck_attr	      || exit 1

shift $((OPTIND - 1))

ARGUMENT=$*

list_cosi
rc=$?

exit $rc

# end of script