#! /bin/ksh -p
#
# ident "@(#)cmd_reboot.ksh	1.1	03/01/23 SMI"
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#####################################################
#
# cmd_reboot
#
#	Reboot the node with a re-configuration reboot.   The
#	/etc/cluster/nodeid must exist, but the /etc/cluster/ccr/did_instances
#	table must not.
#
#	Possible exit codes:
#
#		2 error
#		(this command waits for reboot, if successful)
#
#####################################################

#####################################################
#
# Constant Globals
#
#####################################################

# Files
typeset -r SCRCMD_NODEID_FILE=/etc/cluster/nodeid
typeset -r SC_DID_INSTANCES=/etc/cluster/ccr/did_instances

# Program name
typeset -r PROG=${0##*/}

# Set the PATH
typeset -r SC_BINDIR=/usr/cluster/bin
typeset -r SC_BINDIRS=${SC_BINDIR}:/usr/cluster/lib/sc
PATH=${SC_BINDIRS}:/bin:/usr/bin:/sbin:/usr/sbin; export PATH

# I18N
typeset -x TEXTDOMAIN=TEXT_DOMAIN
typeset -x TEXTDOMAINDIR=/usr/cluster/lib/locale

#####################################################
#
# print_usage()
#
#       Print usage message to stderr
#
#####################################################
print_usage()
{
	echo "$(gettext 'usage'):  ${PROG}" >&2
}  

#####################################################
#
# Main
#
#####################################################
main()
{
	# Check arguments
	if [[ $# -ne 0 ]]; then
		print_usage
		return 2
	fi

	# Make sure that this node has been configured
	if [[ ! -f "${SCRCMD_NODEID_FILE}" ]]; then
		printf "$(gettext 'This node has not yet been configured.')\n"
		return 2
	fi

	# Make sure that we have not already tried to boot into the cluster
	if [[ -f "${SC_DID_INSTANCES}" ]]; then
		printf "$(gettext 'This node has already attempted to boot into the cluster.')\n"
		return 2
	fi

	# Make sure that it is a reconfiguration reboot
	touch /reconfigure

	# Reboot
	/usr/sbin/reboot

	# And, wait until we die from the reboot
	while true
	do
		sleep 60
	done
}

	main $*
	exit $?
