#! /bin/ksh -p
#
# ident "@(#)cmd_reboot.ksh	1.4	04/10/05 SMI"
#
# Copyright 2003-2004 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 after 20 seconds.   The 20 second delay gives us the
	# opportunity to cleanly return before rebooting.   Otherwise,
	# rsh can give us a problem.
	#
	(sleep 20; /usr/sbin/reboot) 0</dev/null 1>/dev/null 2>/dev/null &

	return 0
}

	main $*
	exit $?
