#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72L src/bos/usr/lib/nim/methods/installios.sh 1.23.1.8 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005,2017 
# 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 

NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
export NIMPATH NIM_METHODS
. ${NIM_METHODS}/c_sh_lib

# Set PROGRAM variable for error reporting.
PROGRAM="installios"

# --------------------------------------------------------------------------- #
# GLOBAL VARIABLES
# --------------------------------------------------------------------------- #

# script argument variables
accept_license=""
config_net=""
cleanup=""
client=""
debug=""
duplex=""
force=""
gateway=""
hmc=""
locale=""
location=""
mac_addr=""
managed=""
mask=""
partition=""
profile=""
source=""
speed=""
verbose=""
unconfig=""
wizard=""
vlan_tag=""
vlan_pri=""

BOOT_CLIENT="no"                                # set in ck_ipaddr for client
FILESYSTEM="/export/installios"
INSTALLIOS_LOG="/var/adm/ras/installios.log"
TFTPBOOT="/tftpboot"
CD_MNT_DIR="/tmp/picaso_mnt_pt"
SSH_ENV=0
SOURCE_AIXBASE=""                               # set in ck_source - test AIX Base Media/VIO DVD

CDROMD="no"                                     # set in ck_source to test cdromd running
CDROMD_MOUNTED=""                               # set in ck_source for cdromd
MOUNTED=""                                      # set in ck_source for mount command
MKDIR_CD_MNT=""                                 # set in ck_source
MKSYSB_SPOT_SIZE=350000				# size in KB, used in define_spot


# CODE for cleanup
CODE=""                                         # set in define_client
CODE_NIMCONFIG=1
CODE_INSTALLNIM=2
CODE_MKDIR_BUNDLE=4
CODE_MKDIR_MKSYSB=8
CODE_MKDIR_LPPSOURCE=16
CODE_MKDIR_SPOT=32
CODE_DEFINE_SPOT=64
CODE_DEFINE_CLIENT=128
CODE_DEFINE_BUNDLE=256
CODE_DEFINE_MKSYSB=512
CODE_DEFINE_BOSINST=1024
CODE_DEFINE_LPPSOURCE=2048
CODE_DEFINE_RESOLV=4096
CODE_DEFINE_SCRIPT_LIC=8192
CODE_COPY_MKSYSB=16384
CODE_COPY_BUNDLE=32768
CODE_COPY_BOSINST=65536
CODE_COPY_LPPSOURCE=131072
CODE_MKFS_TFTPBOOT=262144
CODE_MKFS_NIMINSTALL=524288
CODE_COPY_RESOLV=1048576
CODE_CREATE_BOSINST=2097152
CODE_CREATE_SCRIPT_LIC=4194304
CODE_MKDIR_VIO_IMAGE=8388608

NIMCONFIG=""                                   # set in nim_config
INSTALLNIM=""                                  # set in nim_configconfig
MKDIR_BUNDLE=""                                # set in ck_target
MKDIR_MKSYSB=""                                # set in ck_target
MKDIR_LPPSOURCE=""                             # set in ck_target
MKDIR_SPOT=""                                  # set in ck_target
MKDIR_VIO_IMAGE=""
DEFINE_CLIENT=""
DEFINE_BOSINST=""
DEFINE_BUNDLE=""
DEFINE_MKSYSB=""
DEFINE_LPPSOURCE=""
DEFINE_RESOLV=""
DEFINE_SCRIPT_LIC=""
DEFINE_SPOT=""
COPY_BOSINST=""
COPY_MKSYSB=""
COPY_LPPSOURCE=""
COPY_BUNDLE=""
COPY_RESOLV=""
CREATE_BOSINST=""
CREATE_SCRIPT_LIC=""
MKFS_TFTPBOOT=""
MKFS_NIMINSTALL=""

# CLIENT variables				# for defining objects in NIM
CLIENT="${PROGRAM}_client"
CLIENT_BOSINST=${PROGRAM}_bosinst
CLIENT_BUNDLE=${PROGRAM}_bundle
CLIENT_LPPSOURCE=${PROGRAM}_lpp_source
CLIENT_MKSYSB=${PROGRAM}_mksysb
CLIENT_RESOLV=${PROGRAM}_resolv
CLIENT_SCRIPT_LIC=${PROGRAM}_script_lic
CLIENT_SPOT=${PROGRAM}_spot

# variables - set in ck_source
SOURCE=""
SOURCE_BOOTIMAGE=""
SOURCE_BOSINST=""
SOURCE_LOCALE=""
SOURCE_MKSYSB=""
SOURCE_SPOT=""

# variables - set in ck_target
TARGET=""
TARGET_BOSINST=""
TARGET_BUNDLE=""
TARGET_DIR_BUNDLE=""
TARGET_DIR_LPPSOURCE=""
TARGET_DIR_MKSYSB=""
TARGET_DIR_SPOT=""
TARGET_DIR_VIO_IMAGE=""
TARGET_LPPSOURCE=""
TARGET_MKSYSB=""
TARGET_RESOLV=""
TARGET_SCRIPT_LIC=""

INDEX_MKSYSB=""
SOURCE_MKSYSB=""
INDEX_SPOT=""
SOURCE_SPOT=""
TARGET_SPOT=""
INDEX_BOSINST=""
SOURCE_BOSINST=""
SOURCE_CDROM=""

#
# MESSAGES initialization in case MSG tag isn't translated.
# 794166 - (61Q,71D) can be remove once message catalog cmdnim.cat ships message tag
#
[[ -z $MSG_PROCESS_MULTI_VOLUME     ]] && MSG_PROCESS_MULTI_VOLUME=99999
[[ -z $MSG_MOUNT_VOLUME_TO_CONTINUE ]] && MSG_MOUNT_VOLUME_TO_CONTINUE=99999
[[ -z $MSG_PRESS_C_TO_CONTINUE      ]] && MSG_PRESS_C_TO_CONTINUE=99999
[[ -z $MSG_INVALID_MEDIA            ]] && MSG_INVALID_MEDIA=99999

# --------------------------------------------------------------------------- #
# NAME:         ck_program
#
# FUNCTION:     Checks if the program has been executed.  This program is only
#		allow to be executed once.  A cleanup must be perform before
#		the program can be permitted to executed again.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - program was not executed
#               3 - program was executed, cleanup required.
# --------------------------------------------------------------------------- #
function ck_program {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running ck_program function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	#
	# Call decipher to obtain CODE - indicate if program has been executed.
	#
	decipher
	if [[ ${DEFINE_CLIENT} = "yes" ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_SETUP_ALREADY} \
			'This script can only be executed once to setup a machine for node install.\n\
Please perform a cleanup before running this script again.\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 3
	fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         ck_ssh_env
#
# FUNCTION:     Checks for existing SSH environment
#
# PARAMETERS:   None.
#
# RETURNS:      0 - nim environment exist
#               1 - unable to determine environment
# --------------------------------------------------------------------------- #
function ck_ssh_env {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running ck_ssh_env function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	if [[ -s /usr/bin/ssh ]]; then
		SSH_ENV=1
	else
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_SSH_CREDENTIAL} \
			'SSH and security credential is required to perform this type of installation.\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
        fi

	return $SSH_ENV
}

# --------------------------------------------------------------------------- #
# NAME: 	ck_nim_env
#
# FUNCTION: 	Checks for existing nim environment
#
# PARAMETERS:   None.
#
# RETURNS:      0 - nim environment exist
#               1 - unable to determine environment
# --------------------------------------------------------------------------- #
function ck_nim_env {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running ck_nim_env function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	/usr/bin/lslpp -l bos.sysmgt.nim.master >/dev/null 2>&1

	#
	# If the NIM master fileset is not installed, then prompt for the 
	# location of bos.sysmgt.nim.master fileset and configure NIM if
	# cleanup is not being perform.
	#
	if [[ $? -ne 0 ]]; then
		if [[ $cleanup -ne 1 ]]; then
			install_nim || return 1
			nim_config || return 1
		else
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_NO_MASTER_FILESET} \
				'Machine does not have NIM master fileset installed.\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
	else		
		#
		# Fileset exist, so determine if NIM is configure
		#
		ck_init || return 1
	fi
	
	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	install_nim
#
# FUNCTION: 	Install the NIM master fileset
#
# PARAMETERS:   None.
#
# RETURNS:      0 - master fileset installed successful
#               1 - unable to install master fileset
# --------------------------------------------------------------------------- #
function install_nim {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running install_nim function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset response=""

	#
	# If the location flag is not passed in containing the master fileset, 
	# then prompt for the master fileset.
	#
	if [[ -z ${location} ]]; then
		while [[ "${response}" != "q" ]]; do
			/usr/bin/echo ""
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_MASTER_FILESET} \
				'Enter the location to the bos.sysmgt.nim.master fileset or 'q' to quit.\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			read response?'=> '
			/usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
			if [[ "${response}" = "q" ]]; then
				/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_QUIT_INSTALL} \
					'Quiting before initiating install.' \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
				return 1
			else
				#
				# If we get a response, see if the bos.sysmgt.nim.master fileset
				# is present.  Break if it is.
				#
				if [[ "${response}" = cd[0-9]* || "${response}" = /dev/cd[0-9]* ]]; then
					[[ "${response}" = /dev/cd[0-9]* ]] && response=`/usr/bin/basename "${response}"`
					if [[ -n $( /usr/sbin/lsdev -Cl ${response} 2> /dev/null ) ]]; then
						response="/dev/${response}" 
						ck_cdrom "${response}" || return 1 
						/usr/sbin/installp -l -d ${response} | \
							/usr/bin/grep bos.sysmgt.nim.master >/dev/null 2>&1 && break
						unmount_source
					fi
				elif [[ -d ${response} ]]; then
					/usr/sbin/installp -l -d ${response} | \
						/usr/bin/grep bos.sysmgt.nim.master >/dev/null 2>&1 && break
				fi
			fi
		done
	else
		#
		# We are passed in a location to the master fileset.
		#
		if [[ "${location}" = cd[0-9]* || "${location}" = /dev/cd[0-9]* ]]; then
			[[ "${location}" = /dev/cd[0-9]* ]] && location=`/usr/bin/basename "${location}"`
			[[ -n $( /usr/sbin/lsdev -Cl ${location} 2> /dev/null ) ]] && location="/dev/${location}"
		elif [[ -d $location ]]; then
			response=${location}
		else
			response=${location##*/}
			response=${location%%/$response}
		fi
	fi

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_INSTALLING_FILESETS} 'Installing filesets ...\n\n' \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}
	/usr/sbin/installp -acgqXd ${response} bos.sysmgt.nim.master >/dev/null 2>&1
	if [[ ! -e /usr/sbin/nimconfig ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ERR_INSTALL_MASTER_FILESET} \
			'Error installing NIM master fileset - Exiting.\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi

	INSTALLNIM="yes"

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	ck_init
#
# FUNCTION:     Checks if the NIM master is configured.
#
# PARAMETERS: 	None.
#
# RETURNS:  	0 - if the NIM master is configured.
#	    	1 - if the NIM master is not configured.
# --------------------------------------------------------------------------- #
function ck_init {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running ck_init function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	ODMDIR=/etc/objrepos /usr/bin/odmget -q "name=master" nim_object 2>/dev/null |
	        /usr/bin/grep master > /dev/null

	if [[ $? -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_MASTER_NOT_CONFIGURED} \
			'NIM master not configured.\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		if [[ -z $cleanup ]]; then
			nim_config || return 1
		else
			return 1
		fi
	fi
	
	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	nim_config
#
# FUNCTION: 	Configure the nim environment
#
# PARAMETERS:   None.
#
# RETURNS:      0 - nim environment setup successfully 
#               1 - unable to setup the nim environment
# --------------------------------------------------------------------------- #
function nim_config {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running nim_config function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset hostname=""
	typeset platform=""
	typeset kernel=""
	typeset default_route=""
	typeset pif=""
	typeset ring_speed=""
	typeset cable_type=""
	typeset type=""

	[[ $verbose = 1 ]] && /usr/bin/echo "Configuring machine as a NIM master" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	hostname=`/usr/bin/hostname -s`
	platform=`/usr/sbin/bootinfo -p`
	kernel="64"

	# interfaces may have similar strings in their address
	pif=`/usr/bin/netstat -if inet | /usr/bin/awk -v cmp=${hostname} '{
		split($4, ar, ".")
		if ( cmp == ar[1] ) {
			print $1
		}
	} '`

	if [[ ${pif} = tr* ]]; then
		ring_speed=`/usr/sbin/mktcpip -S ${pif} 2>&1 | \
			/usr/bin/awk 'BEGIN { RS="\n"; FS=":" } \
			{ for (i=1; i<=NF; i++) \
			{ if ( match($i, /speed/) ) (j=i) } \
			if (NR==2) {print $j} }'`
			type="-a ring_speed1=${ring_speed}";

	elif [[ ${pif} = e[nt]* ]]; then
		cable_type=`/usr/sbin/mktcpip -S ${pif} 2>&1 | \
			/usr/bin/awk 'BEGIN { RS="\n"; FS=":" } \
			{ for (i=1; i<=NF; i++) \
			{ if ( match($i, /type/) ) (j=i) } \
			if (NR==2) {print $j} }'`
			type="-a cable_type1=${cable_type}";

	else
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ERR_DETERMINE_NET} \
			'Error determining network type.\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_SETUP_NET_SUPPORT} \
			'Easy setup only supports ethernet or token-ring networks - Exiting.\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi

	# define the NIM master
	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DEFINE_MASTER} \
		'Defining NIM master...' | /usr/bin/tee -a ${INSTALLIOS_LOG}
	/usr/sbin/nimconfig -a netname=master_net -a pif_name=${pif} ${type} \
		-a platform=${platform} -a netboot_kernel=${kernel} >/dev/null 2>&1

	if [[ $? -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_CONFIGURE_NIM} \
			'%1$s: Could not congfigure NIM.\n' "${PROGRAM}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi
	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' | /usr/bin/tee -a ${INSTALLIOS_LOG}
	NIMCONFIG="yes"

	mk_fs rootvg ${FILESYSTEM} 1850000 
	if [[ $? -eq 0 ]]; then
		MKFS_NIMINSTALL="yes"
	elif [[ $? -eq 1 ]]; then
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_CREATE_FILESYSTEM} \
			'%1$s: Unable to create filesystem %2$s.\n' "${PROGRAM}" "${FILESYSTEM}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi

	/usr/sbin/lsfs ${TFTPBOOT} >/dev/null 2>&1
	if [[ $? -ne 0 ]]; then
		mk_fs rootvg ${TFTPBOOT} 65536
		if [[ $? -eq 0 ]]; then
			MKFS_TFTPBOOT="yes"
		else
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_CREATE_FILESYSTEM} \
				'%1$s: Unable to create filesystem %2$s.\n' "${PROGRAM}" "${TFTPBOOT}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
	fi

	# add default route to master's network
	default_route=`/usr/sbin/mktcpip -S ${pif} 2>&1 | /usr/bin/awk -F":" ' { if (NR == 2) {print $7} } '`
	/usr/sbin/lsnim -a routing1 master_net 2>&1 | /usr/bin/grep default >/dev/null
	if [[ $? -eq 1 ]]; then
		/usr/sbin/nim -o change -a routing1="default ${default_route}" master_net
	fi

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CONFIG_COMPLETE} 'Configuration is complete.\n' \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         mk_fs
#
# FUNCTION:     Create filesystem 
#
# PARAMETERS:   volume_group - volume group to contain the filesystem
#		filesystem   - name for the filesystem
#		size         - size of the filesystem
#
# RETURNS:      0 - able to create filesystem
#               1 - unable to create filesystem
#		2 - filesystem already exist
# --------------------------------------------------------------------------- #
function mk_fs {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running mk_fs function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}
	
	typeset volume_group=$1
	typeset filesystem=$2
	typeset size=$3
	typeset fs_type="jfs2"

	/usr/sbin/lsfs | /usr/bin/grep "${filesystem}" >/dev/null 2>&1
	if [[ $? -eq 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FS_EXISTS} \
			'The %s filesystem already exists.  It must be removed\n\
before a new filesystem can be created with the same\n\
mount point.\n' "${filesystem}" | /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 2
	else
		[[ -z $wizard ]] && /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CREATE_FS} \
			'Creating %s filesystem...' "${filesystem}" | /usr/bin/tee -a ${INSTALLIOS_LOG}
	
		/usr/sbin/crfs -v ${fs_type} -g ${volume_group} -a size=${size} -m ${filesystem} \
			-A yes -p rw -t no -a agblksize=4096 >/dev/null 2>&1
		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FS_CREATE_FAILED} \
				'Creation of the %s filesystem failed.\n' ${filesystem} \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi

		/usr/sbin/mount ${filesystem} >/dev/null 2>&1
		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ERR_MOUNT_FS} \
				'Error mounting %s filesystem.\n' ${filesystem} \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
		[[ -z $wizard ]] && /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	check_attrs
#
# FUNCTION: 	Checks for valid attributes and command line syntax
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
#               3 - failure - installios invoked, require cleanup before continuing
#		4 - failure - don't invoke undo function
# --------------------------------------------------------------------------- #
function check_attrs {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running check_attrs function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	if [[ -n $unconfig || -n $force ]] && [[ -z $cleanup ]]; then
		usage
		return 4
	fi

	if [[ -z ${client} || -z ${gateway} || -z ${mask} ]] ||
	   [[ -z ${source} ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_REQUIRED_ATTR} \
			'the following attributes are required:' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/echo "\nclient, gateway, mask, source\n" | /usr/bin/tee -a ${INSTALLIOS_LOG}
		usage
		return 4
	fi

	if [[ -n ${hmc} ]] && 
	   [[ -z ${managed} || -z ${partition} || -z ${profile} ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_INSTALLIOS_H_FLAG} \
			'The -h flag requires the -p -r and -s flags.\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		usage
		return 4
	fi

	# Check for valid source
	if [[ -n $hmc ]]; then
		ck_ssh_env                                && return 4
		ck_hmc_lpm "hmc" "hmc"       "$hmc"       || return 4
		ck_hmc_lpm "hmc" "managed"   "$managed"   || return 4
		ck_hmc_lpm "hmc" "partition" "$partition" || return 4
		ck_hmc_lpm "hmc" "profile"   "$profile"   || return 4
	elif [[ -n $managed ]]; then
		ck_hmc_lpm "lpm" "managed"   "$managed"   || return 4
		ck_hmc_lpm "lpm" "partition" "$partition" || return 4
		ck_hmc_lpm "lpm" "profile"   "$profile"   || return 4
	fi 

	if [[ -n $vlan_tag ]]; then
		if [[ $vlan_tag = "0" ]]; then
                        vlan_tag=""
                else
			if [[ $vlan_tag -lt 0 ]] || [[ $vlan_tag -gt 4094 ]]; then	
				/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_BAD_ARG} \
					'Bad argument assignment : %s\n' ${vlan_tag} \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
				return 1
			fi
		fi
	fi

	if [[ -n $vlan_pri ]]; then
		if [[ $vlan_pri = "0" ]]; then
			vlan_pri=""
		else
			if [[ $vlan_pri -lt 0 ]] || [[ $vlan_pri -gt 7 ]]; then
				/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_BAD_ARG} \
					'Bad argument assignment : %s\n' ${vlan_pri} \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
				return 1
			fi
		fi
        fi

	ck_ipaddr "client"  ${client}                     || return 4
	ck_ipaddr "gateway" ${gateway}                    || return 4
	ck_ipaddr "mask"    ${mask}                       || return 4
	ck_speed                                          || return 4
	ck_duplex                                         || return 4
	ck_nim_env                                        || return 1
	ck_program                                        || return 3
	ck_source		                          || return 1
	ck_locale                                         || return 1
	ck_target                                         || return 1

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	ck_ipaddr
#
# FUNCTION: 	Checks for valid attributes
#
# PARAMETERS:   name    - type of ip address
#		ip_addr - ip address
#
# RETURNS:      0 - correct ip value
#               1 - incorrect ip value
# --------------------------------------------------------------------------- #
function ck_ipaddr {

	typeset name=$1
	typeset ip_addr=$2

	[[ $verbose = 1 ]] && /usr/bin/echo "Running ck_ipaddr function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	# Convert hostname to ip address
	if [[ "${name}" = "client" && "${ip_addr}" != *.*.*.* ]]; then
		client=`/usr/bin/host ${ip_addr} | /usr/bin/cut -f3 -d' ' | /usr/bin/sed 's/,//g'`
		if [[ $? -ne 0 ]]; then
			[[ -z $wizard ]] && /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_RESOLVE_HOST} \
				'%1$s: Unable to resolve host %2$s.\n' "${PROGRAM}" "${ip_addr}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
		fi 
		ip_addr=$client
	fi

	# determine the format
	if [[ "${ip_addr}" != *.*.*.* ]]; then
		[[ -z $wizard ]] && /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_WRONG_IP_FORMAT} \
			'%1$s: %2$s is in the wrong format: %3$s\n' "${PROGRAM}" "${name}" "${ip_addr}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi
	
	# Check for valid IP address
	for i in `/usr/bin/echo ${ip_addr} | /usr/bin/sed 's/\./ /g'`
	do
		if [[ $i -gt 255 ]] || [[ $i -lt 0 ]]; then
			[[ -z $wizard ]] && /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_RESOLVE_IP} \
				'%1$s: %2$s is in the wrong format: %3$s\n' "${PROGRAM}" "${name}" "${ip_addr}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
	fi
	done

	# Resolve the ip address to a hostname
	if [[ "${name}" = "client" ]] || [[ "${name}" = "gateway" ]]; then

		if [[ "${name}" = "client" ]]; then
			/usr/bin/host ${ip_addr} > /dev/null 2>&1
		elif [[ "${name}" = "gateway" ]]; then
			/usr/sbin/ping -c 1 ${ip_addr} >/dev/null 2>&1
		fi

		if [[ $? -eq 1 ]]; then
			[[ -z $wizard ]] && /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_RESOLVE_IP} \
				'%1$s: Unable to resolve ip address.\n' "${PROGRAM}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi

		if [[ "${name}" = "client" ]]; then
			/usr/sbin/ping -c 1 ${ip_addr} >/dev/null 2>&1
			if [[ $? -eq 0 ]]; then
				BOOT_CLIENT=yes
			fi
		fi
	fi
		
	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	ck_speed
#
# FUNCTION:	Checks for valid speed value or set default
#
# PARAMETERS:   None.
#
# RETURNS:      0 - correct speed
#               1 - incorrect speed 
# --------------------------------------------------------------------------- #
function ck_speed {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running ck_speed function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	if [[ -n $speed ]]; then
		if [[ $speed != "10" ]] &&
		   [[ $speed != "100" ]] &&
		   [[ $speed != "1000" ]] &&
		   [[ $speed != "auto" ]]; then
			[[ -z $wizard ]] && /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_WRONG_SPEED} \
				'%1$s: Wrong speed setting.\n' "${PROGRAM}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
	else
		speed="100"
		if [[ $verbose = 1 ]]; then
			[[ $verbose = 1 ]] && /usr/bin/echo "Speed setting not set, using default" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			[[ $verbose = 1 ]] && /usr/bin/echo "speed = ${speed}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
		fi
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	ck_duplex
#
# FUNCTION: 	Checks for valid duplex value or set default
#
# PARAMETERS:   None.
#
# RETURNS:      0 - correct duplex value
#               1 - incorrect duplex value
# --------------------------------------------------------------------------- #
function ck_duplex {
	
	[[ $verbose = 1 ]] && /usr/bin/echo "Running ck_duplex function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	if [[ -n ${duplex} ]]; then
		if [[ "${duplex}" != "half" ]] &&
		   [[ "${duplex}" != "full" ]] &&
		   [[ "${duplex}" != "auto" ]]; then
			[[ -z $wizard ]] && /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_WRONG_DUPLEX} \
					'%1$s: Wrong duplex setting.\n' "${PROGRAM}" \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
	else
		duplex="full"
		if [[ $verbose = 1 ]]; then
			[[ $verbose = 1 ]] && /usr/bin/echo "Duplex setting not set, using default" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			[[ $verbose = 1 ]] && /usr/bin/echo "duplex = ${duplex}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
		fi
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	ck_locale
#
# FUNCTION: 	Checks for valid locale	or set default
#
# PARAMETERS:   None.
#
# RETURNS:      0 - valid locale
#               1 - invalid locale
# --------------------------------------------------------------------------- #
function ck_locale {
	
	[[ $verbose = 1 ]] && /usr/bin/echo "Running ck_locale function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset found=""

	if [[ $SOURCE_AIXBASE = "yes" ]]; then
		return 0
	fi

	if [[ -n ${locale} ]]; then
		#
		# Loop throught all bundle files to see if the locale 
		# being passed in matched.
		#
		for i in `/usr/bin/ls ${SOURCE_LOCALE} | /usr/bin/cut -f1 -d.`
		do
			#
			# Locale found
			#
			if [[ ${locale} = $i ]]; then
				found="yes"
				break
			fi
		done
		
		if [[ ${found} != "yes" ]]; then 
			[[ -z $wizard ]] && /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_WRONG_LANGUAGE} \
				'%1$s: Wrong language setting.\n' "${PROGRAM}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
	else
		locale="en_US"
		if [[ $verbose = 1 ]]; then
			[[ $verbose = 1 ]] && /usr/bin/echo "Locale setting not set, using default" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			[[ $verbose = 1 ]] && /usr/bin/echo "locale = ${locale}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
		fi
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	ck_target
#
# FUNCTION: 	Checks for target location and set TARGET variables for 
#		define functions.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function ck_target {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running ck_target function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	#  Check if /export/installios filesystem exist
	if [[ `/usr/sbin/lsfs ${FILESYSTEM} >/dev/null 2>&1; echo $?` -ne 0 ]]; then
		mk_fs rootvg ${FILESYSTEM} 1949120
		if [[ $? -eq 0 ]]; then
			MKFS_NIMINSTALL="yes"
		elif [[ $? -eq 1 ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_CREATE_FILESYSTEM} \
				'%1$s: Unable to create filesystem %2$s.\n' "${PROGRAM}" "${FILESYSTEM}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
	#  Check to see if it is mounted
	elif [[ `/usr/sbin/mount | /usr/bin/grep " ${FILESYSTEM} " >/dev/null 2>&1; echo $?` -ne 0 ]]; then
		if [[ `/usr/sbin/mount ${FILESYSTEM} >/dev/null 2>&1; echo $?` -ne 0 ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_TO_MOUNT} \
				'%1$s: Unable to mount %2$s.\n' "${PROGRAM}" "${FILESYSTEM}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
	fi

	TARGET=${FILESYSTEM}
	TARGET_MKSYSB=${TARGET}/mksysb/${PROGRAM}_mksysb
	TARGET_DIR_LPPSOURCE=${TARGET}/lpp_source
	TARGET_DIR_MKSYSB=${TARGET}/mksysb
	TARGET_DIR_SPOT=${TARGET}/spot
	TARGET_DIR_BUNDLE=${TARGET}/bundle
	TARGET_LPPSOURCE=${TARGET_DIR_LPPSOURCE}/${CLIENT_LPPSOURCE}
	TARGET_BUNDLE=${TARGET_DIR_BUNDLE}/${locale}.bnd
	TARGET_BOSINST=${TARGET}/${PROGRAM}_bosinst.data
	TARGET_RESOLV=${TARGET}/${PROGRAM}_resolv.conf
	TARGET_SCRIPT_LIC=${TARGET}/${PROGRAM}_script_lic
	TARGET_DIR_VIO_IMAGE="${FILESYSTEM}/VIO_IMAGE"


	if [[ ! -d ${TARGET_DIR_MKSYSB} ]] && [[ $cleanup -ne 1 ]] && [[ -z $SOURCE_AIXBASE ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_C_CREATING_DIR} \
			'Creating the %s directory.\n' "${TARGET_DIR_MKSYSB}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/mkdir ${TARGET_DIR_MKSYSB} 
		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CREATE_DIR} \
				'%1$s: Unable to create %2$s directory.\n' "${PROGRAM}" "${TARGET_DIR_MKSYSB}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi

		MKDIR_MKSYSB="yes"
	fi

	if [[ ! -d ${TARGET_DIR_SPOT} ]] && [[ $cleanup -ne 1 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_C_CREATING_DIR} \
			'Creating the %s directory.\n' "${TARGET_DIR_SPOT}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/mkdir ${TARGET_DIR_SPOT}
		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CREATE_DIR} \
				'%1$s: Unable to create %2$s directory.\n' "${PROGRAM}" "${TARGET_DIR_SPOT}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
		MKDIR_SPOT="yes"
	fi

	if [[ ! -d ${TARGET_DIR_BUNDLE} ]] && [[ $cleanup -ne 1 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_C_CREATING_DIR} \
			'Creating the %s directory.\n' "${TARGET_DIR_BUNDLE}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/mkdir ${TARGET_DIR_BUNDLE}
		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CREATE_DIR} \
				'%1$s: Unable to create %2$s directory.\n' "${PROGRAM}" "${TARGET_DIR_BUNDLE}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
		MKDIR_BUNDLE="yes"
	fi

	if [[ ! -d ${TARGET_DIR_LPPSOURCE} ]] && [[ $cleanup -ne 1 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_C_CREATING_DIR} \
			'Creating the %s directory.\n' "${TARGET_DIR_LPPSOURCE}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/mkdir ${TARGET_DIR_LPPSOURCE}
		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CREATE_DIR} \
				'%1$s: Unable to create %2$s directory.\n' "${PROGRAM}" "${TARGET_DIR_LPPSOURCE}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
		MKDIR_LPPSOURCE="yes"
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	usage
#
# FUNCTION: 	Print usage 
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
# --------------------------------------------------------------------------- #
function usage {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running usage function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_USAGE_INSTALLIOS} \
'Usage: %1$s [-h hmc -s managed_sys -p partition -r profile ] \
        -S netmask -i client_addr -g gateway -d source_dir \
        [-P speed] [-D duplex] [-V vlan_tag] [-Y vlan_priority] \
        [-n] [-N] [-l language] [-L location] | -u[f|U]\n' \
	"${PROGRAM}" | /usr/bin/tee -a ${INSTALLIOS_LOG}

	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         ck_cdrom
#
# FUNCTION:     Checks cdrom and determine if it is control by cdromd. Mount
#		the cdrom and set the MOUNTED or CDROMD_MOUNTED flags.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - valid device with valid resources
#               1 - invalid device or invalid resources
# --------------------------------------------------------------------------- #
function ck_cdrom {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running ck_cdrom function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset cdrom=$1

	SOURCE_CDROM=$cdrom

	#
	# check if device is managed by cdromd
	#
	/usr/sbin/cdcheck -aq ${cdrom} 2>/dev/null 1>&2
	if [[ $? -eq 0 ]]; then

		#
		# check if device is mounted
		#
		/usr/sbin/cdcheck -m -q ${cdrom} 2>/dev/null 1>&2
		if [[ $? -ne 0 ]]; then
			/usr/sbin/cdmount -m ${cdrom}
			CDROMD_MOUNTED="yes"
		fi

		#
		# get mount point
		#
		CD_MNT_DIR=`/usr/sbin/cdcheck -m -q ${cdrom} 2>/dev/null`
		CDROMD="yes"
	else
		# mount the DVD-ROM
		if [[ ! -d "${CD_MNT_DIR}" ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_C_CREATING_DIR} \
				'Creating the %s directory.\n' "${CD_MNT_DIR}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/bin/mkdir ${CD_MNT_DIR}
			if [[ $? -ne 0 ]]; then
				/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${ERR_CREATE_DIR} \
					'%1$s: Unable to create %2$s directory.\n' \
					"${PROGRAM}" "${CD_MNT_DIR}" \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
				return 1
			fi
			MKDIR_CD_MNT="yes"
		fi

		/usr/sbin/mount -rv cdrfs ${cdrom} ${CD_MNT_DIR} 2>&1
		if [[ $? -eq 0 ]]; then
			MOUNTED="yes"
		else
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_TO_MOUNT} \
				'%1$s: Unable to mount %2$s.\n' "${PROGRAM}" "${CD_MNT_DIR}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	ck_source
#
# FUNCTION: 	Checks for valid device and resources, mount the device if
#		necessary.  Also, set SOURCE variables for define functions.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - valid device with valid resources
#               1 - invalid device or invalid resources
# --------------------------------------------------------------------------- #
function ck_source {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running ck_source function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset devicetype=""

	if [[ "${source}" = cd[0-9]* || "${source}" = /dev/cd[0-9]* ]] || [[ "${source}" = usbms[0-9]* || "${source}" = /dev/usbms[0-9]* ]]; then
		[[ "${source}" = /dev/cd[0-9]* ]] && source=`/usr/bin/basename "${source}"`
		[[ "${source}" = /dev/usbms[0-9]* ]] && source=`/usr/bin/basename "${source}"`
		if [[ -n $( /usr/sbin/lsdev -Cl ${source} 2> /dev/null ) ]]; then
			source="/dev/${source}"
			
			devicetype="cdrom"
		else
			[[ -z $wizard ]] && /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DVD_ROM_REQUIRED} \
				'Device must be a dvd-rom.\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
	                return 1
		fi
		
	elif [[ "${source}" = ?*:?* ]]; then
		devicetype="remote"
	elif [[ -d "${source}" ]] || [[ -s "${source}" ]]; then
		devicetype="local"
	else 
		[[ -z $wizard ]] && /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_NOT_VALID_DEVICE} \
			'%1$s: %2$s is not a valid device.\n' "${PROGRAM}" "${source}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi

	if [[ "${devicetype}" = "cdrom" ]]; then
		ck_cdrom ${source} || return 1
	elif [[ "${devicetype}" = "remote" ]]; then
		if [[ ! -d "${CD_MNT_DIR}" ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_C_CREATING_DIR} \
				'Creating the %s directory.\n' "${CD_MNT_DIR}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/bin/mkdir ${CD_MNT_DIR}
			if [[ $? -ne 0 ]]; then
				/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${ERR_CREATE_DIR} \
					'%1$s: Unable to create %2$s directory.\n' \
					"${PROGRAM}" "${CD_MNT_DIR}" \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
				return 1
			fi
			MKDIR_CD_MNT="yes"
		fi

		/usr/sbin/mount $source ${CD_MNT_DIR} 2>&1
		if [[ $? -eq 0 ]]; then
			MOUNTED="yes"
		else
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_TO_MOUNT} \
				'%1$s: Unable to mount %2$s.\n' "${PROGRAM}" "${CD_MNT_DIR}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi

	elif [[ "${devicetype}" = "local" ]]; then
		SOURCE=${source}
		CD_MNT_DIR=$SOURCE
	fi

	if [[ -f "${CD_MNT_DIR}/installp/ppc/ios.cli" ]]; then
		SOURCE=${CD_MNT_DIR}
		SOURCE_VIOS_MIG="yes"
	elif [[ -d "${CD_MNT_DIR}/nimol/ioserver_res" ]]; then 
		SOURCE=${CD_MNT_DIR}
	
		SOURCE_NIMOL=${SOURCE}/nimol
		SOURCE_LICENSE=${SOURCE_NIMOL}/licenses
		SOURCE_IOSERVER_RES=${SOURCE_NIMOL}/ioserver_res
		SOURCE_MKSYSB=${SOURCE_IOSERVER_RES}/mksysb
		SOURCE_BOSINST=${SOURCE_IOSERVER_RES}/bosinst.data
		SOURCE_LOCALE=${SOURCE}/installp/ppc
	elif [[ -s "${CD_MNT_DIR}/installp/ppc/bos" ]]; then
		SOURCE=${CD_MNT_DIR}
		SOURCE_AIXBASE="yes"
        elif [[ -d "${CD_MNT_DIR}" ]]; then
		EXTRACT_IMAGE=yes
        elif [[ -s "${CD_MNT_DIR}" ]] && \
	     [[ `LANG=en_US /usr/bin/file ${CD_MNT_DIR} | \
		/usr/bin/grep -i "tar archive" >/dev/null 2>&1; echo $?` -eq 0 ]]; then
		EXTRACT_IMAGE=yes
	elif [[ -s "${CD_MNT_DIR}" ]] && \
  	     [[ `LANG=en_US /usr/bin/file ${CD_MNT_DIR} | \
                /usr/bin/grep -i "data or International Language text" >/dev/null 2>&1; echo $?` -eq 0 ]]; then
		SOURCE=$CD_MNT_DIR
		CD_MNT_DIR=/tmp/picaso_mnt_pt  # Define it again as mount point for loopmount
                SOURCE_NIMOL=${CD_MNT_DIR}/nimol
                SOURCE_LICENSE=${SOURCE_NIMOL}/licenses
                SOURCE_IOSERVER_RES=${SOURCE_NIMOL}/ioserver_res
                SOURCE_MKSYSB=${SOURCE_IOSERVER_RES}/mksysb
                SOURCE_BOSINST=${SOURCE_IOSERVER_RES}/bosinst.data
                SOURCE_LOCALE=${CD_MNT_DIR}/installp/ppc
                ISO_IMAGE=yes
	fi

	if [[ -z ${SOURCE_AIXBASE} && ! -s ${SOURCE_MKSYSB} && -z $EXTRACT_IMAGE && -z ${SOURCE_VIOS_MIG} && -z $ISO_IMAGE ]]; then
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_MKSYSB_REQUIRED} \
			'%1$s: A mksysb image is required.\n' "${PROGRAM}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi
	
	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         extract_image
#
# FUNCTION:     Extract VIO image from tar image
#
# PARAMETERS:   None.
#
# RETURNS:      1 - failure
# --------------------------------------------------------------------------- #
function extract_image {

        [[ $verbose = 1 ]] && /usr/bin/echo "Running extract_image function" \
                | /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset SET_VALUE=""
	typeset FREE_FS_SIZE=""
	typeset MKSYSB_SIZE=""
	typeset INCR_SIZE=""

	if [[ -d "${CD_MNT_DIR}" ]]; then
                /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_PROG_INIT_RES} \
                	' Initializing resources ...\n\n' | /usr/bin/tee -a ${INSTALLIOS_LOG}

                for file in $($LS $CD_MNT_DIR)
                do
                        /usr/bin/tar -tvf ${CD_MNT_DIR}/${file} ./index >/dev/null 2>/dev/null
                        if [[ $? = 0 ]]; then
                                CD_MNT_DIR=${CD_MNT_DIR}/${file}
				SET_VALUE=yes
                                break
                        fi
                done
		
		if [[ $SET_VALUE != "yes" ]]; then
	                /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_MKSYSB_REQUIRED} \
        	                '%1$s: A mksysb image is required.\n' "${PROGRAM}" \
                	        | /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
	fi

	MKSYSB_SIZE=`/usr/bin/du -sk $CD_MNT_DIR | /usr/bin/awk '{print $1}'`
	calc_fs ${FILESYSTEM} $MKSYSB_SIZE

        TARGET_DIR_VIO_IMAGE="${FILESYSTEM}/VIO_IMAGE"
	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_C_CREATING_DIR} \
		'Creating the %s directory.\n' "${TARGET_DIR_VIO_IMAGE}" \
                        | /usr/bin/tee -a ${INSTALLIOS_LOG}
	if [[ ! -d ${TARGET_DIR_VIO_IMAGE} ]]; then
		/usr/bin/mkdir ${TARGET_DIR_VIO_IMAGE}
		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CREATE_DIR} \
				'%1$s: Unable to create %2$s directory.\n' "${PROGRAM}" "${TARGET_DIR_VIO_IMAGE}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
	fi

	MKDIR_VIO_IMAGE=yes
	SOURCE_BOSINST=${TARGET_DIR_VIO_IMAGE}/bosinst.data
	cd ${TARGET_DIR_VIO_IMAGE}

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_START_MK_LPP} \
		'Preparing to copy install images (this will take several minutes)...\n' \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

        /usr/bin/tar -xf $CD_MNT_DIR

	for line in $( /usr/bin/cat ${TARGET_DIR_VIO_IMAGE}/index )
	do
		if [[ ${line%:*} = "mksysb" ]]; then
			INDEX_MKSYSB=${line#*/}
			SOURCE_MKSYSB=${TARGET_DIR_VIO_IMAGE}/${INDEX_MKSYSB}
		elif [[ ${line%:*} = "spot" ]]; then
			INDEX_SPOT=${line#*/}
                        SOURCE_SPOT=${TARGET_DIR_VIO_IMAGE}/${INDEX_SPOT}
			TARGET_SPOT=${FILESYSTEM}/spot/${CLIENT_SPOT}
		elif [[ ${line%:*} = "bosinst_data" ]]; then
			INDEX_BOSINST=${line#*/}
			SOURCE_BOSINST=${TARGET_DIR_VIO_IMAGE}/${INDEX_BOSINST}
		fi
	done

	if [[ -z $INDEX_MKSYSB ]]; then
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_MKSYSB_REQUIRED} \
			'%1$s: A mksysb image is required.\n' "${PROGRAM}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         iso_mount
#
# FUNCTION:     mount ISO image
#
# PARAMETERS:   None.
#
# RETURNS:      1 - failure
# --------------------------------------------------------------------------- #
function iso_mount {
	
	[[ $verbose = 1 ]] && /usr/bin/echo "Running iso_mount function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset source_image=$1

	/usr/sbin/loopmount -i $source_image -o "-V cdrfs -o ro" -m $CD_MNT_DIR
	if [[ $? -eq 0 ]]; then
		MOUNTED="yes"
	else
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_TO_MOUNT} \
			'%1$s: Unable to mount %2$s.\n' "${PROGRAM}" "${CD_MNT_DIR}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         prep_iso_image
#
# FUNCTION:     Prep the environment to handle the ISO image
#
# PARAMETERS:   None.
#
# RETURNS:      1 - failure
# --------------------------------------------------------------------------- #
function prep_iso_image {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running prep_iso_image function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	if [[ -s "${SOURCE}" ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_PROG_INIT_RES} \
			' Initializing resources ...\n\n' | /usr/bin/tee -a ${INSTALLIOS_LOG}

		if [[ ! -d "${CD_MNT_DIR}" ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_C_CREATING_DIR} \
				'Creating the %s directory.\n' "${CD_MNT_DIR}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/bin/mkdir ${CD_MNT_DIR}
			if [[ $? -ne 0 ]]; then
				/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${ERR_CREATE_DIR} \
					'%1$s: Unable to create %2$s directory.\n' \
					"${PROGRAM}" "${CD_MNT_DIR}" \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
				return 1
			fi
			MKDIR_CD_MNT="yes"
		fi

		iso_mount $SOURCE
		if [[ $rc -ne 0 ]]; then
			return 1
		fi
        fi

        /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_START_MK_LPP} \
                'Preparing to copy install images (this will take several minutes)...\n' \
                | /usr/bin/tee -a ${INSTALLIOS_LOG}

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         unmount_source
#
# FUNCTION:     Unmount the cdrom and remove mounted directory if it was created
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#		1 - failure
# --------------------------------------------------------------------------- #
function unmount_source {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running unmount_source function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}
	
	#
        # Unmount and remove picaso directory if we created it
	#
        if [[ "${CDROMD}" = "yes" ]] && [[ "${CDROMD_MOUNTED}" = "yes" ]]; then
                /usr/sbin/cdumount ${CD_MNT_DIR}
        elif [[ "${MOUNTED}" = "yes" ]]; then
                /usr/sbin/unmount ${CD_MNT_DIR}
		if [[ $? -eq 0 ]]; then
			MOUNTED="no"
		fi
        fi

	#
	# Remove picaso directory if we create it
	#
        if [[ "${MKDIR_CD_MNT}" = "yes" ]]; then
		rm_dir ${CD_MNT_DIR}
		if [[ $? -eq 0 ]]; then

			#
			# Set NULL for CODE leanup
			#
			MKDIR_CD_MNT=""
		fi
        fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	err_signal 
#
# FUNCTION:	Perform cleanup and exit during unexpected interruption 
#
# PARAMETERS:   None.
#
# RETURNS:      1 - failure
# --------------------------------------------------------------------------- #
function err_signal {

	[[ $verbose = 1 ]] && /usr/bin/echo "err_signal invoked" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	trap "" 2 11 15

	/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_PROGRAM_INTERRUPTED} \
		'%1$s: The program was interrupted unexpectedly.\n' "${PROGRAM}" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}
	cleanup=1
	cleanup

	exit 1
}

# --------------------------------------------------------------------------- #
# NAME:         cmd_err
#
# FUNCTION:     Print error, call undo and exit with error code
#
# PARAMETERS:   error_code = error code for program exit
#		error_msg = error string to be printed
#
# RETURNS:      error_code passed in
# --------------------------------------------------------------------------- #
function cmd_err {

	typeset error_code=$1
	typeset error_msg=$2

	[[ -n $error_msg ]] && err_from_cmd {$error_msg}
        undo
	
	exit $error_code
}

# --------------------------------------------------------------------------- #
# NAME:         calc_fs
#
# FUNCTION:     Calculate the filesystem size and increase size if necessary to
#		accomodate the file being copied in
#
# PARAMETERS:   filesystem = the filesystem that needs its size calculated
#               filesize = the size of the file going into the file system
#
# RETURNS:      0 - success
# --------------------------------------------------------------------------- #
function calc_fs {

	[[ $verbose = 1 ]] && /usr/bin/echo "Performing calc_fs" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset filesystem=$1
	typeset filesize=$2
	typeset FREE_FS_SIZE=`/usr/bin/df -kM ${filesytem} | /usr/bin/grep ${filesystem} | /usr/bin/awk '{print $4}'`

	if [[ $filesize -gt $FREE_FS_SIZE ]]; then
		INCR_SIZE=$(( $filesize - FREE_FS_SIZE ))
		let INCR_SIZE=`/usr/bin/expr $INCR_SIZE / 1000 + 150`
		/usr/sbin/chfs -a size=+${INCR_SIZE}M ${filesystem}
		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_EXP_FAILED} \
				'0042-187 %s: Failed to expand filesystem %s on %s' \
				"${PROGRAM}" "${filesystem}" `hostname`
			return 1
		fi
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	cipher
#
# FUNCTION:	Generate code for cleanup - Added to master comments
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
# --------------------------------------------------------------------------- #
function cipher {
	
	[[ $verbose = 1 ]] && /usr/bin/echo "Performing cipher function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset code_attr=""	
	typeset -i code=0
	typeset -i rc=0

	[[ "${NIMCONFIG}"         = "yes" ]] && let code=$(( $code | $CODE_NIMCONFIG         ))
	[[ "${INSTALLNIM}"        = "yes" ]] && let code=$(( $code | $CODE_INSTALLNIM        ))
	[[ "${MKDIR_SPOT}"        = "yes" ]] && let code=$(( $code | $CODE_MKDIR_SPOT        ))
	[[ "${MKDIR_BUNDLE}"      = "yes" ]] && let code=$(( $code | $CODE_MKDIR_BUNDLE      )) 
	[[ "${MKDIR_MKSYSB}"      = "yes" ]] && let code=$(( $code | $CODE_MKDIR_MKSYSB      ))
	[[ "${MKDIR_LPPSOURCE}"   = "yes" ]] && let code=$(( $code | $CODE_MKDIR_LPPSOURCE   ))
	[[ "${DEFINE_SPOT}"       = "yes" ]] && let code=$(( $code | $CODE_DEFINE_SPOT       ))
	[[ "${DEFINE_CLIENT}"     = "yes" ]] && let code=$(( $code | $CODE_DEFINE_CLIENT     ))
	[[ "${DEFINE_MKSYSB}"     = "yes" ]] && let code=$(( $code | $CODE_DEFINE_MKSYSB     ))
	[[ "${DEFINE_BUNDLE}"     = "yes" ]] && let code=$(( $code | $CODE_DEFINE_BUNDLE     ))
	[[ "${DEFINE_BOSINST}"    = "yes" ]] && let code=$(( $code | $CODE_DEFINE_BOSINST    ))
	[[ "${DEFINE_LPPSOURCE}"  = "yes" ]] && let code=$(( $code | $CODE_DEFINE_LPPSOURCE  ))
	[[ "${DEFINE_RESOLV}"     = "yes" ]] && let code=$(( $code | $CODE_DEFINE_RESOLV     ))
	[[ "${DEFINE_SCRIPT_LIC}" = "yes" ]] && let code=$(( $code | $CODE_DEFINE_SCRIPT_LIC ))
	[[ "${COPY_BUNDLE}"       = "yes" ]] && let code=$(( $code | $CODE_COPY_BUNDLE       ))
	[[ "${COPY_MKSYSB}"       = "yes" ]] && let code=$(( $code | $CODE_COPY_MKSYSB       ))
	[[ "${COPY_BOSINST}"      = "yes" ]] && let code=$(( $code | $CODE_COPY_BOSINST      ))
	[[ "${COPY_LPPSOURCE}"    = "yes" ]] && let code=$(( $code | $CODE_COPY_LPPSOURCE    ))
        [[ "${COPY_RESOLV}"       = "yes" ]] && let code=$(( $code | $CODE_COPY_RESOLV       ))
	[[ "${CREATE_BOSINST}"    = "yes" ]] && let code=$(( $code | $CODE_CREATE_BOSINST    ))
	[[ "${CREATE_SCRIPT_LIC}" = "yes" ]] && let code=$(( $code | $CODE_CREATE_SCRIPT_LIC ))
	[[ "${MKFS_TFTPBOOT}"     = "yes" ]] && let code=$(( $code | $CODE_MKFS_TFTPBOOT     ))
	[[ "${MKFS_NIMINSTALL}"   = "yes" ]] && let code=$(( $code | $CODE_MKFS_NIMINSTALL   ))
	[[ "${MKDIR_VIO_IMAGE}"   = "yes" ]] && let code=$(( $code | $CODE_MKDIR_VIO_IMAGE   ))

	[[ $verbose = 1 ]] && /usr/bin/echo "The code for cleanup is : ${code}" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	CODE=${code}

	#
	# Add the CODE to nim master comments attribute, this will be use by decipher to 
	# instruct us what to do for cleanup
	#
	if [[ ! -s /usr/sbin/lsnim ]]; then
		return 1
	fi

	code_attr=`/usr/sbin/lsnim -a comments master | /usr/bin/grep "comments =" | /usr/bin/cut -d'=' -f2 | /usr/bin/sed 's/^ //g'`
	if [[ -n ${code_attr} ]]; then
		/usr/bin/echo ${code_attr} | /usr/bin/grep "code:" >/dev/null 2>&1
		rc=$?
		if [[ $rc -ne 0 ]] && [[ ${CODE} -ne 0 ]]; then
			
			#
			# code_attr exist but with any code, just add the code
			#
			code_attr="${code_attr} - code:$CODE"
		elif [[ $rc -eq 0 ]] && [[ ${CODE} -ne 0 ]]; then

			#
			# If code_attr already contain cleanup code, then remove it and add the
			# latest code.
			#
			code_attr=`/usr/bin/echo ${code_attr} | /usr/bin/cut -d '-' -f1 | /usr/bin/sed 's/ $//g'`
			code_attr="${code_attr} - code:$CODE"
		elif [[ $rc -eq 0 ]] && [[ ${CODE} -eq 0 ]]; then
			#
			# If code_attr already contain cleanup code, then remove it and add the
			# latest code.
			#
			code_attr=`/usr/bin/echo ${code_attr} | /usr/bin/cut -d '-' -f1 | /usr/bin/sed 's/ $//g'`
		fi
	elif [[ ${CODE} -ne 0 ]]; then

		#
		# If code_attr does not exist, then create our own comments
		#
		code_attr=`/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_MASTER_COMMENT} \
			'NIM master for controlling %1$s' "${PROGRAM}"`
		code_attr="${code_attr} - code:${CODE}" 
	fi

	/usr/sbin/nim -o change -a comments="${code_attr}" master >/dev/null 2>&1

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	decipher
#
# FUNCTION:	Retrieve code from master comments to set program variables
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
# --------------------------------------------------------------------------- #
function decipher {

	[[ $verbose = 1 ]] && /usr/bin/echo "Performing decipher function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset code_attr=""
	typeset -i code=0
	typeset -i value=0

	code_attr=`/usr/sbin/lsnim -a comments master | /usr/bin/grep "comments =" | /usr/bin/cut -d'=' -f2`
	/usr/bin/echo "${code_attr}" | /usr/bin/grep "code:" >/dev/null 2>&1
	if [[ $? -eq 0 ]]; then
		code=`/usr/bin/echo ${code_attr} | /usr/bin/cut -d':' -f2`
	fi

	CODE=${code}

	let value=$(( $code & $CODE_NIMCONFIG ))
	[[ $value -eq $CODE_NIMCONFIG ]] && NIMCONFIG="yes"

	let value=$(( $code & $CODE_INSTALLNIM ))  
	[[ $value -eq $CODE_INSTALLNIM ]] && INSTALLNIM="yes"
	
	let value=$(( $code & $CODE_MKDIR_BUNDLE ))
	[[ $value -eq $CODE_MKDIR_BUNDLE ]] && MKDIR_BUNDLE="yes"

	let value=$(( $code & $CODE_MKDIR_MKSYSB ))
	[[ $value -eq $CODE_MKDIR_MKSYSB ]] && MKDIR_MKSYSB="yes"

	let value=$(( $code & $CODE_MKDIR_LPPSOURCE ))
	[[ $value -eq $CODE_MKDIR_LPPSOURCE ]] && MKDIR_LPPSOURCE="yes"

	let value=$(( $code & $CODE_MKDIR_SPOT ))
	[[ $value -eq $CODE_MKDIR_SPOT ]] && MKDIR_SPOT="yes"

	let value=$(( $code & $CODE_DEFINE_SPOT ))
	[[ $value -eq $CODE_DEFINE_SPOT ]] && DEFINE_SPOT="yes"

	let value=$(( $code & $CODE_DEFINE_CLIENT ))
	[[ $value -eq $CODE_DEFINE_CLIENT ]] && DEFINE_CLIENT="yes"

	let value=$(( $code & $CODE_DEFINE_BUNDLE ))
	[[ $value -eq $CODE_DEFINE_BUNDLE ]] && DEFINE_BUNDLE="yes"

	let value=$(( $code & $CODE_DEFINE_MKSYSB ))
	[[ $value -eq $CODE_DEFINE_MKSYSB ]] && DEFINE_MKSYSB="yes"

	let value=$(( $code & $CODE_DEFINE_BOSINST ))
	[[ $value -eq $CODE_DEFINE_BOSINST ]] && DEFINE_BOSINST="yes"

	let value=$(( $code & $CODE_DEFINE_LPPSOURCE ))
	[[ $value -eq $CODE_DEFINE_LPPSOURCE ]] && DEFINE_LPPSOURCE="yes"

	let value=$(( $code & $CODE_DEFINE_RESOLV ))
	[[ $value -eq $CODE_DEFINE_RESOLV ]] && DEFINE_RESOLV="yes"

	let value=$(( $code & $CODE_DEFINE_SCRIPT_LIC ))
	[[ $value -eq $CODE_DEFINE_SCRIPT_LIC ]] && DEFINE_SCRIPT_LIC="yes"

	let value=$(( $code & $CODE_COPY_RESOLV ))
	[[ $value -eq $CODE_COPY_RESOLV ]] && COPY_RESOLV="yes"

	let value=$(( $code & $CODE_COPY_BUNDLE ))
	[[ $value -eq $CODE_COPY_BUNDLE ]] && COPY_BUNDLE="yes"

	let value=$(( $code & $CODE_COPY_MKSYSB ))
	[[ $value -eq $CODE_COPY_MKSYSB ]] && COPY_MKSYSB="yes"

	let value=$(( $code & $CODE_COPY_BOSINST ))
	[[ $value -eq $CODE_COPY_BOSINST ]] && COPY_BOSINST="yes"
	
	let value=$(( $code & $CODE_COPY_LPPSOURCE ))
	[[ $value -eq $CODE_COPY_LPPSOURCE ]] && COPY_LPPSOURCE="yes"

	let value=$(( $code & $CODE_CREATE_BOSINST ))
	[[ $value -eq $CODE_CREATE_BOSINST ]] && CREATE_BOSINST="yes"

	let value=$(( $code & $CODE_CREATE_SCRIPT_LIC ))
	[[ $value -eq $CODE_CREATE_SCRIPT_LIC ]] && CREATE_SCRIPT_LIC="yes"

	let value=$(( $code & $CODE_MKFS_TFTPBOOT ))
	[[ $value -eq $CODE_MKFS_TFTPBOOT ]] && MKFS_TFTPBOOT="yes"

	let value=$(( $code & $CODE_MKFS_NIMINSTALL ))
	[[ $value -eq $CODE_MKFS_NIMINSTALL ]] && MKFS_NIMINSTALL="yes"

        let value=$(( $code & $CODE_MKDIR_VIO_IMAGE ))
        [[ $value -eq $CODE_MKDIR_VIO_IMAGE ]] && MKDIR_VIO_IMAGE="yes"

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	cleanup
#
# FUNCTION: 	Perform cleanup by removing directory, nim definitions or
#		unconfiguring and uninstalling nim
#
# PARAMETERS:   None.
#
# RETURNS:      0 - cleanup perform successfully
#               1 - unable to perform cleanup
# --------------------------------------------------------------------------- #
function cleanup {

	[[ $verbose = 1 ]] && /usr/bin/echo "Performing cleanup operation" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	#
	# Check for existing nim environment
	#
	ck_nim_env || return 1

	#
	# Retrieve CODE for cleanup
	#
	decipher 
	ck_target || return 1

	/usr/sbin/lsnim -a spot ${CLIENT} 2>&1 | /usr/bin/grep "spot =" >/dev/null 2>&1
	if [[ $? -eq 0 ]]; then
		if [[ -z ${force} ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_CLEANUP} \
				'%1$s: Unable to perform cleanup because resources are still being used.\n' "${PROGRAM}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_TRY_FORCE} \
				'Try using the force flag.\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		else
			#
			# Force was invoked for cleanup, allow every thing to be
			# remove except for unconfiguring nim unless the unconfig
			# was specify.  All will be done in undo.
			#
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_RESET_AND_DEALLOCATE} \
				'Resetting client and deallocating resources.\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/sbin/nim -Fo reset ${CLIENT}
			/usr/sbin/nim -Fo deallocate -a subclass=all ${CLIENT}
		fi
       	fi

	#
	# Call undo to remove everything according to variable set by CODE.
	#
	undo 

	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         rm_nim_obj
#
# FUNCTION:     Remove NIM object by calling nim -o remove
#
# PARAMETERS:   None.
#
# RETURNS:      0 - object removed successfully
#               1 - unable to remove object
# --------------------------------------------------------------------------- #
function rm_nim_obj {

	[[ $verbose = 1 ]] && /usr/bin/echo "Performing rm_nim_obj operation" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset object=$1

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_REMOVING_OBJECT} \
		'Removing %1$s object definition...' "${object}" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}
	/usr/sbin/nim -o remove ${object} >/dev/null 2>&1
	if [[ $? -eq 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}
	else
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_TO_REMOVE} \
			'%1$s: Unable to remove %2$s.\n' "${PROGRAM}" "${object}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME:         rm_obj_file
#
# FUNCTION:     Remove the file that from NIM object location
#
# PARAMETERS:   None.
#
# RETURNS:      0 - file removed successfully
#               1 - unable to remove file
# --------------------------------------------------------------------------- #
function rm_obj_file {

        [[ $verbose = 1 ]] && /usr/bin/echo "Performing rm_obj_file operation" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

        typeset file=$1

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_REMOVING_FILE} \
		'Removing file %1$s...' "${file}" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}
	/usr/bin/rm -r ${file} >/dev/null 2>&1
	if [[ $? -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_REMOVE_FILE} \
			'%1$s: Unable to remove file %2$s.\n' "${PROGRAM}" "${file}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	else
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
	fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME:         rm_dir
#
# FUNCTION:     Remove directory
#
# PARAMETERS:   None.
#
# RETURNS:      0 - directory removed successfully
#               1 - unable to remove directory
# --------------------------------------------------------------------------- #
function rm_dir {

	[[ $verbose = 1 ]] && /usr/bin/echo "Performing rm_dir operation" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset directory=$1

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_REMOVING_DIR} \
		'Removing directory %1$s...' "${directory}" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}
	/usr/bin/rmdir ${directory} >/dev/null 2>&1
	if [[ $? -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_TO_REMOVE} \
			'%1$s: Unable to remove %2$s.\n' "${PROGRAM}" "${directory}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
	else
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	undo
#
# FUNCTION: 	Undo what was done.  
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function undo {

	[[ $verbose = 1 ]] && /usr/bin/echo "Performing undo operation" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset lpp_location=""
	typeset bundle_location=""
	typeset mksysb_location=""
	typeset bosinst_location=""
	typeset resolv_location=""
	typeset script_lic_location=""
	
	# Remove object definition
	if [[ "${DEFINE_CLIENT}" = "yes" ]]; then
		rm_nim_obj ${CLIENT}
		if [[ $? -eq 0 ]]; then
			DEFINE_CLIENT=""
		fi
	fi

	if [[ "${DEFINE_SPOT}" = "yes" ]]; then
		rm_nim_obj ${CLIENT_SPOT}
		if [[ $? -eq 0 ]]; then
			DEFINE_SPOT=""
		fi
	fi

	if [[ "${DEFINE_MKSYSB}" = "yes" ]]; then
		mksysb_location=`/usr/sbin/lsnim -a location ${CLIENT_MKSYSB} | \
			/usr/bin/grep "location =" | /usr/bin/cut -d'=' -f2 | /usr/bin/sed 's/ //g'`
		rm_nim_obj ${CLIENT_MKSYSB}

		#
		# If object cannot be remove, then blank out location
		# to prevent the object file from being deleted
		#
		if [[ $? -ne 0 ]]; then
			mksysb_location=""
		else
			DEFINE_MKSYSB=""
		fi
	fi

	if [[ "${DEFINE_LPPSOURCE}" = "yes" ]]; then
		lpp_location=`/usr/sbin/lsnim -a location ${CLIENT_LPPSOURCE} | \
			/usr/bin/grep "location =" | /usr/bin/cut -d'=' -f2 | /usr/bin/sed 's/ //g'`
		rm_nim_obj ${CLIENT_LPPSOURCE}

                #
                # If object cannot be remove, then blank out location
                # to prevent the object file from being deleted
                #
                if [[ $? -ne 0 ]]; then
                        lpp_location=""
		else
			DEFINE_LPPSOURCE=""
                fi

	fi

	if [[ "${DEFINE_BOSINST}" = "yes" ]]; then
		bosinst_location=`/usr/sbin/lsnim -a location ${CLIENT_BOSINST} | \
			/usr/bin/grep "location =" | /usr/bin/cut -d'=' -f2 | /usr/bin/sed 's/ //g'`
		rm_nim_obj ${CLIENT_BOSINST}

                #
                # If object cannot be remove, then blank out location
                # to prevent the object file from being deleted
                #
                if [[ $? -ne 0 ]]; then
                        bosinst_location=""
		else
			DEFINE_BOSINST=""
                fi
	fi

	if [[ "${DEFINE_BUNDLE}" = "yes" ]]; then
		bundle_location=`/usr/sbin/lsnim -a location ${CLIENT_BUNDLE} | \
			/usr/bin/grep "location =" | /usr/bin/cut -d'=' -f2 | /usr/bin/sed 's/ //g'`
		rm_nim_obj ${CLIENT_BUNDLE}

                #
                # If object cannot be remove, then blank out location
                # to prevent the object file from being deleted
                #
                if [[ $? -ne 0 ]]; then
                        bundle_location=""
		else
			DEFINE_BUNDLE=""
		fi
	fi

	if [[ "${DEFINE_RESOLV}" = "yes" ]]; then
		resolv_location=`/usr/sbin/lsnim -a location ${CLIENT_RESOLV} | \
			/usr/bin/grep "location =" | /usr/bin/cut -d'=' -f2 | /usr/bin/sed 's/ //g'`
		rm_nim_obj ${CLIENT_RESOLV}

		#
		# If object cannot be remove, then blank out location
		# to prevent the object file from being deleted
		#
		if [[ $? -ne 0 ]]; then
			resolv_location=""
		else
			DEFINE_RESOLV=""
		fi
	fi

	if [[ "${DEFINE_SCRIPT_LIC}" = "yes" ]]; then
		script_lic_location=`/usr/sbin/lsnim -a location ${CLIENT_SCRIPT_LIC} | \
			/usr/bin/grep "location =" | /usr/bin/cut -d'=' -f2 | /usr/bin/sed 's/ //g'`
		rm_nim_obj ${CLIENT_SCRIPT_LIC}

		#
		# If object cannot be remove, then blank out location
		# to prevent the object file from being deleted
		#
		if [[ $? -ne 0 ]]; then
			script_lic_location=""
		else
			DEFINE_SCRIPT_LIC=""
		fi
        fi

	# Remove files
	if [[ "${COPY_MKSYSB}" = "yes" ]] && [[ -s ${mksysb_location} ]]; then
		rm_obj_file ${mksysb_location}
		if [[ $? -eq 0 ]]; then
			COPY_MKSYSB=""
		fi
	fi

	if [[ "${COPY_BOSINST}" = "yes" ]] || [[ "${CREATE_BOSINST}" = "yes" ]] &&
           [[ -s ${bosinst_location} ]]; then
		rm_obj_file ${bosinst_location}
		if [[ $? -eq 0 ]]; then
			COPY_BOSINST=""
		fi
	fi 

	if [[ "${COPY_BUNDLE}" = "yes" ]] && [[ -s ${bundle_location} ]]; then
		rm_obj_file ${bundle_location}
		if [[ $? -eq 0 ]]; then
			COPY_BUNDLE=""
		fi
	fi

	if [[ "${COPY_LPPSOURCE}" = "yes" ]] || [[ -s ${lpp_location} ]]; then
		rm_obj_file ${lpp_location}
		if [[ $? -eq 0 ]]; then
			COPY_LPPSOURCE=""
		fi
	fi

	if [[ "${COPY_RESOLV}" = "yes" ]] && [[ -s ${resolv_location} ]]; then
		rm_obj_file ${resolv_location}
		if [[ $? -eq 0 ]]; then
			COPY_RESOLV=""
		fi
	fi

	if [[ "${CREATE_SCRIPT_LIC}" = "yes" ]] && [[ -s ${script_lic_location} ]]; then
		rm_obj_file ${script_lic_location}
	fi

	# Remove mksysb directory if we created it
	if [[ "${MKDIR_MKSYSB}" = "yes" ]]; then
		rm_dir ${TARGET_DIR_MKSYSB}
		if [[ $? -eq 0 ]]; then
			MKDIR_MKSYSB=""
		fi
	fi

	# Remove spot directory if we created it
	if [[ "${MKDIR_SPOT}" = "yes" ]]; then
		rm_dir ${TARGET_DIR_SPOT}
		if [[ $? -eq 0 ]]; then
			MKDIR_SPOT=""
		fi
	fi

	# Remove bundle directory if we created it
        if [[ "${MKDIR_BUNDLE}" = "yes" ]]; then
		rm_dir ${TARGET_DIR_BUNDLE}
		if [[ $? -eq 0 ]]; then
			MKDIR_BUNDLE=""
		fi
        fi

	# Remove lpp_source directory if we created it
	if [[ "${MKDIR_LPPSOURCE}" = "yes" ]]; then
		rm_dir ${TARGET_DIR_LPPSOURCE}
		if [[ $? -eq 0 ]]; then
			MKDIR_LPPSOURCE=""
		fi
        fi

	# Remove lpp_source directory if we created it
        if [[ "${MKDIR_VIO_IMAGE}" = "yes" ]]; then
                rm_obj_file ${TARGET_DIR_VIO_IMAGE}
                if [[ $? -eq 0 ]]; then
                        MKDIR_VIO_IMAGE=""
                fi
        fi

	unmount_source

	#
	# If we configured nim, then unconfigure it.
	#
	if [[ -n $unconfig ]]; then

        	if [[ "${NIMCONFIG}" = "yes" ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_UNCONFIGURING} '>>>> Unconfiguring NIM\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/sbin/nim -o unconfig master

			#
			# Remove the filesystem that we created and mount point
			#
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_REMOVING_FS} \
				'>>>> Removing the %s filesystem.\n' "${FILESYSTEM}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			if [[ "${MKFS_NIMINSTALL}" = "yes" ]]; then
				/usr/sbin/unmount ${FILESYSTEM} >/dev/null 2>&1
				/usr/sbin/rmfs -r ${FILESYSTEM} >/dev/null 2>&1
			fi

			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_REMOVING_FS} \
                                '>>>> Removing the %s filesystem.\n' "${TFTPBOOT}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			if [[ "${MKFS_TFTPBOOT}" = "yes" ]]; then
				/usr/sbin/unmount ${TFTPBOOT} >/dev/null 2>&1
				/usr/sbin/rmfs ${TFTPBOOT} >/dev/null 2>&1
			fi
		fi

		# if we install the bos.sysmgt.nim.master fileset, then uninstall it
		if [[ "${INSTALLNIM}" = "yes" ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_UNINSTALLING} \
				'Uninstalling %1$s fileset.\n' "bos.sysmgt.nim.master" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/sbin/installp -ug bos.sysmgt.nim.master
			if [[ $? -ne 0 ]]; then
				/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_UNINSTALL} \
					'%1$s: Unable to uninstall %2$s fileset.\n' "${PROGRAM}" "bos.sysmgt.nim.master" \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
			fi
		fi
	fi

	#
	# Call cipher again to add CODE to nim master comments attribute
	# in case we weren't told to cleanup completely.
	#
	if [[ -z ${unconfig} ]]; then
		cipher
	fi

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	create_resources
#
# FUNCTION: 	Create resources from VIO dvd or files from command line.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function create_resources {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running create_resources function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	if [[ $EXTRACT_IMAGE = "yes" ]]; then
		extract_image	  || return 1
	elif [[ $ISO_IMAGE = "yes" ]]; then
		prep_iso_image || return 1
	fi

	define_client             || return 1

	if [[ -z ${SOURCE_AIXBASE} && -z ${SOURCE_VIOS_MIG} ]]; then
	 	define_mksysb     || return 1
		define_bundle     || return 1
	fi

	define_lppsource	  || return 1	# define_lppsource after define_bundle

	define_spot               || return 1	# define_spot after define_lppsource and define_mksysb
	define_bosinst            || return 1
       	define_resolv_conf        || return 1

	if [[ -n $accept_license ]]; then
		define_script_lic ${TARGET_SCRIPT_LIC} ${locale} 
	fi

	cipher

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	define_client
#
# FUNCTION: 	Create client definition
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function define_client {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running define_client function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset clientname=""
	typeset comments=""

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DEFINING_OBJECT} \
		'Defining %1$s object...' "${CLIENT}" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	comments=`/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CLIENT_FOR} 'Client for %1$s' ${PROGRAM}`

	# resolve client ip address to hostname
	clientname=`/usr/bin/host ${client} | /usr/bin/cut -f1 -d" " | /usr/bin/cut -f1 -d"."`

	/usr/sbin/nim -o define -t standalone \
		-a if1="find_net $clientname 0" -a cable_type1=N/A -a connect=nimsh \
		-a netboot_kernel=mp -a net_definition="ent ${mask} ${gateway}" \
		-a net_settings1="$speed $duplex" \
		${vlan_tag:+-a vlan_tag=$vlan_tag} ${vlan_pri:+-a vlan_pri=$vlan_pri} \
		-a comments="${comments}" ${CLIENT} \
		>> ${INSTALLIOS_LOG} 2>&1

	
	if [[ $? -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_DEFINE_OBJ} \
			'%1$s: Unable to define %2$s object.\n' "${PROGRAM}" "${CLIENT}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi
	
	DEFINE_CLIENT="yes"
	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' | /usr/bin/tee -a ${INSTALLIOS_LOG}
	
        return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	define_spot
#
# FUNCTION: 	Create spot definition from the mksysb
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function define_spot {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running define_spot function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset spot_source=""

        if [[ ${MKDIR_VIO_IMAGE} = "yes" ]]; then
                $MV ${SOURCE_SPOT} ${TARGET_SPOT} || cmd_err 1 "${MV} ${SOURCE_SPOT} ${TARGET_SPOT}"
	else 
		if [[ -z ${SOURCE_AIXBASE} && -z ${SOURCE_VIOS_MIG} ]]; then
			spot_source=${CLIENT_MKSYSB}
		else
			spot_source=${CLIENT_LPPSOURCE}
		fi
	fi

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DEFINING_OBJECT} \
       	        'Defining %1$s object...' "${CLIENT_SPOT}" | /usr/bin/tee -a ${INSTALLIOS_LOG}

	if [[ ${MKDIR_VIO_IMAGE} = "yes" ]]; then
		OLD_ODMDIR=`echo $ODMDIR`
                ODMDIR=${TARGET_SPOT}/usr/lib/objrepos
		ODMADD=${TARGET_SPOT}/usr/bin/odmadd
                echo "product:\nlpp_name=\"bos.sysmgt.nim.spot\"\nstate=5" \
                        | ${ODMADD} >> ${INSTALLIOS_LOG} 2>&1
		ODMDIR=$OLD_ODMDIR

		/usr/lpp/bos.sysmgt/nim/methods/m_mkspot -o -a server=master -a location=${FILESYSTEM}/spot \
			-a source=no ${CLIENT_SPOT} >> ${INSTALLIOS_LOG} 2>&1
		rc=$?

	else
		# Increase size of the filesystem by 200M to store the SPOT from the mksysb
		calc_fs ${FILESYSTEM} $MKSYSB_SPOT_SIZE

		# Attempt to define spot from the mksysb
		/usr/sbin/nim -o define -t spot -a location=${TARGET_DIR_SPOT} \
			-a server=master -a source=${spot_source} ${CLIENT_SPOT} \
			>> ${INSTALLIOS_LOG} 2>&1
		rc=$?
	fi

	if [[ $rc -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_DEFINE_SPOT_FOR_MKSYSB} \
			'%1$s: Unable to define %2$s from %3$s.\n' \
			"${PROGRAM}" "${CLIENT_SPOT}" "${CLIENT_MKSYSB}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi

	DEFINE_SPOT="yes"
	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' | /usr/bin/tee -a ${INSTALLIOS_LOG}
	
	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	define_mksysb
#
# FUNCTION: 	Create mksysb definition
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function define_mksysb {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running define_mksysb function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	ulimit -f unlimited
	if [[ ${MKDIR_VIO_IMAGE} = "yes" ]]; then
		$MV ${SOURCE_MKSYSB} ${TARGET_MKSYSB} || cmd_err 1 "${MV} ${SOURCE_MKSYSB} ${TARGET_MKSYSB}"
		COPY_MKSYSB="yes"
	else
	        # Calculate and increase filesystem size if necessary
                MKSYSB_SIZE=0
                /usr/bin/ls ${SOURCE_MKSYSB}* | while read SYSB_FILE;
                do
			[[ -s $SYSB_FILE ]] && \
			MKSYSB_SIZE=$((${MKSYSB_SIZE} + $(/usr/bin/du -kL ${SYSB_FILE} | /usr/bin/awk '{print $1}')))
                done

		calc_fs ${FILESYSTEM} $MKSYSB_SIZE

	        # Copy mksysb from DVD to location
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_COPY_FROM_TO} \
			'Copying from %1$s to %2$s...' "${SOURCE_MKSYSB}" "${TARGET_DIR_MKSYSB}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}

		LASTVOLUME=
		if [[ -s ${CD_MNT_DIR}/mkcd.data ]]; then
			LASTVOLUME=`$GREP "LASTVOLUME" ${CD_MNT_DIR}/mkcd.data`
			
			VOLUME=`$GREP "VOLUME=" ${CD_MNT_DIR}/mkcd.data | $CUT -d= -f2`
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_PROCESS_MULTI_VOLUME} \
				'Processing multivolume %1$s...\n' "${VOLUME}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
		fi

		/usr/bin/cp ${SOURCE_MKSYSB} ${TARGET_DIR_MKSYSB}/${CLIENT_MKSYSB}
		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_COPY_FROM_TO} \
				'%1$s: Unable to copy from %2$s to %3$s.\n' \
				"${PROGRAM}" "${SOURCE_MKSYSB}" "${TARGET_DIR_MKSYSB}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi

                if [[ -s "${SOURCE_MKSYSB}2" ]]; then
			$LS ${SOURCE_MKSYSB}* | /usr/bin/grep -v "${SOURCE_MKSYSB}"$ | while read SYSB_FILE;
			do
				/usr/bin/cat ${SYSB_FILE} >> ${TARGET_DIR_MKSYSB}/${CLIENT_MKSYSB}
				if [[ $? -ne 0 ]]; then
					/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
						| /usr/bin/tee -a ${INSTALLIOS_LOG}
					/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_COPY_FROM_TO} \
						'%1$s: Unable to copy from %2$s to %3$s.\n' \
						"${PROGRAM}" "${SYSB_FILE}" "${TARGET_DIR_MKSYSB}" \
						| /usr/bin/tee -a ${INSTALLIOS_LOG}
					return 1
				fi
			done
		fi

		if [[ $ISO_IMAGE = "yes" ]]; then
			FIND_ISO="yes"
		fi

		while [[ -z $LASTVOLUME ]]; do
			typeset response=""
			if [[ -z $ISO_IMAGE ]]; then
				/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_MOUNT_VOLUME_TO_CONTINUE} \
					'Please mount volume %1$s to continue.\n' \
					"$(( $VOLUME + 1 ))" \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
			fi
			unmount_source

			while [[ "${response}" != "c" ]]; do

				if [[ $ISO_IMAGE = "yes" ]]; then
					response="c"
					if [[ $FIND_ISO = "yes" ]]; then
						iso_file=`/usr/bin/basename $SOURCE`
						iso_dir=`/usr/bin/dirname $SOURCE`
						iso_file=`echo $iso_file | ${SED} s/${VOLUME}.iso/$(( $VOLUME+1 )).iso/`
						NEW_SOURCE_DIR=${iso_dir}/${iso_file}
						FIND_ISO=
						if [[ ! -s $NEW_SOURCE_DIR ]]; then
							NEW_SOURCE_DIR=
						fi
					else
						/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_PATH_OR_QUIT} \
							'Enter the full path to volume %1$s ('q' to quit).\n' \
							"$(( $VOLUME + 1 ))" \
							| /usr/bin/tee -a ${INSTALLIOS_LOG}
						read NEW_SOURCE_DIR
						/usr/bin/echo "$NEW_SOURCE_DIR" >> ${INSTALLIOS_LOG}
						if [[ $NEW_SOURCE_DIR = "q" ]]; then
							cmd_err 1
						fi
                                        fi
                                fi

				if [[ $response != "c" ]]; then
					/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_PRESS_C_TO_CONTINUE} \
						'Please press 'c' to continue ('q' to quit).\n' \
						| /usr/bin/tee -a ${INSTALLIOS_LOG}
					echo "=> " | /usr/bin/tee -a ${INSTALLIOS_LOG}
					read response
					/usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
				fi

				if [[ "${response}" = "q" ]]; then
					cmd_err 1
				elif [[ "${response}" = "c" ]]; then
					if [[ $ISO_IMAGE = "yes" ]]; then
						if [[ -z $NEW_SOURCE_DIR ]]; then
							continue
						fi

						if [[ -s $NEW_SOURCE_DIR ]]; then
							iso_mount "${NEW_SOURCE_DIR}"
							rc=$?
						fi
					else
						ck_cdrom "${SOURCE_CDROM}"
						rc=$?
					fi

					if [[ $rc -ne 0 ]]; then
						response=
						continue
					fi

					if [[ -s ${CD_MNT_DIR}/mkcd.data ]]; then
						LASTVOLUME=`$GREP "LASTVOLUME" ${CD_MNT_DIR}/mkcd.data`
						NEW_VOLUME=`$GREP "VOLUME=" ${CD_MNT_DIR}/mkcd.data | $CUT -d= -f2`
						if [[ $NEW_VOLUME = $((VOLUME + 1 )) ]] && [[ -s ${SOURCE_MKSYSB} ]]; then
							VOLUME=$NEW_VOLUME
							/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_PROCESS_MULTI_VOLUME} \
								'Processing multivolume %1$s...\n' "${VOLUME}" \
								| /usr/bin/tee -a ${INSTALLIOS_LOG}
							MKSYSB_SIZE=0
							/usr/bin/ls ${SOURCE_MKSYSB}* | while read SYSB_FILE;
							do
								[[ -s $SYSB_FILE ]] && \
								MKSYSB_SIZE=$((${MKSYSB_SIZE} + $(/usr/bin/du -kL ${SYSB_FILE} | /usr/bin/awk '{print $1}')))
							done
							calc_fs ${FILESYSTEM} $MKSYSB_SIZE

							/usr/bin/cat ${SOURCE_MKSYSB} >> ${TARGET_DIR_MKSYSB}/${CLIENT_MKSYSB}
							if [[ $? -ne 0 ]]; then
								/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
									| /usr/bin/tee -a ${INSTALLIOS_LOG}
								/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_COPY_FROM_TO} \
									'%1$s: Unable to copy from %2$s to %3$s.\n' \
									"${PROGRAM}" "${SYSB_FILE}" "${TARGET_DIR_MKSYSB}" \
									| /usr/bin/tee -a ${INSTALLIOS_LOG}
								return 1
							fi

							if [[ -s "${SOURCE_MKSYSB}2" ]]; then
								$LS ${SOURCE_MKSYSB}* | /usr/bin/grep -v "${SOURCE_MKSYSB}"$ | while read SYSB_FILE;
								do
									$CAT ${SYSB_FILE} >> ${TARGET_DIR_MKSYSB}/${CLIENT_MKSYSB}
									if [[ $? -ne 0 ]]; then
										/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
											| /usr/bin/tee -a ${INSTALLIOS_LOG}
										/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_COPY_FROM_TO} \
											'%1$s: Unable to copy from %2$s to %3$s.\n' \
											"${PROGRAM}" "${SYSB_FILE}" "${TARGET_DIR_MKSYSB}" \
											| /usr/bin/tee -a ${INSTALLIOS_LOG}
										return 1
									fi
								done
							fi
						else
							/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_INVALID_MEDIA} \
								'Invalid media.\n' | /usr/bin/tee -a ${INSTALLIOS_LOG}
							break
						fi
					else
						/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_INVALID_MEDIA} \
							'Invalid media.\n' | /usr/bin/tee -a ${INSTALLIOS_LOG}
						break
					fi
				fi
			done
		done

		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		COPY_MKSYSB="yes"
	fi

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DEFINING_OBJECT} \
               	'Defining %1$s object...' "${CLIENT_MKSYSB}" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	# Define mksysb
	/usr/sbin/nim -o define -t mksysb -a server=master -a location=${TARGET_MKSYSB} ${CLIENT_MKSYSB} \
		>> ${INSTALLIOS_LOG} 2>&1

	if [[ $? -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
       	        /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_DEFINE_OBJ} \
			'%1$s: Unable to define %2$s object.\n' "${PROGRAM}" "${CLIENT_MKSYSB}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}

		return 1
	fi

	DEFINE_MKSYSB="yes"
	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' | /usr/bin/tee -a ${INSTALLIOS_LOG}

	return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	define_lppsource
#
# FUNCTION: 	Create lpp_source definition with filesets information 
#		obtained from installp bundle
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function define_lppsource {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running define_lppsource function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset source_lpp=""

	if [[ -z ${SOURCE_AIXBASE} && -z ${SOURCE_VIOS_MIG} ]] && [[ $DEFINE_BUNDLE = "yes" ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CREATE_LPP_FROM_BUNDLE} \
			'Creating %1$s lpp_source from %2$s bundle file...' \
			"${CLIENT_LPPSOURCE}" "${TARGET_BUNDLE}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}

		/usr/sbin/gencopy -X -d $SOURCE_LOCALE -t ${TARGET_LPPSOURCE} \
			$(/usr/bin/cat $TARGET_BUNDLE) >/dev/null 2>&1
		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_COMMAND_FAILED} \
				'%1$s: %2$s command failed.\n' "${PROGRAM}" "gencopy" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
 		fi
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		COPY_LPPSOURCE="yes"
		source_lpp=${TARGET_LPPSOURCE}

		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DEFINING_OBJECT} \
			'Defining %1$s object...' "${CLIENT_LPPSOURCE}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}

		/usr/sbin/nim -o define -t lpp_source -a server=master -a location=${TARGET_LPPSOURCE} \
			-a source=${source_lpp} ${CLIENT_LPPSOURCE} >> ${INSTALLIOS_LOG} 2>&1

		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_DEFINE_OBJ} \
				'%1$s: Unable to define %2$s object.\n' "${PROGRAM}" "${CLIENT_LPPSOURCE}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi

		DEFINE_LPPSOURCE="yes"
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}

	elif [[ -n ${SOURCE_AIXBASE} ]]; then
		source_lpp=${SOURCE}

		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DEFINING_OBJECT} \
			'Defining %1$s object...' "${CLIENT_LPPSOURCE}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}

		/usr/sbin/nim -o define -t lpp_source -a server=master -a location=${TARGET_LPPSOURCE} \
			-a source=${source_lpp} ${CLIENT_LPPSOURCE} >> ${INSTALLIOS_LOG} 2>&1

		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_DEFINE_OBJ} \
				'%1$s: Unable to define %2$s object.\n' "${PROGRAM}" "${CLIENT_LPPSOURCE}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi

		DEFINE_LPPSOURCE="yes"
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}

	elif [[ -n ${SOURCE_VIOS_MIG} ]]; then
		source_lpp=${SOURCE}

		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DEFINING_OBJECT} \
			'Defining %1$s object...' "${CLIENT_LPPSOURCE}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}

		/usr/sbin/gencopy -X -d ${source_lpp} -t ${TARGET_LPPSOURCE} all
		[[ $? -eq 0 ]] && /usr/sbin/nim -o define -t lpp_source -a server=master -a location=${TARGET_LPPSOURCE} \
			${CLIENT_LPPSOURCE} >> ${INSTALLIOS_LOG} 2>&1

		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_DEFINE_OBJ} \
				'%1$s: Unable to define %2$s object.\n' "${PROGRAM}" "${CLIENT_LPPSOURCE}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi

		DEFINE_LPPSOURCE="yes"
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
	fi

        return 0
}

# --------------------------------------------------------------------------- #
# NAME: 	define_bosinst
#
# FUNCTION: 	Create bosinst definition
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function define_bosinst {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running define_bosinst function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	if [[ -z ${SOURCE_AIXBASE} ]]; then
		if [[ $MKDIR_VIO_IMAGE = "yes" ]] && [[ -n $SOURCE_BOSINST ]]; then
		        ${MV} ${SOURCE_BOSINST} ${TARGET_BOSINST} || cmd_err 1 "${MV} ${SOURCE_BOSINST} ${TARGET_BOSINST}"
			COPY_BOSINST="yes"
		else
			if [[ ! -s ${SOURCE_BOSINST} ]]; then
				create_bosinst ${TARGET_BOSINST}
			else
				# Copy bosinst_data from DVD to location
				/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_COPY_FROM_TO} \
					'Copying from %1$s to %2$s...' "${SOURCE_BOSINST}" "${TARGET_BOSINST}" \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
				/usr/bin/cp ${SOURCE_BOSINST} ${TARGET_BOSINST} 

				if [[ $? -ne 0 ]]; then
					/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
						| /usr/bin/tee -a ${INSTALLIOS_LOG}
                		        /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_COPY_FROM_TO} \
                        		        '%1$s: Unable to copy from %2$s to %3$s.\n' \
                                		"${PROGRAM}" "${SOURCE_BOSINST}" "${TARGET_BOSINST}" \
						| /usr/bin/tee -a ${INSTALLIOS_LOG}

					return 1
				fi
			fi
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			COPY_BOSINST="yes"
		fi
	else
		create_bosinst ${TARGET_BOSINST}
	fi

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DEFINING_OBJECT} \
		'Defining %1$s object...' "${CLIENT_BOSINST}" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	# Define bosinst_data
	/usr/sbin/nim -o define -t bosinst_data -a server=master -a location=${TARGET_BOSINST} ${CLIENT_BOSINST} \
		>> ${INSTALLIOS_LOG} 2>&1

	if [[ $? -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_DEFINE_OBJ} \
                       	'%1$s: Unable to define %2$s object.\n' "${PROGRAM}" "${CLIENT_BOSINST}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi

	DEFINE_BOSINST="yes"
	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' | /usr/bin/tee -a ${INSTALLIOS_LOG}

	return 0
}

# -------------------------------------------------------------------------- #
# NAME: 	define_bundle
#
# FUNCTION: 	Create bundle definition
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function define_bundle {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running define_bundle function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	if [[ -s ${SOURCE_LOCALE}/${locale}.bnd ]]; then
		# Copy bundle from DVD to location
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_COPY_FROM_TO} \
			'Copying from %1$s to %2$s...' \
			"${SOURCE_LOCALE}/${locale}.bnd" "${TARGET_DIR_BUNDLE}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/cp ${SOURCE_LOCALE}/${locale}.bnd ${TARGET_DIR_BUNDLE}

		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_UNABLE_COPY_FROM_TO} \
				'%1$s: Unable to copy from %2$s to %3$s.\n' \
				"${PROGRAM}" "${SOURCE_LOCALE}/${locale}.bnd" "${TARGET_DIR_BUNDLE}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi

		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		COPY_BUNDLE="yes"

		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DEFINING_OBJECT} \
       		        'Defining %1$s object...' "${CLIENT_BUNDLE}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}

		# Define Bundle for locale
		/usr/sbin/nim -o define -t installp_bundle -a server=master -a location=${TARGET_BUNDLE} ${CLIENT_BUNDLE} \
			>> ${INSTALLIOS_LOG} 2>&1

		if [[ $? -ne 0 ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
	               	/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_DEFINE_OBJ} \
        	               	'%1$s: Unable to define %2$s object.\n' "${PROGRAM}" "${CLIENT_BUNDLE}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi

		DEFINE_BUNDLE="yes"
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' | /usr/bin/tee -a ${INSTALLIOS_LOG}
	fi

        return 0
}

# -------------------------------------------------------------------------- #
# NAME:         define_resolv_conf
#
# FUNCTION:     Create resolv_conf definition
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function define_resolv_conf {

        [[ $verbose = 1 ]] && /usr/bin/echo "Running define_resolv_conf function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	if [[ -s /etc/resolv.conf ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DEFINING_OBJECT} \
			'Defining %1$s object...' "${CLIENT_RESOLV}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/cp /etc/resolv.conf ${TARGET_RESOLV}
		COPY_RESOLV="yes"

	        # Define resolv_conf
       		/usr/sbin/nim -o define -t resolv_conf -a server=master -a location=${TARGET_RESOLV} ${CLIENT_RESOLV} \
			>> ${INSTALLIOS_LOG} 2>&1

	        if [[ $? -ne 0 ]]; then
       		        /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_DEFINE_OBJ} \
				'%1$s: Unable to define %2$s object.\n' "${PROGRAM}" "${CLIENT_RESOLV}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi

	        DEFINE_RESOLV="yes"
       		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' | /usr/bin/tee -a ${INSTALLIOS_LOG}
	fi

	return 0
}

# -------------------------------------------------------------------------- #
# NAME: 	create_bosinst
#
# FUNCTION:
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function create_bosinst {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running create_bosinst function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset location=$1

	if [[ -s $location ]]; then
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_CREATE} \
			'%1$s: Unable to create %2$s.\n' "${PROGRAM}" "${location}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CREATE_RES} \
		'Creating %s resource %s...' "script" ${location} \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	if [[ -s /usr/lpp/bosinst/bosinst.template && ! -e $location ]]; then
		/usr/bin/sed 's/CONSOLE = .*/CONSOLE = Default/;
			s/INSTALL_METHOD = .*/INSTALL_METHOD = overwrite/;
			s/PROMPT = .*/PROMPT = no/;
			s/EXISTING_SYSTEM_OVERWRITE = .*/EXISTING_SYSTEM_OVERWRITE = yes/;
			s/RECOVER_DEVICES = .*/RECOVER_DEVICES = no/;
			s/ACCEPT_LICENSES = .*/ACCEPT_LICENSES = yes/;
			s/DESKTOP = .*/DESKTOP = NONE/' /usr/lpp/bosinst/bosinst.template >$location

		if [[ $? -eq 0 ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			CREATE_BOSINST="yes"
		else
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		fi
	else
		return 1
	fi
	
	return 0
}

# -------------------------------------------------------------------------- #
# NAME:         define_script_lic
#
# FUNCTION:	Create a script resource to accept license agreement for the
#		specified languange to be run during post_install.
#		Then define the script resource.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function define_script_lic {

        [[ $verbose = 1 ]] && /usr/bin/echo "Running define_script_lic function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

        typeset location=$1
	typeset lang=$2

	if [[ -s ${location} ]]; then
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_CREATE} \
                        '%1$s: Unable to create %2$s.\n' "${PROGRAM}" "${location}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi

        /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CREATE_RES} \
                'Creating %s resource %s...' "script" $location \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	/usr/bin/echo "#!/usr/bin/ksh\n\n/usr/ios/cli/ioscli license -accept -lang $lang" > $location
	/usr/bin/echo "/usr/bin/chlang $lang" >> $location

	if [[ $? -eq 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		CREATE_SCRIPT_LIC="yes"
	else
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi	

	/usr/bin/chmod 755 ${location}

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DEFINING_OBJECT} \
		'Defining %1$s object...' "${CLIENT_SCRIPT_LIC}" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	/usr/sbin/nim -o define -t script -a server=master -a location=$location ${CLIENT_SCRIPT_LIC} \
		>> ${INSTALLIOS_LOG} 2>&1

	if [[ $? -ne 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_DEFINE_OBJ} \
			'%1$s: Unable to define %2$s object.\n' "${PROGRAM}" "${CLIENT_SCRIPT_LIC}" \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	else
		DEFINE_SCRIPT_LIC=yes
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
	fi

        return 0
}

# -------------------------------------------------------------------------- #
# NAME:         do_licenses
#
# FUNCTION:   
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function do_licenses {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running do_licenses function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

        typeset lang=$1
	typeset response=""

	if [[ -s ${SOURCE_LICENSE}/${lang}/IOS.la ]]; then
		/usr/bin/more ${SOURCE_LICENSE}/${lang}/IOS.la
	else
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_NO_LICENSE_FOR_LANG} \
			'The language %1$s does not have a license agreement.\n' $lang \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		return 1
	fi

	while [[ $response = "" ]]; do
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ACCEPT_LICENSE} \
			'Do you accept the license agreement? y/n: ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		read response
		if [[ $response = "y" ]] || 
	 	   [[ $response = `/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_YES} 'yes'` ]]; then

			# Accept license will create a script resouce to accept license
			accept_license=1
			break;
		elif [[ $response = "n" ]] || 
		     [[ $response = `/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_NO} 'no'` ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_EXIT_NO_LICENSE} \
				'Exiting because license agreement was not accepted.\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			return 1
		else
			response=""
		fi
	done
	
	return 0
}

# -------------------------------------------------------------------------- #
# NAME:        ck_hmc_lpm
#
# FUNCTION:	Check if the HMC is pingable.
#		Check if the managed system is correct
#		Check if the partition is correct for the managed system
#		Check if the profile is correct for the partition
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
#
# INFO:		Use global variable managed, partition
# --------------------------------------------------------------------------- #
function ck_hmc_lpm {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running ck_hmc_lpm function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset system=$1
	typeset type=$2
	typeset value=$3
	typeset command=""
	typeset output=""

	if [[ $system = "hmc" ]]; then
		if [[ $type = "managed" ]]; then
			command="/usr/bin/ssh hscroot@$hmc lssyscfg -r sys -F name"
		elif [[ $type = "partition" ]]; then
			command="/usr/bin/ssh hscroot@$hmc lssyscfg -r lpar -m \"$managed\" --filter \"lpar_names=$value\" -F name"
		elif [[ $type = "profile" ]]; then
			command="/usr/bin/ssh hscroot@$hmc lssyscfg -r prof -m \"$managed\" --filter \"lpar_names=$partition\" -F name"
		else 
			/usr/bin/host ${value} > /dev/null 2>&1
			if [[ $? -eq 1 ]]; then
				[[ -z $wizard ]] && /usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CANT_RESOLVE_HOST} \
					'%1$s: Unable to resolve host %2$s.\n' "${PROGRAM}" "${type}" \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
				return 1
			fi
			return 0
		fi
	elif [[ $system = "lpm" ]]; then
		if [[ $type = "managed" ]]; then
			command="/usr/bin/ssh hscroot@$hmc lssyscfg -r sys -F name"
		elif [[ $type = "partition" ]]; then
			command="/usr/bin/ssh hscroot@$hmc lssyscfg -r lpar -m \"$managed\" --filter \"lpar_names=$value\" -F name"
		elif [[ $type = "profile" ]]; then
			command="/usr/bin/ssh hscroot@$hmc lssyscfg -r prof -m \"$managed\" --filter \"lpar_names=$partition\" -F name"

		fi
	fi

	output=`$command`
	if [[ $? -eq 0 ]]; then
		if [[ $type = "managed" ]]; then
			/usr/bin/echo $output | /usr/bin/grep "$value" >/dev/null 2>&1
			if [[ $? -ne 0 ]]; then
				/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_INVALID_MANAGED} \
					'%1$s: Invalid managed system.\n' "${PROGRAM}" \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
				return 1
			fi
		elif [[ $type = "partition" ]]; then
			/usr/bin/echo $output | /usr/bin/grep "$value" >/dev/null 2>&1
			if [[ $? -ne 0 ]]; then
				/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_INVALID_PARTITION} \
					'%1$s: Invalid partition failed.\n' "${PROGRAM}" \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
				return 1
			fi
		elif [[ $type = "profile" ]]; then
			/usr/bin/echo $output | /usr/bin/grep "$value" >/dev/null 2>&1
			if [[ $? -ne 0 ]]; then
				/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_INVALID_PROFILE} \
					'%1$s: Invalid profile.\n' "${PROGRAM}" \
					| /usr/bin/tee -a ${INSTALLIOS_LOG}
				return 1
			fi
		fi
	else
		if [[ $type = "managed" ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_INVALID_MANAGED} \
				'%1$s: Invalid managed system.\n' "${PROGRAM}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
		elif [[ $type = "partition" ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_INVALID_PARTITION} \
				'%1$s: Invalid partition failed.\n' "${PROGRAM}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
		elif [[ $type = "profile" ]]; then
			/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_INVALID_PROFILE} \
				'%1$s: Invalid profile.\n' "${PROGRAM}" \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
                fi

		return 1
	fi

	return 0
}

# -------------------------------------------------------------------------- #
# NAME:         initiate_install
#
# FUNCTION:
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function initiate_install {

	[[ $verbose = 1 ]] && /usr/bin/echo "Running initiate_install function" \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}
	
	typeset arguments=""
	
	[[ -n $accept_license ]]        && arguments="-a script=$CLIENT_SCRIPT_LIC"
	[[ $DEFINE_BUNDLE = "yes" ]]    && arguments="${arguments} -a installp_bundle=${CLIENT_BUNDLE}"
	[[ $DEFINE_LPPSOURCE = "yes" ]] && arguments="${arguments} -a lpp_source=${CLIENT_LPPSOURCE}"
	
	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_INITIATING_INSTALL} 'Initiating install...' \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}
	if [[ -z ${SOURCE_AIXBASE} && -z ${SOURCE_VIOS_MIG} ]]; then
		#
		# Initiate bos install with source=mksysb
		#
		/usr/sbin/nim -o bos_inst -a source=mksysb -a spot=${CLIENT_SPOT} -a mksysb=${CLIENT_MKSYSB} \
			-a bosinst_data=${CLIENT_BOSINST} -a resolv_conf=${CLIENT_RESOLV} ${arguments} \
			-a boot_client=${BOOT_CLIENT:-no} -a accept_licenses=yes ${config_net} \
			${apply_license} ${CLIENT} >/dev/null 2>&1
	else
		#
		# Initiate bos rte install
		#
		/usr/sbin/nim -o bos_inst -a spot=${CLIENT_SPOT} -a bosinst_data=${CLIENT_BOSINST} \
			-a resolv_conf=${CLIENT_RESOLV} ${arguments} \
			-a boot_client=${BOOT_CLIENT:-no} -a accept_licenses=yes ${config_net} \
			${apply_license} ${CLIENT} >/dev/null 2>&1
	fi

	rc=$?

	if [[ $rc -eq 0 ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
	else
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
	fi

	if [[ -n $managed ]]; then
		if [[ -n $INSTALLIOS_DEBUG || -n $debug ]]; then
			debug_arg="-x -v"
		fi

		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_INITIATING_BOOT} 'Initiating network boot...' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		if [[ -n $hmc ]]; then
			server=`/usr/bin/hostname | /usr/bin/xargs /usr/bin/host | /usr/bin/cut -d' ' -f3 | /usr/bin/sed 's/,//g'`
			/usr/bin/ssh hscroot@$hmc lpar_netboot $debug_arg -A -D -t ent -d $duplex -s $speed -S $server -G $gateway -C $client -K $mask ${vlan_tag:+-V $vlan_tag} ${vlan_pri:+-Y $vlan_pri} \"$partition\" \"$profile\" \"$managed\"
		fi

		if [[ $? -eq 0 ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
		else
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_FAILED} 'failed\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
		fi

		rc=4
	fi

	return $rc
}

# -------------------------------------------------------------------------- #
# NAME:         run_wizard
#
# FUNCTION:    
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
# --------------------------------------------------------------------------- #
function run_wizard {

        [[ $verbose = 1 ]] && /usr/bin/echo "Running run_wizard function" \
                | /usr/bin/tee -a ${INSTALLIOS_LOG}

	typeset -i count=0
	typeset -i input=0
	typeset -i rc=0
	typeset config_net=""
	typeset debug_arg=""
	typeset lpar=""
	typeset lpartype=""
	typeset response

        ck_nim_env || return 1
        ck_program || return 3

	#
	# prompt for device location
	#
	response=""
	while [[ $response = "" ]]; do
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_SOURCE} \
			'Enter the source of the installation images [/dev/cd0]: ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		read response
		/usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
		if [[ -n $response ]]; then
			source=$response
		else
			source="/dev/cd0"
		fi
		ck_source
		if [[ $? -eq 0 ]]; then
			break
		else
			response=""
		fi
	done
	
	#
	# prompt for hmc
	#
	response=""
	while [[ $response = "" ]]; do
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_HMC} 'Enter the name of the HMC[none]: ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		read response
		/usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
		if [[ -n $response ]]; then
			hmc=$response
			ck_hmc_lpm "hmc" "hmc" "$hmc"
			[[ $? -ne 0 ]] && response=""
		else
			break
                fi
        done

	if [[ -n $hmc ]]; then
		ck_ssh_env && return 1	# invoke undo function with return 1
	fi

	#
	# prompt for managed system
	#
	if [[ -n $hmc ]]; then
		managed=`/usr/bin/ssh hscroot@$hmc lssyscfg -r sys -F name | /usr/bin/sed 's/ /\%/g'`
	else
		[[ -s /usr/hmcrbin/lssyscfg ]] && managed=`/usr/hmcrbin/lssyscfg -r sys -F name | /usr/bin/sed 's/ /\%/g'`
	fi
	
	if [[ -n $managed ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_SELECT_MANAGED} \
			'The following objects of type "managed system" were found.  Please select one:\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		count=0
		for i in $managed
		do
			count=count+1
			temp_managed=`echo $i | /usr/bin/sed 's/\%/ /g'`
			echo "$count.  $temp_managed" | /usr/bin/tee -a ${INSTALLIOS_LOG}
			managed[$count]=$temp_managed
		done

		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_NUMBER} 'Enter a number: ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		while [[ $input -gt $count || $input -eq 0 ]]; do
			read input
			/usr/bin/echo "$input" >> ${INSTALLIOS_LOG}
		done

		managed=${managed[$input]}
	fi

	#
	# prompt for vio partition
	#
	if [[ -n $managed ]]; then
		if [[ -n $hmc ]]; then
			partition=`/usr/bin/ssh hscroot@$hmc lssyscfg -r lpar -m \"$managed\" -F name,lpar_env | /usr/bin/sed 's/ /\%/g'`
		else
			[[ -s /usr/hmcrbin/lssyscfg ]] && 
				partition=`/usr/hmcrbin/lssyscfg -r lpar -m \"$managed\" -F name,lpar_env | /usr/bin/sed 's/ /\%/g'`
		fi
	fi

	if [[ -n $partition ]]; then
		if [[ -z $SOURCE_AIXBASE && -n $hmc ]]; then
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_SELECT_VIO_PARTITION} \
				'The following objects of type "virtual I/O server partition" were found.  Please select one:\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
		else
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_SELECT_PARTITION} \
				'The following partitions were found.  Please select one:\n' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
		fi

		count=0
		input=0
		for i in $partition
		do
			if [[ -z $SOURCE_AIXBASE && -n $hmc ]]; then
				lpar=`/usr/bin/echo $i | /usr/bin/sed 's/\%/ /g' | /usr/bin/awk '{FS=","; } {if (match($2, /vioserver/) ) print $1}'`

				if [[ -n $lpar ]]; then
					count=$count+1
					/usr/bin/echo "$count. $lpar" | /usr/bin/tee -a ${INSTALLIOS_LOG}
					partition[$count]=$lpar
				fi
			else
				lpar=`/usr/bin/echo $i | /usr/bin/sed 's/\%/ /g' | /usr/bin/cut -f1 -d','`
				lpartype=`/usr/bin/echo $i | /usr/bin/sed 's/\%/ /g' | /usr/bin/cut -f2 -d','`

				if [[ -n $lpar ]]; then
					count=$count+1
					echo "$count. $lpar\t$lpartype" | /usr/bin/tee -a ${INSTALLIOS_LOG}
					partition[$count]=$lpar
				fi
			fi
		done

		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_NUMBER} 'Enter a number: ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		while [[ $input -gt $count || $input -eq 0 ]]; do
			read input
			/usr/bin/echo "$input" >> ${INSTALLIOS_LOG}
		done

		partition=${partition[$input]}
	fi

	#
	# prompt for partition profile
	#
	if [[ -n $partition ]]; then
		if [[ -n $hmc ]]; then
			profile=`/usr/bin/ssh hscroot@$hmc lssyscfg -r prof -m \"$managed\" --filter "lpar_names=$partition" -F name | /usr/bin/sed 's/ /\%/g'`
		else
			[[ -s /usr/hmcrbin/lssyscfg ]] &&
				profile=`/usr/hmcrbin/lssyscfg -r prof -m \"$managed\" --filter "lpar_names=$partition" -F name | /usr/bin/sed 's/ /\%/g'`
		fi
	fi

	if [[ -n $profile ]]; then
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_SELECT_PROFILE} \
			'The following objects of type "profile" were found.  Please select one:\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		count=0
		input=0
		for i in `echo $profile | /usr/bin/sed 's/"//g'`
		do
			count=$count+1
			temp_profile=`echo $i | /usr/bin/sed 's/\%/ /g'`
			echo "$count. $temp_profile" | /usr/bin/tee -a ${INSTALLIOS_LOG}
			profile[$count]=$temp_profile
		done

		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_NUMBER} 'Enter a number: ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		while [[ $input -gt $count || $input -eq 0 ]]; do
			read input
			/usr/bin/echo "$input" >> ${INSTALLIOS_LOG}
		done

		profile=${profile[$input]}
	fi

	#
	# prompt for client's IP
	#
	response=""
	while [[ $response = "" ]]; do
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_CLIENT_IP} \
			'Enter the intended IP address of the client: ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		read response
		/usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
		ck_ipaddr "client" $response 
		if [[ $? -eq 0 ]]; then
			client=`/usr/bin/host ${response} | /usr/bin/cut -f3 -d' ' | /usr/bin/sed 's/,//g'`
			break
		else
			response=""
		fi
	done

	#
	# prompt for subnet mask
	#
	response=""
	while [[ $response = "" ]]; do
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_SUBNET_MASK} \
			'Enter the intended subnet mask of the client: ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		read response
		/usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
		ck_ipaddr "mask" $response
		if [[ $? -eq 0 ]]; then
			mask=$response
			break
		else
			response=""
		fi
	done

	#
	# prompt for client's gateway
	#
	response=""
	while [[ $response = "" ]]; do
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_CLIENT_GATEWAY} \
			'Enter the gateway of the client: ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		read response
		/usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
		ck_ipaddr "gateway" $response
		if [[ $? -eq 0 ]]; then
			gateway=$response
			break
		else
			response=""
		fi
	done

	#
	# prompt for speed
	#
	response=""
	while [[ $response = "" ]]; do
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_CLIENT_SPEED} \
			'Enter the speed for the client[100]: ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		read response
		/usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
		if [[ -n $response && $response != "100" ]]; then
			speed=$response
			ck_speed
			if [[ $? -eq 0 ]]; then
				break;
			else
				response=""
			fi
		else
			response="100"
			speed=$response
		fi
	done

	#
	# prompt for duplex
	#
	response=""
	while [[ $response = "" ]]; do
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_CLIENT_DUPLEX} \
			'Enter the duplex for the client[full]: ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		read response
		/usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
		if [[ -n $response ]] &&
		   [[ $response != "full" ]]; then
			duplex=$response
			ck_duplex 
			if [[ $? -eq 0 ]]; then
				break;
			else
				response=""
			fi
		else
			response="full"
			duplex=$response
		fi
	done

	#
	# prompt for VLAN priority
	#
	response=""
	while [[ $response = "" ]]; do
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_VLAN_PRI} \
			'Enter the VLAN tag priority for the client (0 to 7), 0=none [0]: ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
                read response
                /usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
                if [[ -n $response ]] &&
                   [[ $response != "0" ]]; then
                        vlan_pri=$response
			if [[ $vlan_pri -gt 0 ]] && [[ $vlan_pri -lt 8 ]]; then
                                break;
                        else
                                response=""
                        fi
                else
                        response="0"
                        vlan_pri=""
                fi
        done

        #
        # prompt for VLAN tag
        #
        response=""
        while [[ $response = "" ]]; do
                /usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_VLAN_TAG} \
                        'Enter the VLAN tag identifier for the client (0 to 4094), 0=none [0]: ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
                read response
                /usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
                if [[ -n $response ]] &&
                   [[ $response != "0" ]]; then
                        vlan_tag=$response
			if [[ $vlan_tag -gt 0 ]] && [[ $vlan_tag -lt 4095 ]]; then
                                break;
                        else
                                response=""
                        fi
                else
                        response="0"
                        vlan_tag=""
                fi
        done

	#
	# prompt for client network configuration
	#
	response=""
	while [[ $response = "" ]]; do
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_CONFIG_CLIENT_NET} \
			'Would you like to configure the network for the client after the installation [yes]/no? ' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		read response
		/usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
		if [[ $response = `/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_NO} 'no'` ]]; then
			config_net="-a preserve_net=no"
			break;
		elif [[ $response = `/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_YES} 'yes'` ]] || 
		     [[ -z $response ]]; then
			break;
		else
			response=""
		fi
	done

	#
	# Prompt for language
	#
	if [[ -z $SOURCE_AIXBASE ]]; then
		response=""
		while [[ $response = "" ]]; do
			/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_ENTER_LANG_FOR_LIC} \
				'Enter a language in which to view the license agreement, \
or leave blank and press enter to view the agreement later: ' \
				| /usr/bin/tee -a ${INSTALLIOS_LOG}
			read response
			/usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
			if [[ -n $response ]]; then
				locale=$response
				ck_locale 
				if [[ $? -ne 0 ]]; then
					/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_LANG_NOT_SUPPORTED} \
						'The language \"1%$s\" is not supported.\n' $response \
						| /usr/bin/tee -a ${INSTALLIOS_LOG}
					response=""
				fi
			else
				locale="en_US"
				break;
			fi
		
			if [[ -n response ]]; then
				do_licenses $locale
				if [[ $? -ne 0 ]]; then
					response=""
					while [[ $response = "" ]]; do
						/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_WANT_TO_CONTINUE} \
							'Do you want to continue? [yes]/no? ' \
							| /usr/bin/tee -a ${INSTALLIOS_LOG}
						read response
						/usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
						if [[ $response = `/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_NO} 'no'` ]]; then
							return 1
						elif [[ $response = `/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_YES} 'yes'` ]]; then
							break
						else
							response=""
						fi
					done
				else
					break
				fi
			fi
		done
	fi

	/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_VALUES_YOU_ENTERED} 'Here are the values you entered:\n' \
		| /usr/bin/tee -a ${INSTALLIOS_LOG}
	
	if [[ -n $managed ]]; then 
		/usr/bin/echo "managed_system = $managed"  | /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/echo "vio_partition = $partition" | /usr/bin/tee -a ${INSTALLIOS_LOG}
		/usr/bin/echo "profile = $profile"         | /usr/bin/tee -a ${INSTALLIOS_LOG}
	fi

	/usr/bin/echo "source = $source"      | /usr/bin/tee -a ${INSTALLIOS_LOG}
	/usr/bin/echo "client_addr = $client" | /usr/bin/tee -a ${INSTALLIOS_LOG}
	/usr/bin/echo "netmask = $mask"       | /usr/bin/tee -a ${INSTALLIOS_LOG}
	/usr/bin/echo "gateway = $gateway"    | /usr/bin/tee -a ${INSTALLIOS_LOG}
	/usr/bin/echo "speed = $speed"        | /usr/bin/tee -a ${INSTALLIOS_LOG}
	/usr/bin/echo "duplex = $duplex"      | /usr/bin/tee -a ${INSTALLIOS_LOG}
	/usr/bin/echo "vlan_pri = $vlan_pri"  | /usr/bin/tee -a ${INSTALLIOS_LOG}
	/usr/bin/echo "vlan_tag = $vlan_tag"  | /usr/bin/tee -a ${INSTALLIOS_LOG}

	response="continue"
	while [[ -n $response ]]; do
		/usr/bin/dspmsg -s ${MSG_SET} cmdnim.cat ${MSG_PROCEED_OR_CANCEL} \
			'Press enter to proceed or type Ctrl-c to cancel...\n' \
			| /usr/bin/tee -a ${INSTALLIOS_LOG}
		read response
		/usr/bin/echo "$response" >> ${INSTALLIOS_LOG}
	done
	
        ck_target || return 1
	create_resources || return 1

	#
	# Unmount sources if it was mounted.
	#
	unmount_source

	initiate_install
	rc=$?

	return $rc
}

# --------------------------------------------------------------------------- #
#
# NAME: 	installios
#
# FUNCTION: 	This script will setup the current machine as a NIM master and
#		create the resources from a VIO DVD or resources passed in from
#		command line arguments to install a client.  
#
#		If the NIM environment does not exit, then it will prompt
#		for the bos.sysmgt.nim.master fileset.  
#
# NOTES:	This script was made to run once and perform a clean up afterward.
#
# PARAMETERS:   None.
#
# RETURNS:      0 - success
#               1 - failure
#		2 - failure - filesystem already defined
#		3 - failure - installios invoked, require cleanup before continuing
#		4 - failure - don't invoke undo function
#
# --------------------------------------------------------------------------- #
trap undo 1
trap err_signal 2 11 15

if [[ -n $INSTALLIOS_DEBUG ]]; then
	set -x
	for i in $(typeset +f)
	do
		typeset -ft $i
	done
fi

# set parameters from command line
while getopts :D:L:NP:S:Ud:fg:h:i:l:m:np:r:s:tuvxV:Y: c
do
	case ${c} in
                D)      duplex=${OPTARG}                  ;;
                L)      location=${OPTARG}                ;;
		N)	accept_license=1                  ;;        
                P)      speed=${OPTARG}                   ;;
                S)      mask=${OPTARG}                    ;;
		U)	unconfig=1                        ;;
		V)      vlan_tag=${OPTARG}                ;;
		Y)	vlan_pri=${OPTARG}                ;;
                d)      source=${OPTARG}                  ;;
		f)	force=1                           ;;
                g)      gateway=${OPTARG}                 ;;
		h)	hmc=${OPTARG}	                  ;;
                i)      client=${OPTARG}                  ;;
                l)      locale=${OPTARG}                  ;;
                m)      mac_addr=${OPTARG}                ;; # mac_addr  
		n)      config_net="-a preserve_net=no"   ;; # no_net
                p)      partition=${OPTARG}               ;; # partition 
                r)      profile=${OPTARG}                 ;; # profile 
		s)	managed=${OPTARG}                 ;; # managed system
		t)	migrate=1                         ;;
		u)	cleanup=1                         ;;
		v)	verbose=1                         ;;
		x)	debug=1  
			set -x
			for i in $(typeset +f)
			do
				typeset -ft $i
			done
			;;

		\?)	# unknown option
			usage
			exit 1
			;;
	esac
done

# Initialize log
> ${INSTALLIOS_LOG}

if [[ -n ${cleanup} ]] && 
   [[ -n ${source} || -n ${client} || -n ${gateway} || -n ${mask} || -n ${speed} || -n ${duplex} || -n ${locale} || -n ${location} ]]; then
	/usr/bin/dspmsg -s ${ERR_SET} cmdnim.cat ${ERR_CLEANUP_FLAG_USAGE} \
		'%1$s: The cleanup flag is not allowed with any other flags except for force and unconfig.\n' \
		"${PROGRAM}" | /usr/bin/tee -a ${INSTALLIOS_LOG}
	usage
	exit 1
fi

if [[ $# -eq 0 ]]; then

	#
	# No arguments?!  Call the wizard!
	#
	wizard=1
	run_wizard
	rc=$?

	if [[ $rc -eq 3 ]]; then
		exit 3
	elif [[ $rc -eq 4 ]]; then
		exit 4
	elif [[ $rc -ne 0 ]]; then
		undo
		exit 1
	fi

elif [[ $cleanup -eq 1 ]]; then

	#
	# Call cleanup for -c flag.
	#
	cleanup
	rc=$?
else
	#
	# Check for valid command line arguments and create resources
	#
	check_attrs 
	rc=$?

	if [[ $rc -eq 3 ]]; then
		exit 3
	elif [[ $rc -eq 4 ]]; then
		exit 4
	elif [[ $rc -ne 0 ]]; then
		undo 
		exit 1
	fi

	create_resources
	if [[ $? -ne 0 ]]; then
		undo
		exit 1
	fi

	#
	# Unmount sources if it was mounted.  
	#
	unmount_source

	initiate_install 
	rc=$?
fi

exit $rc