#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_mkroot.sh 1.20.5.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1993,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 
# @(#)37        1.20.5.2  src/bos/usr/lib/nim/methods/c_mkroot.sh, cmdnim, bos720 7/15/11 03:46:31
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/c_mkroot.sh
#		
#
#   ORIGINS: 27 83
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1993, 1996
#   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

#---------------------------- module globals    --------------------------------
REQUIRED_ATTRS="name hostname boot_info adpt_name server 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_mkroot           ------------------------------
#
# NAME: c_mkroot
#
# FUNCTION:
#		creates a root directory for a diskless or dataless 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
				# the attribute 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

# populate the root directory
pop_root

# get the vrmf of the SPOT for use later, and add it to current env.
${C_GETLEVEL} -a type=spot -a source=${spot} \
	> ${TMPDIR}/__src_level.$$
. ${TMPDIR}/__src_level.$$

# link /unix to the kernel indicated by the 
# client's netboot_kernel attribute
# -- Only 64 bit kernel support beyond version 5.
if [[ "${netboot_kernel}" = "64" ]] || (( ${version} > 5 ))
then
	/usr/bin/ln -fs /usr/lib/boot/unix_64 ${root}/unix

elif (( (( ${version} == 5 )) && (( ${release} > 2 )) ))
then
	/usr/bin/ln -fs /usr/lib/boot/unix_mp ${root}/unix

elif [[ "${netboot_kernel}" = "mp" ]]
then
	/usr/bin/ln -fs /usr/lib/boot/unix_mp ${root}/unix

else
	/usr/bin/ln -fs /usr/lib/boot/unix_up ${root}/unix
fi

# lets add soft links for /opt 
# release 5.x - rpm stuff
if (( ${version} > 4 )) 
then
	/usr/bin/rm -fr ${root}/opt
	/usr/bin/ln -s /usr/lpp/bos/inst_root/opt ${root}
fi


# now, sync the root with the SPOT before we edit any files in the root
${C_SYNC_ROOT} -s -a location=${spot_host:+${spot_host}:}${spot} \
	-a installp_flags=-aOr -a filesets=all ${root} 2>${ERR} || \
	warning_from_cmd ${C_SYNC_ROOT}

# prepare for interrupts
undo_on_interrupt=undo

# add the client's hostname to its /etc/hosts file
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 entry in inittab for nimclient
inittab || undo inittab

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

# set the nfs_domain
[[ -n "${nfs_domain}" ]] && init_local_domain

undo_on_interrupt=""

# success
exit 0

