#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_cust_shroot.sh 1.3.1.2 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009,2011 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)50        1.3.1.2  src/bos/usr/lib/nim/methods/c_cust_shroot.sh, cmdnim, bos720 7/14/11 21:04:34
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/c_cust_shroot.sh
#		
#   ORIGINS: 83
#

# 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

#---------------------------- module globals    --------------------------------
REQUIRED_ATTRS="name hostname boot_info adpt_name server shared_root spot master type"
OPTIONAL_ATTRS="paging ring_speed cable_type home tmp log root_initialized resolv_conf netboot_kernel iplrom_emu dump nfs_domain"

# include common root/shared_root defines/functions
. ${NIM_METHODS}/dd_funcs

#*---------------------------- c_cust_shroot      ------------------------------
#
# NAME: c_cust_shroot
#
# FUNCTION:
#	customizes a shared root directory for a diskless client
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#		0							= no errors
#		>0							= failure
#
# OUTPUT:
#-----------------------------------------------------------------------------*/

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

# NIM initialization
nim_init

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

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

				# if host:dir format given, separate them
				# value can have 4 different formats:
				#    - host:dir:version
				#    - host:dir
				#    - dir:version
				#    - dir_or_name
				# separate according to the format used
				if [[ ${value} = ?*:/*:[3,4] ]]
				then
					loc_vers="${value#*:}"
					eval ${variable}=\"${loc_vers%:*}\"
					eval ${variable}_vers=\"${loc_vers#*:}\"
					eval ${variable}_host=\"${value%%:*}\"

				elif [[ ${value} = ?*:/* ]]
				then
					eval ${variable}=\"${value#*:}\"
					eval ${variable}"_host"=${value%:*}

				elif [[ ${value} = /*:[3,4] ]]
				then
					eval ${variable}=\"${value%:*}\"
					eval ${variable}_vers=\"${value#*:}\"
				else
					eval ${variable}=\"${value}\"
				fi
		;;

		q)		# show attr info
				cmd_what
				exit 0
		;;

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

		\?)	# unknown option
				error ${ERR_BAD_OPT} ${OPTARG}
		;;
	esac
done

# check for missing attrs
ck_attrs

# for non-/usr SPOTs, get the vrmf from the SPOT's attributes
if [[ "${spot}" != "/usr" ]]
then
        # extract SPOT name from SPOT location
        spotname=${spot%/usr}
        spotname=${spotname##*/}

        # use lsnim if we're the master, or nimclient -l if we're a client
        if [[ "${server}" = "${master}" ]]
        then
                lsnim="${LSNIM}"
        else
                lsnim="${NIMCLIENT} -l"
        fi

        # add vrmf to current env
        eval `${lsnim} -a version -a release -a mod -a oslevel_r \
                       ${spotname} 2>/dev/null | ${TAIL} -n +2 | ${TR} -d ' '`
fi

# for /usr SPOTs, or if the method above failed (which happens for example
# if the client is also configured as a master), fall back to the slower
# c_getlevel method
if [[ -z ${version} || -z ${release} || -z ${mod} || -z ${oslevel_r} ]]
then
        # add vrmf to current env
        eval `${C_GETLEVEL} -a type=spot -a source=${spot}`
fi

# prepare for interrupts
undo_on_interrupt=undo

# save the root path and root NFS version
root="${shared_root}"
root_vers="${shared_root_vers}"

# add the client's hostname to its specific /etc/hosts file
cat ${root}${HOSTS}.save > ${root}${HOSTS}.${name}
saved_name="${name}"
add_host ${hostname} || undo add_host

# initialize the client's /etc/filesystems
init_filesystems || undo init_filesystems

# initialize the client's /etc/swapspaces
init_swapspaces || undo init_swapspaces

# initialize the client's /etc/firstboot
init_firstboot || undo init_firstboot

# add entries in inittab for nimclient and recfgct
inittab || undo inittab

# initialize client's /etc/rhosts
init_rhosts || undo init_rhosts

# move the client specific files into the dedicated directory created in
# c_mkshroot.sh
for file in filesystems swapspaces firstboot hosts
do
	mv ${root}/etc/${file}.${name} ${root}/etc/.client_data/${file}.${name}
done

# set the nfs_domain
if [[ -n "${nfs_domain}" ]]
then
	init_local_domain
	mv ${root}/etc/nfs/local_domain.${name} ${root}/etc/.client_data/local_domain.${name}
fi


undo_on_interrupt=""

# success
exit 0

