#! /usr/bin/sh +x
#
# ident	"@(#)ssp_configmain.sh	1.29	98/03/04 SMI"
#
# Copyright (c) 1996 by Sun Microsystems, Inc.
# All rights reserved.
#
# Script for configuring defaults for an SSP.
#
# It is normally only invoked by the /etc/init.d/sspdefs startup script
# if the file /.SSP_DEFAULTS exists.  This procedure interactively
# prompts the user for responses in setting up a default SSP environtment.

umask 033

# Usage: ssp_config
USAGE="Usage: $0 [cb]"
ENV=/etc/opt/SUNWssp/ssp_env.sh
HOSTSFILE=/etc/hosts
ETHERSFILE=/etc/ethers
RESERVED_PLAT=starfire
fatal()
{
	echo "ERROR! $*" >&2
	echo "ABORTING!" >&2
	exit 2
}
[ -f ${ENV} ] || fatal "Couldn't find ${ENV}!"
. ${ENV}
CBCONFIGFILE=${SSPVAR}/.ssp_private/cb_config
#CHANGE=false
CHANGE=true
CB_ONLY=false

#
# If any arguments, print out usage and exit.
#
if [ $# -eq 1 ]; then
	if [ "$1" != "cb" ]; then
                echo ${USAGE}
                exit 4
	else
		CB_ONLY=true
        fi
elif [ $# -gt 1 ]; then
	echo ${USAGE}
	exit 4
fi

#############################################################################
#
# Check that the user is root
#
#############################################################################
if [ -x /usr/bin/id ]
then
	eval `id | /usr/bin/sed 's/[^a-z0-9=].*//'`
	if [ "${uid:=0}" -ne 0 ]
	then
		fatal "You must be root to run $0."
	fi
else
	fatal "$0: can't check user id."
fi

###########################################################################
#
# save_n_copy
# 
# save dst file before copying from src file
# $1 - dst
# $2 - src
# $3 - mode
# $4 - owner
# $5 - cp_anyway - a flag indicating if copy is needed if the file does
#		   not exist
#
###########################################################################
save_n_copy()
{
	dst=${1}
	src=${2}
	mode=${3}
	owner=${4}
	cp_anyway=${5}
	if [ -f ${dst} ]; then
		diff ${dst} ${src} > /dev/null 2>&1
		result=$?
		if [ ${result} -eq 0 ]; then
			# no copy necessary
			return
		fi
		for i in 8 7 6 5 4 3 2 1 0; do
			n=`expr ${i} + 1`
			[ -f ${dst}.${i} ] && mv -f ${dst}.${i} ${dst}.${n}
		done
		cp -f ${dst} ${dst}.0
	elif [ "${cp_anyway}" = "no" ]; then
		return
	fi
	if [ -f "${src}" ]; then
		if cp ${src} ${dst} ; then
			chmod ${mode} ${dst}
			chown ${owner} ${dst}
		else
			fatal "Couldn't create ${dst}!"
		fi
	fi
}

#############################################################################
#
# the get_hostaddr function and server type code based upon plagarized code
# from a Sun script (rm_install_client)
# takes a hostname as an argument,
# set the HOST_ADDR variable, return status of match
#
#############################################################################
get_hostaddr()
{
	case ${NS} in
	NIS+)	line=`nismatch ${1} hosts.org_dir 2>/dev/null`
		status=$?
		HOST_ADDR=`echo ${line} | \
				(read cname name addr junk; echo $addr)`
		;;
	NIS)	line=`ypmatch ${1} hosts 2>/dev/null`
		status=$?
		HOST_ADDR=`echo ${line} | (read addr name junk; echo $addr)`
		;;
	*)	line=`grep "\<${1}\>" ${HOSTSFILE} 2>/dev/null`
		status=$?
		HOST_ADDR=`echo ${line} | (read addr name junk; echo $addr)`
		;;
	esac
	return $status
}

#############################################################################
#
# the get_etheraddr was mimic'ed with get_hostaddr
#
#############################################################################
get_etheraddr()
{
	case ${NS} in
	NIS+)	line=`nismatch ${1} ethers.org_dir 2>/dev/null`
		status=$?
		;; 
		
	NIS)	line=`ypmatch ${1} ethers 2>/dev/null`
		status=$?
		;;
	*)	line=`grep "\<${1}\>" ${ETHERSFILE} 2>/dev/null`
		status=$?
		;;
	esac
	if [ $status -ne 0 ]
	then
		echo "\nERROR : No ethernet address configured for ${1}\n"
		echo "\n Script aborting !!! }\n"
		exit 1
	fi
}
 
#############################################################################
#
# getipaddr function - prompts the user for an IP address for the host
# given as an argument and checks to make sure that the answer is at least
# of the form of an IP address.
#
#############################################################################
IP_RE="^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\$"
getipaddr()
{
	echo "I could not automatically determine the IP address of ${1}."
	IPSET=false
	while [ "$IPSET" = "false" ]
	do
		echo "Please enter the IP address of ${1}: \c"
		read A
		if [ -n "$A" ]
		then
			echo $A > /tmp/iptst.$$
			if egrep "${IP_RE}" /tmp/iptst.$$ > /dev/null
			then
				HOST_ADDR=$A
				IPSET=true
			else
				echo "\nERROR! \"$A\" is not an IP address!\n"
			fi
			rm -f /tmp/iptst.$$
		fi
	done
	fmt << EOF
You should make sure that this host/IP address is set up properly in
the /etc/inet/hosts file or in your local name service system.
EOF
}

#############################################################################
#
# hexip - function to convert a standard IP address into a hexidecimal
# IP address suitable for tftpboot.  This does no error checking so you'd
# better send it a valid IP address.  The result is echo'd so you will
# probably want to use this with back-quotes as in: HIP=`hexip 1.2.3.4`
#
#############################################################################
hexip()
{
	IP=$1
	HEXIP=""
	set -- `echo ${IP} | tr . ' '`
	for x in $* ; do
		h=`echo "obase=16\n${x}" | bc`
		if [ ${x} -lt 16 ]; then
			HEXIP="${HEXIP}0${h}"
		else
			HEXIP="${HEXIP}${h}"
		fi
	done
	echo ${HEXIP}
}

#############################################################################
#
# function to get a control board name.
#
#############################################################################
getcbname()
{
	while /bin/true; do
		echo "Do you have a control board $1? (y/n): \c"
		read A
		if [ "$A" = "y" ]; then
			break
		fi
		if [ "$A" = "n" ]; then
			case $1 in
				0) CB0_NAME="" ;;
				1) CB1_NAME="" ;;
			esac
			return
		fi
	done
	case $1 in
		0) CBNAME="${CB0_NAME}" ;;
		1) CBNAME="${CB1_NAME}" ;;
	esac
	OCBNAME="${CBNAME}"
	if [ -z "${CBNAME}" ]; then
		CBNAME=${PLATFORM_NAME}cb$1
	fi
	echo "Please enter the host name of the control board $1 [${CBNAME}]: \c"
	read A
	if [ -n "$A" ]; then
		CBNAME=$A
	fi
	if [ "${OCBNAME}" != "${CBNAME}" ]; then
		CHANGE=true
	fi
	case $1 in
		0) CB0_NAME="${CBNAME}" ;;
		1) CB1_NAME="${CBNAME}" ;;
	esac
}

#############################################################################
#
# parse cb_config file
#
#############################################################################
parse_cb_config()
{
	# try and parse current info
	PLATFORM_NAME=`cut -d: -f1 ${CBCONFIGFILE}`
	PLATFORM_TYPE=`cut -d: -f2 ${CBCONFIGFILE}`
	CB0_NAME=`cut -d: -f3 ${CBCONFIGFILE}`
	CB0_STATUS=`cut -d: -f4 ${CBCONFIGFILE}`
	CB1_NAME=`cut -d: -f5 ${CBCONFIGFILE}`
	CB1_STATUS=`cut -d: -f6 ${CBCONFIGFILE}`
	CB0_ADDR=""
	CB1_ADDR=""
}

#############################################################################
#
# Set up the tftpboot stuff.
#
#############################################################################
tftpboot_setup()
{
	if [ ! -d /tftpboot ]; then
		mkdir /tftpboot
		chmod 755 /tftpboot
	fi
	CBOBJS=${SSPOPT}/cbobjs
	cp ${CBOBJS}/flash_boot.ima /tftpboot/flash_boot.ima
	cp ${CBOBJS}/cbe.ima /tftpboot/cbe.ima
	cp ${SSPVAR}/.ssp_private/cb_port /tftpboot/cb_port
	if [ ${CB0_ADDR} ]; then
		CB0HEXIP=`hexip ${CB0_ADDR}`
		rm -f /tftpboot/${CB0HEXIP} 2> /dev/null 
		ln /tftpboot/cbe.ima /tftpboot/${CB0HEXIP}
		chmod 444 /tftpboot/${CB0HEXIP}
		rm -f /tftpboot/${CB0HEXIP}.cb_port 2> /dev/null 
		ln /tftpboot/cb_port /tftpboot/${CB0HEXIP}.cb_port
		chmod 444 /tftpboot/${CB0HEXIP}.cb_port
	fi
	if [ ${CB1_ADDR} ]; then
		CB1HEXIP=`hexip ${CB1_ADDR}`
		rm -f /tftpboot/${CB1HEXIP} 2> /dev/null 
		ln /tftpboot/cbe.ima /tftpboot/${CB1HEXIP}
		chmod 444 /tftpboot/${CB1HEXIP}
		rm -f /tftpboot/${CB1HEXIP}.cb_port 2> /dev/null 
		ln /tftpboot/cb_port /tftpboot/${CB1HEXIP}.cb_port
		chmod 444 /tftpboot/${CB1HEXIP}.cb_port
	fi
}

#############################################################################
#
# change cb_config file
#
#############################################################################
change_cb_config()
{
	if [ -s ${CBCONFIGFILE} ]; then
		parse_cb_config
		if [ -z "${PLATFORM_NAME}" ]; then
			echo "Missing platform name ${PLATFORM_NAME}."
		fi
		if [ -z "${PLATFORM_TYPE}" ]; then
			echo "Missing platform type."
		fi
		if [ -z "${PLATFORM_NAME}" -o -z "${PLATFORM_TYPE}" ]; then
			echo "Can not continue. Re-run ssp_config."
			exit 0
		fi
		if [ "${CB0_NAME}" -a "${CB0_STATUS}" = "P" -a -z "${CB1_STATUS}" ]; then
			PRI_CB=${CB0_NAME}
		elif [ "${CB1_NAME}" -a "${CB1_STATUS}" = "P" -a -z "${CB0_STATUS}" ]; then
			PRI_CB=${CB1_NAME}
		else
			PRI_CB=""
		fi
		while /bin/true ; do
			if [ "${PRI_CB}" != "" ] ; then
				if [ -n "${CB0_NAME}" -o -n "${CB1_NAME}" ]; then
					echo "\nPlatform name   = ${PLATFORM_NAME}"
					if [ ${CB0_NAME} ]; then
						echo "Control Board 0 = ${CB0_NAME} => ${CB0_ADDR}"
					else
						echo "Control Board 0 = [empty]"
					fi
					if [ ${CB1_NAME} ]; then
						echo "Control Board 1 = ${CB1_NAME} => ${CB1_ADDR}"
					else
						echo "Control Board 1 = [empty]"
					fi
					echo "Primary Control Board = ${PRI_CB}\n"
					if [ "${SSP_UPGRADE}" = "true" ]; then
						break
					fi
					echo "Is this correct? (y/n): \c"
					read A
					A=`echo "$A" | tr '[A-Z]' '[a-z]'`
					if [ "$A" = "y" -o "$A" = "yes" ]; then
						break
					fi
				fi
			fi

			getcbname 0
			if [ "${CB0_NAME}" ]; then
				if get_hostaddr ${CB0_NAME} ; then
					:
				else
					getipaddr ${CB0_NAME}
				fi
				CB0_ADDR=${HOST_ADDR}
			else
				CB0_ADDR=""
			fi
			getcbname 1
			if [ "${CB1_NAME}" ]; then
				if get_hostaddr ${CB1_NAME} ; then
					:
				else
					getipaddr ${CB1_NAME}
				fi
				CB1_ADDR=${HOST_ADDR}
			else
				CB1_ADDR=""
			fi
			if [ "{CB0_NAME}" != "" -a "${CB0_NAME}" = "${CB1_NAME}" ] ; then
				echo "\nControl Board 0 and Control Board 1 are identical.\n"
			elif [ "${CB0_NAME}" -a "${CB1_NAME}" ]; then
					while /bin/true; do
						echo "\nPlease identify the primary control board."
						echo "Is Control Board 0 [${CB0_NAME}] the primary? (y/n) \c"
						read A
						if [ "$A" = "y" ] ; then
							PRI_CB=${CB0_NAME}
							break;
						fi
						echo "Is Control Board 1 [${CB1_NAME}] the primary? (y/n) \c"
						read A
						if [ "$A" = "y" ]; then
							PRI_CB=${CB1_NAME}
							break;
						fi
					done
			elif [ "${CB0_NAME}" ]; then
				PRI_CB=${CB0_NAME}
			elif [ "${CB1_NAME}" ]; then
				PRI_CB=${CB1_NAME}
			else
				echo "\nNo control board has been identified.\n"
			fi
		done
		#############################################################################
		#
		# Set up the cb_config file.
		#
		#############################################################################
		if [ "${CHANGE}" = "true" ]; then
			if [ "${CB0_NAME}" = "${PRI_CB}" ]; then
				echo "${PLATFORM_NAME}:${PLATFORM_TYPE}:${CB0_NAME}:P:${CB1_NAME}:" > /tmp/tmpcb$$
			else
				echo "${PLATFORM_NAME}:${PLATFORM_TYPE}:${CB0_NAME}::${CB1_NAME}:P" > /tmp/tmpcb$$
			fi
			save_n_copy ${CBCONFIGFILE} /tmp/tmpcb$$ 644 ssp
			rm -f /tmp/tmpcb$$
		fi
		tftpboot_setup
	else 
		echo "Missing ${CBCONFIGFILE}."
		echo "Can not continue. Re-run ssp_config."
	fi
	exit 0
}

#############################################################################
#
# figure out what the server is using for its database(s)
#
#############################################################################
if [ -f /var/nis/NIS_COLD_START ]; then
	NS="NIS+"
elif ( ps -e | grep "[ /]nisd" > /dev/null ); then
	NS="NIS+"
elif ( ps -e | grep "[ /]ypbind" > /dev/null ); then
	NS="NIS"
else
	NS="local"
fi

#############################################################################
#
# Process cb option
#
#############################################################################
if [ "${CB_ONLY}" = "true" ]; then
	change_cb_config
fi


#############################################################################
#
# See if the cb_config file exists
#
#############################################################################
SSP_UPGRADE=false
if [ -s ${CBCONFIGFILE} ]
then
	# how many entries in file? assuming only a single line...
	# this needs to be expanded to handle multiple entries.
	parse_cb_config

	#
	# If cb_config does exist and we have a non-NULL platform name
	# we don't allow the user to change it.
	#
	if [ "${PLATFORM_NAME}" ]; then
		SSP_UPGRADE=true
	fi
	if [ -n "${CB0_NAME}" ]; then
		if get_hostaddr ${CB0_NAME} ; then
			CB0_ADDR=${HOST_ADDR}
		fi
		# This routine is not working properly now
		# so we are bypassing the ethers test.
		#get_etheraddr ${CB0_NAME} 
	fi
	if [ -n "${CB1_NAME}" ]; then
		if get_hostaddr ${CB1_NAME} ; then
			CB1_ADDR=${HOST_ADDR}
		fi
		#get_etheraddr ${CB1_NAME} 
	fi
else
	PLATFORM_NAME=""
	PLATFORM_TYPE=""
	CB0_NAME=""
	CB1_NAME=""
	CB0_STATUS=""
	CB1_STATUS=""
	CB0_ADDR=""
	CB1_ADDR=""
fi
if [ "${CB0_NAME}" -a "${CB0_STATUS}" = "P" -a -z "${CB1_STATUS}" ]; then
	PRI_CB=${CB0_NAME}
elif [ "${CB1_NAME}" -a "${CB1_STATUS}" = "P" -a -z "${CB0_STATUS}" ]; then
	PRI_CB=${CB1_NAME}
else
	PRI_CB=""
fi

#############################################################################
#
# Print a little intro.
#
#############################################################################
fmt << EOF

Beginning setup of this workstation to act as a MAIN or SPARE SSP.

EOF

#############################################################################
#
# Prompt the user for the platform type and control board names.  If
# they are already set, ask if they are correct and allow changes if
# they are not.
#
#############################################################################
while /bin/true ; do
	if [ "${PLATFORM_NAME}" != "" -a "${PRI_CB}" != "" ] ; then
		if [ -n "${CB0_NAME}" -o -n "${CB1_NAME}" ]; then
			echo "\nPlatform name   = ${PLATFORM_NAME}"
			if [ ${CB0_NAME} ]; then
				echo "Control Board 0 = ${CB0_NAME} => ${CB0_ADDR}"
			else
				echo "Control Board 0 = [empty]"
			fi
			if [ ${CB1_NAME} ]; then
				echo "Control Board 1 = ${CB1_NAME} => ${CB1_ADDR}"
			else
				echo "Control Board 1 = [empty]"
			fi
			echo "Primary Control Board = ${PRI_CB}\n"
			if [ "${SSP_UPGRADE}" = "true" ]; then
				break
			fi
			echo "Is this correct? (y/n): \c"
			read A
			A=`echo "$A" | tr '[A-Z]' '[a-z]'`
			if [ "$A" = "y" -o "$A" = "yes" ]; then
				break
			fi
		fi
	fi

	PLATNAMESET=false
	while [ "$PLATNAMESET" = "false" ]; do
		if [ -n "${PLATFORM_NAME}" ]; then
			echo ""
			echo "The platform name identifies the entire host machine to"
			echo "the SSP software. The platform name occupies a different"
			echo "name space than domain names(hostnames of bootable systems)."
			echo ""
			echo "Please enter the name of the platform this ssp will service [${PLATFORM_NAME}]: \c"
		else
			echo ""
			echo "The platform name identifies the entire host machine to"
			echo "the SSP software. The platform name occupies a different"
			echo "name space than domain names(hostnames of bootable systems)."
			echo ""
			echo "Please enter the name of the platform this ssp will service: \c"
		fi
		read A
		if [ -n "$A" ]; then
			if [ -n "${PLATFORM_NAME}" ]; then
				if [ "${PLATFORM_NAME}" != "$A" ]; then
					if [ -n "${OLDPLAT}" ]; then
						OLDPLAT="${OLDPLAT} ${PLATFORM_NAME}"
					else
						OLDPLAT="${PLATFORM_NAME}"
					fi
					CHANGE=true
				fi
			else
				CHANGE=true
			fi
			PLATFORM_NAME=$A
		fi
		if [ -n "${PLATFORM_NAME}" ]; then
			PLATNAMESET=true
		fi
		for r in ${RESERVED_PLAT}; do
			if [ "${PLATFORM_NAME}" = "$r" ]; then
				echo "\nERROR!  ${PLATFORM_NAME} is a reserved name!\n"
				PLATFORM_NAME=""
				PLATNAMESET=false
				break
			fi
		done
	done
	PLATFORM_TYPE=Ultra-Enterprise-10000
	getcbname 0
	if [ "${CB0_NAME}" ]; then
		if get_hostaddr ${CB0_NAME} ; then
			:
		else
			getipaddr ${CB0_NAME}
		fi
		CB0_ADDR=${HOST_ADDR}
		#get_etheraddr ${CB0_NAME} 
	else
		CB0_ADDR=""
	fi
	getcbname 1
	if [ "${CB1_NAME}" ]; then
		if get_hostaddr ${CB1_NAME} ; then
			:
		else
			getipaddr ${CB1_NAME}
		fi
		CB1_ADDR=${HOST_ADDR}
		#get_etheraddr ${CB1_NAME} 
	else
		CB1_ADDR=""
	fi
	if [ "{CB0_NAME}" != "" -a "${CB0_NAME}" = "${CB1_NAME}" ] ; then
		echo "\nControl Board 0 and Control Board 1 are identical.\n"
	elif [ "${CB0_NAME}" -a "${CB1_NAME}" ]; then
			while /bin/true; do
				echo "\nPlease identify the primary control board."
				echo "Is Control Board 0 [${CB0_NAME}] the primary? (y/n) \c"
				read A
				if [ "$A" = "y" ] ; then
					PRI_CB=${CB0_NAME}
					break;
				fi
				echo "Is Control Board 1 [${CB1_NAME}] the primary? (y/n) \c"
				read A
				if [ "$A" = "y" ]; then
					PRI_CB=${CB1_NAME}
					break;
				fi
			done
	elif [ "${CB0_NAME}" ]; then
		PRI_CB=${CB0_NAME}
	elif [ "${CB1_NAME}" ]; then
		PRI_CB=${CB1_NAME}
	else
		echo "\nNo control board has been identified.\n"
	fi
done

	#############################################################################
	#
	# Set up the data config file.  Rename old ones if we've changed
	# only if this is NOT an upgrade. Upgrade is controlled by the
	# SSP_UPGRADE variable.
	#
	#############################################################################
	if [ "${SSP_UPGRADE}" = "false" ]; then
		CONFIGDIR=${SSPVAR}/data/${PLATFORM_TYPE}/config
		if [ -n "${OLDPLAT}" ]; then
			for plat in ${OLDPLAT} ; do
				if [ -f ${CONFIGDIR}/${plat}.config ]; then
					echo "Removing ${CONFIGDIR}/${plat}.config"
					rm -f ${CONFIGDIR}/${plat}.config
				fi
			done
		fi
		if [ ! -s ${CONFIGDIR}/${PLATFORM_NAME}.config -o "${CHANGE}" = "true" ]; then
			if cp ${CONFIGDIR}/starfire.config ${CONFIGDIR}/${PLATFORM_NAME}.config ; then
				chown ssp ${CONFIGDIR}/${PLATFORM_NAME}.config
				chmod 644 ${CONFIGDIR}/${PLATFORM_NAME}.config
			else
				fatal "Couldn't create ${CONFIGDIR}/${PLATFORM_NAME}.config!"
			fi
		fi
	fi

	#############################################################################
	#
	# Set up the cb_config file.
	#
	#############################################################################
	if [ ! -s "${CBCONFIGFILE}" -o "${CHANGE}" = "true" ]; then
		if [ "${CB0_NAME}" = "${PRI_CB}" ]; then
			echo "${PLATFORM_NAME}:${PLATFORM_TYPE}:${CB0_NAME}:P:${CB1_NAME}:" > /tmp/tmpcb$$
		else
			echo "${PLATFORM_NAME}:${PLATFORM_TYPE}:${CB0_NAME}::${CB1_NAME}:P" > /tmp/tmpcb$$
		fi
		save_n_copy ${CBCONFIGFILE} /tmp/tmpcb$$ 644 ssp yes
		rm -f /tmp/tmpcb$$
	fi

	#############################################################################
	#
	# Add the hostname link in data.
	#
	#############################################################################
	DATA=${SSPVAR}/data/${PLATFORM_TYPE}
	if [ -d ${DATA} ]; then
		rm -f ${DATA}/${PLATFORM_NAME}
		if ln -s ./starfire ${DATA}/${PLATFORM_NAME} ; then
			:
		else
			fatal "Couldn't create ${DATA}/${PLATFORM_NAME}!"
	fi
else
	fatal "Couldn't find ${DATA}!"
fi

tftpboot_setup

#
# Is this an ssp spare ?
#
RESPONSE=false
while [ "$RESPONSE" = "false" ]; do
	echo "\nAre you currently configuring the MAIN SSP? (y/n) \c"
	read A
	if [ "$A" = "y" ]; then
		RESPONSE=true
		cp /etc/inittab.main /etc/inittab
	else
		if [ "$A" = "n" ]; then
			cp /etc/inittab.spare /etc/inittab
			#init q
			# kill all SSP daemons if any
			${SSPOPT}/bin/ssp_terminate
			# Don't make the spare as a tftp boot server
			if grep '^tftp' /etc/inetd.conf > /dev/null ; then
ed /etc/inetd.conf << EOF > /dev/null
/^tftp
s/^tftp/#tftp/
.
w
q
EOF
			fi
			rm -f /.SSP_DEFAULTS
			echo "\nSPARE SSP configuration completed.\n"
			exit
		fi
	fi
done
#
# uncomment tftp from the inetd.conf file if necessary.
if grep '^#tftp' /etc/inetd.conf > /dev/null ; then
	ed /etc/inetd.conf << EOF >/dev/null
/^#tftp
s/^#//
.
w
q
EOF
else
	if grep '^tftp' /etc/inetd.conf > /dev/null ; then
		: already done.
	else
		# Uh-oh!  Where's the tftp line?!
		# Let's add it near the end.
		ed /etc/inetd.conf << EOF >/dev/null
$
i
tftp	dgram	udp	wait	root	/usr/sbin/in.tftpd	in.tftpd -s /tftpboot
.
w
q
EOF
	fi
fi

#
#############################################################################
#
# Create /var/opt/SUNWssp/etc   /var/opt/SUNWssp/etc/${PLATFORM_NAME}
# and /var/opt/SUNWssp/etc/${PLATFORM_NAME}/cbobjs directories. Also copy
# over edd configure files for this platform.
#############################################################################

tmpl=${SSPVAR}/.ssp_private/templates/${PLATFORM_TYPE}
p_etc=${SSPVAR}/etc/${PLATFORM_NAME}
cbobjs_dir=$p_etc/cbobjs
PLATFORM_ERC_FILE=edd.platform.erc
DOMAIN_ERC_FILE=edd.domain.erc
EDD_ERC_FILE=edd.erc
EDD_EMC_FILE=edd.emc

if [ ! -d ${p_etc} ]; then 
	mkdir -p ${p_etc} 
	chmod 755 ${p_etc}
	chown ssp ${p_etc}
fi 

if [ ! -d ${cbobjs_dir} ]; then 
	mkdir -p ${cbobjs_dir} 
	chmod 755 ${cbobjs_dir}
	chown ssp ${cbobjs_dir}
fi 

# if a copy of the erc file exists, save it off 
save_n_copy ${p_etc}/${EDD_ERC_FILE} ${tmpl}/${PLATFORM_ERC_FILE} 640 ssp yes
 
# if a copy of the emc file exists, save it off 
save_n_copy ${p_etc}/${EDD_EMC_FILE} ${tmpl}/${EDD_EMC_FILE} 640 ssp  yes

#
# Need to take care of domain erc files too
#
for i in `ls ${p_etc}`; do
	newdir=${p_etc}/${i}
	if [ -d ${newdir} ]; then
		save_n_copy ${newdir}/${EDD_ERC_FILE} ${tmpl}/${DOMAIN_ERC_FILE} 640 ssp no
	fi
done

#
# Remove globalconst.tcl and let it rebuild by edd
#
rm -f ${SSPVAR}/etc/${PLATFORM_NAME}/cbobjs/globalconst.tcl

#
# Make sure bids.tcl and thermcaldata.tcl are there
# (bugs 4054660 4067723)
touch ${cbobjs_dir}/bids.tcl
chmod 644 ${cbobjs_dir}/bids.tcl
chown ssp ${cbobjs_dir}/bids.tcl

touch ${cbobjs_dir}/thermcaldata.tcl
chmod 644 ${cbobjs_dir}/thermcaldata.tcl
chown ssp ${cbobjs_dir}/thermcaldata.tcl

#
# Remove /etc/opt/SUNWssp/snmp/*.idy and let it rebuild by scotty
#
rm -f /etc/opt/SUNWssp/snmp/*.idy

#
# Clean up and exit.
#
rm -f /.SSP_DEFAULTS
cat << EOF


MAIN SSP configuration completed


EOF
exit 0
