#!/sbin/sh
#
# Copyright (c) 1998, by Sun Microsystems, Inc.
# All rights reserved.
#
#ident "@(#)u4ftsplitl1 1.4     99/04/23 SMI" 
#
# S41u4ftsplitl1.sh
#

if [ `uname -i` != "SUNW,Ultra-4FT" ]
then
	# Not an ft1800
	exit 0
fi


#
# Set $PATH and command names
# Called always
#
set_useful_vars() {

	LOSER_FILE_A="/.loser_A"
	LOSER_FILE_B="/.loser_B"
	PLAT_LIB=/platform/SUNW,Ultra-4FT/lib

	#
	# set PATH
	#
	PATH=/sbin:/usr/bin:/usr/sbin:$PLAT_LIB; export PATH

	#
	# define used commands
	#
	U4FTMODE=u4ftmode
	VXSERIAL=vxserial
	VXDCTL=vxdctl

	#
	# check used commands
	#
	type $U4FTMODE > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		# cant determine current mode, leave
		exit 0
	fi

	type $VXSERIAL > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		# vxserial missing, leave
		exit 0
	fi

	type $VXDCTL > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		# vxdctl missing, leave
		exit 0
	fi
}

#
# cleanup rcS.d
# Called always
#
cleanup_rcS() {

	if [ -f /etc/rcS.d/.S20pnet.sh ]; then
		mv /etc/rcS.d/.S20pnet.sh /etc/rcS.d/S20pnet.sh
	fi
	if [ -f /etc/rcS.d/.S30rootusr.sh ]; then
		mv /etc/rcS.d/.S30rootusr.sh /etc/rcS.d/S30rootusr.sh
	fi
}

#
# exit if not loser at first or second boot
# Set $mode, $BOOTNO, $LOSER_NAME, $OLD_HOSTNAME
#
exit_if_not_loser() {

	#
	# u4ftmode prints
	# C if system is configured combined
	# A if system is configured split and current CPUset is A
	# B if system is configured split and current CPUset is B
	#
	mode=`$U4FTMODE`
	if [ "X$mode" = "XC" ]; then
		# combined mode: leave
		exit 0
	fi

	if [ "X$mode" = "XA" ]; then
		ln=`cat $LOSER_FILE_A 2>/dev/null`
		if [ ! -z "$ln" ]; then
			#
			# loser is side A, and it is currently running
			#
			LOSER_SIDE="A"
			eval `grep LOSER_NAME $LOSER_FILE_A`
		fi
	elif [ "X$mode" = "XB" ]; then
		ln=`cat $LOSER_FILE_B 2>/dev/null`
		if [ ! -z "$ln" ]; then
			#
			# loser is side B, and it is currently running
			#
			LOSER_SIDE="B"
			eval `grep LOSER_NAME $LOSER_FILE_B`
		fi
	else
		#
		# unrecognised mode
		#
		exit 0
	fi

	if [ -z "$LOSER_NAME" ]; then
		#
		# split mode, not loser side or not first or second loser boot
		#
		echo "RUNNING IN SPLIT MODE"
		exit 0
	fi

	#
	# if we are here, it is the first or second losers' boot
	#

	#
	# determine if it is the first or second boot
	#
	OLD_HOSTNAME=`cat /etc/nodename`
	if [ "$OLD_HOSTNAME" = "$LOSER_NAME" ]; then
		BOOTNO=second
	else
		BOOTNO=first
	fi
}

#
# Assign the new hostname to the new side:
# replace current hostname with loser hostname in Solaris conf files
#
change_hostname() {

	cmd="eval sed -e 's/$OLD_HOSTNAME/$LOSER_NAME/g'"

	for i in /etc/nodename\
	 	/etc/net/ticlts/hosts\
	 	/etc/net/ticots/hosts\
	 	/etc/net/ticotsord/hosts
	do
		cat $i | $cmd > $i.new
		mv -f $i.new $i
	done

	cf=`ls /etc/hostname.* 2> /dev/null`
	if [ ! -z "$cf" ]; then
		for i in $cf
		do
			hn=`cat $i`
			if [ ! -z "$hn" ]; then
				if [ "$hn" = "$OLD_HOSTNAME" ]; then
					echo $LOSER_NAME > $i
				fi
			fi
		done
	fi
}


#
# do Veritas licensing
#
do_veritas() {

	echo "YOU MUST ENTER THE VERITAS LICENCE KEYS HERE"
	$VXSERIAL -c < /dev/console
	$VXSERIAL -c < /dev/console
	$VXDCTL license
}

#
# sync_well
#
sync_well() {
	sync; sync; sync
}

#
# reboot
#
and_reboot() {
	echo "*** REBOOT THE SYSTEM ***"
	uadmin 2 1
}

#
# MAIN
#

#
# Ensure that there is not a stale splitd lock around
# otherwise u4ftsplitd will never start
#

rmdir /etc/.split_start_lock > /dev/null 2>&1

cleanup_rcS
set_useful_vars
exit_if_not_loser
if [ "$BOOTNO" = "first" ]; then
	echo "FIRST LOSER BOOT"
	do_veritas
	change_hostname
	sync_well
	and_reboot
else
	echo "SECOND LOSER BOOT"
fi
