#!/sbin/sh
#
# Copyright (c) 1998, by Sun Microsystems, Inc.
# All rights reserved.
#
#ident "@(#)u4ftsplitl2 1.5     99/01/19 SMI" 
#
# S87u4ftsplitl2.sh
#

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


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

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

	#
	# set PATH
	#
	PATH=/usr/bin:/usr/sbin:$PLAT_LIB; export PATH
	
	#
	# define used commands
	#
	U4FTMODE=u4ftmode
	VXDCTL=vxdctl

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


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 $BOOTNO, $LOSER_NAME, $OLD_HOSTNAME
# Called at first and second boot
#
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`
			eval `grep CMS_HOME $LOSER_FILE_A`
			eval `grep WINNER_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`
			eval `grep CMS_HOME $LOSER_FILE_B`
			eval `grep WINNER_NAME $LOSER_FILE_B`
		fi
	else
		#
		# unrecognised mode
		#
		exit 0
	fi

	if [ -z "$LOSER_NAME" ]; then
		#
		# not loser side
		#
		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
}


#
# do Veritas ops
# Called at second boot
#
do_veritas() {

	if [ ! -z "$LOSER_NAME" ]; then
		$VXDCTL hostid $LOSER_NAME
	fi
}

#
# remove the loser flag
# Called at second boot
#
rem_loser_flag() {

	if [ -f $LOSER_FILE_A ]
	then
		rm $LOSER_FILE_A
	fi
	if [ -f $LOSER_FILE_B ]
	then
		rm $LOSER_FILE_B
	fi
}

#
# MAIN
#
cleanup_rcS
set_useful_vars
exit_if_not_loser
if [ "$BOOTNO" = "first" ]; then
	echo "FIRST LOSER BOOT"
else
	do_veritas
	rem_loser_flag
fi
