#!/bin/sh
#
#ident	"@(#)reboot_host.sh	1.25	95/03/27 CRS"
#
# Copyright (c) 1993, by Cray Research Superservers, Inc.
#
# This command is used by hostmon's strategy agents to 
# perform a quick reboot when requested by openboot.

# First verify that environmental dependencies are met

full_bringup()
{
    	rm -f ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/arbstop
    	bringup -F ${non_local_args} -L -g -l17
    	exit $?
}

if test -z "${CRAY_SSPROOT}" 
then
	echo "No CRAY_SSPROOT environment variable. Cannot proceed."
	exit 9
fi
if test -z "${CRAY_HOSTNAME}"
then
	echo "No CRAY_HOSTNAME environment variable. Cannot proceed."
	exit 9
fi

#
# non_local_args contains argument that are not used by reboot_host.
# These arguments are to be passed onto subsequent programs/scripts.
#
non_local_args=
do_full_bringup=0
for a in $*
do
	arg=`echo $a | sed -e 's/\(..\).*/\1/'`
	case $arg in
	-B)
		# full bringup
		do_full_bringup=1
		;;
	*)
		non_local_args="${non_local_args} $arg"
		;;
	esac
done

if [ ${do_full_bringup} -eq 1 ]
then
    	logger -p local0.info -t "reboot_host:(CRAY_HOSTNAME:${CRAY_HOSTNAME}):" "Requested full bringup."
	full_bringup
fi

# get the boot processor from our last boot.
if [ -f ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/bootproc ]
then
    bootproc=`cat ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/bootproc`
else
   logger -p local0.info -t "reboot_host:(CRAY_HOSTNAME:${CRAY_HOSTNAME}):" "bootproc file missing. Trying full bringup."
    full_bringup
fi
#
if [ ${bootproc} -ge 0 -a ${bootproc} -le 63 ]
then
    # Run quick minimal testing POST using the config previously 
    # established .An exception to this is if we are rebooting after 
    # an arbstop dump
    if test ! -f ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/arbstop
    then
        if [ -f ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/obp_helper.pid ]
	then
            obp_helper_pid=`cat ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/obp_helper.pid` 
	    if test -n "${obp_helper_pid}"
	    then
	        ps -ef | grep obp_helper | grep -v grep |  grep ${obp_helper_pid} > /dev/null 2>&1
    	        if test $? -eq 0
    	        then
   	            logger -p local0.info -t "reboot_host:(CRAY_HOSTNAME:${CRAY_HOSTNAME}):" "killing obp_helper"
  	            kill -KILL ${obp_helper_pid} > /dev/null 2>&1
    	        else
   	            logger -p local0.info -t "reboot_host:(CRAY_HOSTNAME:${CRAY_HOSTNAME}):" "obp_helper is NOT running "
    	        fi
	    fi

	else
   	    logger -p local0.info -t "reboot_host:(CRAY_HOSTNAME:${CRAY_HOSTNAME}):" "obp_helper.pid file missing "
	fi
	logger -p local0.info -t "reboot_host:(CRAY_HOSTNAME:${CRAY_HOSTNAME}):" "quick reboot starting."
	sd_post -s -Q${bootproc} -g
	bootproc=$?
	logger -p local0.info -t "reboot_host:(CRAY_HOSTNAME:${CRAY_HOSTNAME}):" "bootproc $bootproc"
	if [ ${bootproc} -ge 0  -a ${bootproc} -le 63 ] 
    	then
            logger -p local0.info -t "reboot_host:(CRAY_HOSTNAME:${CRAY_HOSTNAME}):" "spawning a new obp_helper ."
	    echo ${bootproc} > ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/bootproc
	    obp_helper -A on ${non_local_args} -m ${bootproc} &
	    exit 0
	fi
	logger -p local0.info -t "reboot_host:(CRAY_HOSTNAME:${CRAY_HOSTNAME}):" "quick reboot failed. Trying full bringup."
     fi
fi

# Either arbstop or failure of quick post or bootproc was invalid
full_bringup
