#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/lsts.sh 1.3.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 
# @(#)50    1.3.1.2  src/bos/usr/lib/nim/methods/lsts.sh, cmdnim, bos720  7/15/11  02:24:35
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/lsts.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_LSTS_USAGE} \
		'%1$s [[-l{1|2|3}] . . .] [-v] [Thinserver]\n\
1\tSummary listing.\n\
2\tDisplay thin server software content.\n\
3\tDisplay thin server installation log, error report.\n' "${PROGNAME}"

        return 0
}

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

	typeset ts=$1

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

	return 0
}

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

	typeset ts=$1
	typeset cosi=""

	if [[ -z $ts ]]; then
		return 1
	else
		echo $ts
		if [[ $IS_MASTER = "yes" ]]; then
			cosi=`get_attr_value "master" $ts "spot"`
                        if [[ -n $cosi ]]; then
				/usr/sbin/nim -o lslpp -a lslpp_flags=L $cosi
			else
				return 0
			fi
		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 ts=$1
	typeset cosi=""

	if [[ -z $ts ]]; then
		return 1
	else
		echo $ts
		if [[ $IS_MASTER = "yes" ]]; then
			cosi=`get_attr_value "master" $ts "spot"`
			if [[ -n $cosi ]]; 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
			else
				return 0
			fi
		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_ts
#
# FUNCTION:     List thinsever information.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - successful
#               1 - unsuccessful
# --------------------------------------------------------------------------- #
function list_ts {

	if [[ -z $ARGUMENT ]] && [[ $IS_MASTER = "yes" ]]; then
	        /usr/sbin/lsnim -c machines | /usr/bin/grep -v standalone | \
			/usr/bin/grep -v alternate_master | /usr/bin/awk '{print $1}' | \
			/usr/bin/grep -v master | /usr/bin/sort
	else
	        if [[ -z $ARGUMENT ]]; then
	                if [[ $IS_MASTER = "yes" ]]; then
        	                thinservers=`/usr/sbin/lsnim -c machines | \
					/usr/bin/grep -v standalone | \
					/usr/bin/awk '{print $1}' | \
					/usr/bin/grep -v master | /usr/bin/sort`
                	elif [[ $IS_CLIENT = "yes" ]]; then
	                        thinservers=$NIM_NAME
        	        fi

	                for ts in $thinservers
        	        do
                	        [[ "${LEVEL_ONE}"   = "yes" ]] && display_level_one   $ts
	                        [[ "${LEVEL_TWO}"   = "yes" ]] && display_level_two   $ts
	                        [[ "${LEVEL_THREE}" = "yes" ]] && display_level_three $ts
	                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_TS_PER_OP} \
						'%1$s: Only one thinserver allowed per operation.\n' "${PROGNAME}"
                                	return 1
	                        fi
        	        fi

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

					machine_type=`get_attr_value "master" $ts "type"`
        	                elif [[ $IS_CLIENT = "yes" ]]; then
                	                if [[ $(obj_exist "client" $ts; 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}" "${ts}"
                                	        return 1
	                                fi

					machine_type=`get_attr_value "client" $ts "type"`
        	                fi

				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}" "${ts}"
					echo
					return 1
				fi

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

	return 0
}

# ----------------------------------- lsts ---------------------------------- #
#
# NAME: lsts
#
# FUNCTION:	 /usr/sbin/lsts 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_ts
rc=$?
exit $rc

# end of script