#! /usr/bin/sh
#
# ident	"@(#)ssp_configspare.sh	1.1	97/02/06 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 "
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

#############################################################################
#
# 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
#
###########################################################################
save_n_copy()
{
	dst=${1}
	src=${2}
	mode=${3}
	owner=${4}
	if [ -f ${dst} ]; then
		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
		mv -f ${dst} ${dst}.0
	fi
	if [ "${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
}

#############################################################################
#
# 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
}

#############################################################################
#
# 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

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

	# 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=""
	#
	# 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
		MODIFY_PLATFORM_NAME=false
	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 the SPARE SSP workstation to control the host.
 
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 [ "${MODIFY_PLATFORM_NAME}" = "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 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
	rm -f /tmp/tmpcb$$
fi

#############################################################################
#
# Set up the tftpboot stuff.
#
#############################################################################
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


# 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

# get the inittab to run ssp_startup
cp /etc/inittab.main /etc/inittab

#
init q

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

This SPARE SSP is NOW configured to control the host.

This machine is the MAIN SSP now.


EOF
exit 0
