#!/bin/sh
#
#ident	"@(#)bringup.sh	1.30	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.
#
# test env
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
if test -z "${CRAY_SSPLOGS}"
then
	echo "No CRAY_SSPLOGS environment variable. Warning!"
else
	# test if an sd_post is running from some other script
	if test -f ${CRAY_SSPLOGS}/${CRAY_HOSTNAME}/sd_post.lock
	then
		postpid=`cat ${CRAY_SSPLOGS}/${CRAY_HOSTNAME}/sd_post.lock`
   		ps -ef | grep sd_post | grep ${postpid} > /dev/null 2>&1
   		if test $? -eq 0
   		then
			echo "A POST is running, BRINGUP EXITING!! "
			exit 1
		else
	                logger -p local0.info -t "bringup:(CRAY_HOSTNAME:${CRAY_HOSTNAME}): " " No POST is running"
	                logger -p local0.info -t "bringup:(CRAY_HOSTNAME:${CRAY_HOSTNAME}): " " Removing ${CRAY_SSPLOGS}/${CRAY_HOSTNAME}/sd_post.lock file"
			rm -f ${CRAY_SSPLOGS}/${CRAY_HOSTNAME}/sd_post.lock > /dev/null 2>&1 

		fi
	fi
fi

# test if another bringup is alive
if test -d "${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}"
then
	touch ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/bringup.pid 
else
	echo "${CRAY_HOSTNAME} is not configured properly.  Exiting !!"
fi
# 
# get the previous bringup pid
#
bringupid=`cat ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/bringup.pid`
#
# see if it is alive
#
if test -n "$bringupid" 
then
   ps -ef | grep bringup | grep -v grep |  grep ${bringupid} > /dev/null 2>&1
   if test $? -eq 0
   then
       echo " Another bringup command is running, sorry exiting! " 
       exit 1
   fi
fi
#
# put the new bringup id in the bringup.pid file.
#
echo $$ > ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/bringup.pid

# This bringup is free to proceed
force=0
FORCE=0
for a in $*
do
	arg=`echo $a | sed -e 's/\(..\).*/\1/'`
	case $arg in
	-f)
	     force=1
		;;
	-F)
	     # this is used when called from 
	     # reboot_host or arbstop_restart
	     FORCE=1
		;;
	*)
		;;
	esac
done

if test ${FORCE} -eq 0
then
    # This path will not execute if call (-F) came from reboot_host 
    # and arbstop_restart. 
    
	# check if host is UP 
    	check_host
	if test $? -eq 0
	then 
	    # Host id UP
            if test ${force} -eq 0
	    then 
		echo "Host is active : use bringup -f, sorry exiting! "
		exit 1
            else 
	        echo "WARNING : Host is active, bringup -f may corrupt filesystems "
	        echo " "
	        echo "Do you really wish to continue ? (y/n)"
	        read selection
	        if test "${selection}" = "y"
	        then
	            echo "Is this bringup executed because of a Hung Host ?(y/n)"
	    	    read selection
	            if test "${selection}" = "y"
	            then
	                logger -p local0.info -t "bringup:(CRAY_HOSTNAME:${CRAY_HOSTNAME}): " " DOWN  System Hung" 
		    fi
	        else
	            echo "Bringup Exiting"
		    exit 1
	        fi
	    fi
        fi
fi

echo "Bringup proceeds"


POSTARGS=""; verbose=0; force=0; unknowns=1; unk=""
BOOTARGS=""

for a in $*
do
	arg=`echo $a | sed -e 's/\(..\).*/\1/'`
	case $arg in
	-v)
		verbose=1;
		POSTARGS="${POSTARGS} -v70";
		;;
	-f)
		force=1; 
		;;
	-F)
		force=1;
		;;
	-g)
		# This is strictly for POST to maintain a log file.
		POSTARGS="${POSTARGS} -g"
		;;
	-p)
		pnum=`echo $a | sed -e 's/..\(.*\)/\1/'`;
		POSTARGS="${POSTARGS} -p${pnum}";
		;;
	-L)
		# This is used to have post send everyting to syslog.
		POSTARGS="${POSTARGS} -s -v10";
		;;
	-l)
		# This is used to have post send everyting to syslog.
		level=`echo $a | sed -e 's/..\(.*\)/\1/'`
		POSTARGS="${POSTARGS} -l${level}";
		;;
	-X)
		# Have sd_post look at a different blacklist file.
		blacklistfile=`echo $a | sed -e 's/..\(.*\)/\1/'`
		POSTARGS="${POSTARGS} -X${blacklistfile}"
		;;
	*)
		# assumed boot args.  These must be handed to obp_helper.
		case ${a} in
			# known args to obp_helper... just pass them on 
		-C | -D | -A | on | off )
			BOOTARGS="${BOOTARGS} ${a}"
			;;
		*)
			# unknown argument. 
			# keep track of them and append them after an --
			# to obp_helper (its using optarg and just
			# doesn't know any better
			unk="${unk} ${a}"
			unknowns=1
		esac 
		
		;;
	esac
done
if test ${unknowns} -ne 0
then
	BOOTARGS="${BOOTARGS} -- ${unk}"
fi

#
# kill any obp_helpers floating around
#
touch ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/obp_helper.pid 
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
        kill -KILL $obp_helper_pid > /dev/null 2>&1
    fi
fi

# remove the arbstop indicator if present.
rm -f ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/arbstop

# run full post and remember boot processor
if [ ! -r ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/machine_config ]
then
    if [ ! -r ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/physical_host ]
    then
        echo "The physcial_host file does not exist, aborting..."
        exit 9
    fi
    PHYSICAL_HOST=`cat ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/physical_host`
    ENTRY=`grep "^${CRAY_HOSTNAME} " ${CRAY_SSPROOT}/etc/${PHYSICAL_HOST}/machine_config | sed "s/://"`
    set -- $ENTRY
    POSTARGS="${POSTARGS} -Il$2";
fi
 
# reset non redlisted parts of the entire system
sysreset
 
sd_post ${POSTARGS}
bootproc=$?
if [ ${bootproc} -lt 0 -o  ${bootproc} -gt 63 ]
then 
    echo "sd_post FAILED, exiting!! "
    exit 1
fi
rm -f ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/bootproc
echo ${bootproc} > ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/bootproc
if test $? -eq 1 
then
	echo " Could not create file : ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/bootproc"
   	exit 1
else
	bootproc_test=`cat ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/bootproc`
	if test -z "$bootproc_test"
	then
		echo "${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/bootproc empty"
		echo "file system might be full"
		exit 1
	fi
fi


obp_helper -q -m `cat  ${CRAY_SSPROOT}/etc/${CRAY_HOSTNAME}/bootproc` \
	 ${BOOTARGS} </dev/null >/dev/null 2>&1 &

exit 0

