#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_prepspot.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009 
# 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 
# @(#)30 1.2 src/bos/usr/lib/nim/methods/c_prepspot.sh, cmdnim, bos720 4/16/09 11:06:16
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/c_prepspot.sh
#
#   ORIGINS: 27
#
#   (C) COPYRIGHT International Business Machines Corp. 2009
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#

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

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

#---------------------------- module globals    --------------------------------
REQUIRED_ATTRS="location spot"
OPTIONAL_ATTRS=""
location=""
spot=""
remove=""
verbose=""

#---------------------------- ck_fileset_requirements --------------------------------
#
# NAME: ck_fileset_requirements
#
# FUNCTION:
# 	Check the SPOT to see if the fileset requiements are met.
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#               calls error on failure
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#               parameters:
#               global:
#
# RETURNS: (int)
#               0 = success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function ck_fileset_requirements {

        typeset rc=0
	typeset found=""
	typeset missing=""
	typeset REQUIRED_FILESET="krb5.client clic.rte modcrypt.base expect tk tcl"

	# setup_chroot_env uses the "location" variable to access the SPOT resource.
	chroot=""
	setup_chroot_env

	# Ensure that the server's libraries mounted into the SPOT will be used.
	${SET_CHROOT_LIBPATH}

	fileset=`${chroot} ${LSLPP} -Lc | ${CUT} -d: -f1 | /usr/bin/uniq | \
                 ${EGREP} 'krb5.client$|clic.rte$|modcrypt.base$|expect$|expect.base$|tk$|tk.base$|tcl$|tcl.base$'`

	for i in $REQUIRED_FILESET; do
		found="false"
		for filesetname in `echo ${fileset} | /usr/bin/tr ' ' '\n'`; do
			if [[ $filesetname = "${i}"* ]]; then
				found="true"
				break
			fi
		done
		if [[ $found = "false" ]]; then
			missing="$i $missing"
		fi
	done

	if [[ -n $missing ]]; then
		# 0042-505 c_prepspot: The following software images are missing in the spot
		# 	and are needed before this operation can continue:
		# 	krb5.client, clic.rte, modcrypt.base, expect, tcl, tk
		error ${ERR_SPOT_FILESET_REQUIRED} "${missing}"
	fi      

	${UNSET_CHROOT_LIBPATH}

	return 0

} # end of ck_fileset_requirements

#---------------------------- cp_credential --------------------------------
#
# NAME: cp_credential
#
# FUNCTION:
#	Copy the Kerberos credential from the NIM master into the SPOT.
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#		0 = success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function cp_credential {

	if [[ ! -d $location/lpp/bos/inst_root/etc/nfs ]]; then
		${MKDIR} $location/lpp/bos/inst_root/etc/nfs
		if [[ $? -ne 0 ]]; then
			# 0042-175 c_prepspot: An unexpected result was returned by the
			# 	"<cp_credential>" command:
			#	<mkdir <SPOT>/lpp/bos/inst_root/etc/nfs>
			error ${ERR_CMD} ${cp_credential} "${MKDIR} $location/lpp/bos/inst_root/etc/nfs"
		fi
	fi

	if [[ ! -d $location/lpp/bos/inst_root/etc/krb5 ]]; then
		${MKDIR} $location/lpp/bos/inst_root/etc/krb5
		if [[ $? -ne 0 ]]; then
			# 0042-175 c_prepspot: An unexpected result was returned by the
			#	"<cp_credential>" command:
			# 	<mkdir <SPOT>/lpp/bos/inst_root/etc/krb5>
			error ${ERR_CMD} ${cp_credential} "${MKDIR} $location/lpp/bos/inst_root/etc/krb5"
		fi
	fi

	for files in `${LS} /etc/nfs/*`; do
		${CP} $files $location/lpp/bos/inst_root/etc/nfs
		if [[ $? -ne 0 ]]; then
			# 0042-175 c_prepspot: An unexpected result was returned by the
			#       "<cp_credential>" command:
			#       <cp <files> <SPOT>/lpp/bos/inst_root/etc/nfs>
			error ${ERR_CMD} ${cp_credential} "${CP} $files $location/lpp/bos/inst_root/etc/nfs"
		fi
	done

	for files in `${LS} /etc/krb5/*`; do
		${CP} $files $location/lpp/bos/inst_root/etc/krb5
		if [[ $? -ne 0 ]]; then
			# 0042-175 c_prepspot: An unexpected result was returned by the
			# 	"<cp_credential>" command:
			# 	<cp <files> <SPOT>/lpp/bos/inst_root/etc/krb5>
			error ${ERR_CMD} ${cp_credential} "${CP} $files $location/lpp/bos/inst_root/etc/krb5"
		fi
	done

	return 0

} # end of cp_credential

#---------------------------- rm_credential --------------------------------
#
# NAME: rm_credential
#
# FUNCTION:
#	Remove Kerberos credential (cp_credential - copied from NIM master 
#	to SPOT) from the SPOT.
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#               parameters:
#               global:
#
# RETURNS: (int)
#               0 = success
#
# OUTPUT:
#-------------------------------------------------------------------------------
function rm_credential {

	if [[ -d $location/lpp/bos/inst_root/etc/nfs ]]; then
		${RM} -r $location/lpp/bos/inst_root/etc/nfs
		if [[ $? -ne 0 ]]; then
			# 0042-175 c_prepspot: An unexpected result was returned by the
			#       "<rm_credential>" command:
			#       <rm -r <SPOT>/lpp/bos/inst_root/etc/nfs>
			error ${ERR_CMD} ${rm_credential} "${RM} -r $location/lpp/bos/inst_root/etc/nfs"
		fi
	fi

	if [[ -d $location/lpp/bos/inst_root/etc/krb5 ]]; then
		${RM} -r $location/lpp/bos/inst_root/etc/krb5
		if [[ $? -ne 0 ]]; then
			# 0042-175 c_prepspot: An unexpected result was returned by the
			#       "<rm_credential>" command:
			#       <rm -r <SPOT>/lpp/bos/inst_root/etc/krb5>
			error ${ERR_CMD} ${rm_credential} "${RM} -r $location/lpp/bos/inst_root/etc/krb5"
		fi
	fi

	return 0

} # end of rm_credential

#---------------------------- c_prepspot --------------------------------
#
# NAME: c_prepspot
#
# FUNCTION:
#	Call by m_bos_inst to copy the Kerberos credential from the 
#	NIM master to the SPOT resource (SPOT server must be the NIM master).
#	
#	Call by m_dealloc_spot (-u) to remove the Kerberos credential in
#	the SPOT resource.
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#	0  = SPOT installed
#	1  = error encountered - message on stderr
#
# OUTPUT:
#  stderr = NIM error message detailing what went wrong with execution.
#-------------------------------------------------------------------------------
# signal processing
trap cleanup 0
trap err_signal 1 2 11 15

# NIM initialization
nim_init

# initialize local variables
typeset rc=0

# set parameters from command line
while getopts a:quv c; do
	case ${c} in
		a) 	# validate the attr ass
			parse_attr_ass "${OPTARG}"

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

		q)	# show attr info
			cmd_what
			exit 0
			;;

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

		u)	# remove credentials
			remove=TRUE
			;;

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

# check for missing attrs
ck_attrs

if [[ -n $remove ]]; then
	rm_credential
else
	ck_fileset_requirements
	cp_credential
fi

# all done
exit 0
