#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72D src/bos/usr/lib/nim/methods/c_updateios.sh 1.6 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2011,2015 
# 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 
# @(#)20        1.6  src/bos/usr/lib/nim/methods/c_updateios.sh, cmdnim, bos72D, d2015_37A0  8/21/15  11:31:32
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/c_backupios.sh
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 2011
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

# include common NIM shell defines/functions
NIMPATH=${0%/*}
NIMPATH=${NIMPATH%/*}
[[ ${NIMPATH} = ${0} ]] && NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
. ${NIM_METHODS}/c_sh_lib

#---------------------------- local defines     --------------------------------

#---------------------------- module globals    --------------------------------
REQUIRED_ATTRS="updateios_flags"
OPTIONAL_ATTRS="lpp_source updateios_flags filesets installp_bundle preview accept_licenses force"
force=""
lpp_source=""
updateios_flags=""
filesets=""
installp_bundle=""
preview="yes"
accept_licenses=""

#----------------------------- process_bundle ----------------------------
#
# NAME: process_bundle
#
# FUNCTION:
#               process_bundle file for updateios remove
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#               parameters:
#               global:
#
# RETURNS: (int)
#               0                                                       = no errors
#               >0                                                      = failure
#
# OUTPUT:
#-----------------------------------------------------------------------------*/
function process_bundle {

	typeset bundle_location=$1
	typeset bundle_process=$2

	$CAT $bundle_location | $GREP -v "^#" | $CUT -d':' -f 2 >$bundle_process

	return 0
}

#----------------------------- c_updateios ----------------------------
#
# NAME: c_updateios
#
# FUNCTION:
#		sets up remote access to resources, then calls updateios
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#		0							= no errors
#		>0							= failure
#
# OUTPUT:
#-----------------------------------------------------------------------------*/
function c_updateios {

	# mount the lpp_source resource
	if [[ -n $lpp_source ]]; then
		nim_mount ${lpp_source}
		lpp_access=${access_pnt}
	fi

	if [[ -n $installp_bundle ]]; then
		nim_mount ${installp_bundle}
		bundle_access=${access_pnt}
	fi

	case ${updateios_flags} in
		-cleanup)
			command="/usr/ios/cli/ioscli updateios -cleanup"
			;;
		-commit)
			command="/usr/ios/cli/ioscli updateios -commit"
			;;

		-install)
			argument="-install -dev $lpp_access ${force:+-f} ${accept_licenses:+-accept}"

			response_file="/home/padmin/._nim_updateios_response"
			if [[ $preview = "no" ]]; then
				if [[ -s $response_file ]]; then
					response=`$CAT $response_file`
				fi

				if [[ -z $response ]]; then
					response="y\ny"
				fi

				command="eval echo '$response' | /usr/ios/cli/ioscli updateios $argument"
			else
				command="eval echo 'n' | /usr/ios/cli/ioscli updateios $argument"
			fi
			;;

		-reject)
			command="/usr/ios/cli/ioscli updateios -reject ${force:+-f}"
			;;
		-remove)
			if [[ -n $filesets ]]; then
				argument="$filesets"
			fi

			if [[ -n $bundle_access ]]; then
				typeset temp_file=/tmp/nim_updatios_bundle.$$
				process_bundle $bundle_access $temp_file
				argument="-file $temp_file"
			fi

			command="/usr/ios/cli/ioscli updateios -remove $argument"
			;;
	esac

	$command

	# return code from updateios
	RC=$?

	if [[ -s $temp_file ]]; then
		$RM $temp_file
	fi

	# unmount the lpp_source resource
	if [[ -n $lpp_access ]]; then
		nim_unmount ${lpp_access}
	fi

	# unmount the installp_bundle resource
	if [[ -n $bundle_access ]]; then
		nim_unmount ${bundle_access}
	fi

	return $RC

} # end c_updateios


#-------------------------------  MAIN  ---------------------------------------
# signal processing
trap cleanup 0
trap err_signal 1 2 11 15

# NIM initialization
nim_init

# initialize local variables
typeset c=""

# set parameters from command line
while getopts :a:qv c; do
	case ${c} in

		a)	# validate the attr ass
			parse_attr_ass "${OPTARG}"

			# include the assignment for use in this environment
			eval ${variable}=\"${value}\"
			;;

		q)	# show attr info
			cmd_what
			exit 0
			;;

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

		\?)	# unknown option
			# 0042-016 <method>: "-<OPTARG>" is not a valid option for this
			# operation
			#	<>
			error ${ERR_BAD_OPT} ${OPTARG}
			;;

	esac
done

# check for missing attrs
ck_attrs

c_updateios

exit $?
