#!/bin/sh
#
#    Copyright (c) 2015 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   reboot
#    Module name: fabos/src/utils/sys
#
#    This script is wrapper to the traditional /sbin/reboot command 
#    so that /fabos/sbin/ will contain this wrapper to 
#    1, Allow admin account to use it. admin account should not have 
#	/sbin/reboot (linux reboot) access in its PATH. If this reboot was
#       invoked from dual CP actiove CP side, A prompt will show up for user
#       confirmation.
#    2, FOS can call this function with -s option to skip the prompt. 
#    3,	FOS should call this function with -r option for reboot reason.
#	The reboot reason will be displayed in raslog meassge HAM-1004
#	after switch boot up.
#	The reboot reason for -r option has to be one word without spcae.
#	example: reboot -r ChassisConfig
#
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/fabos/bin:/fabos/sbin

chassis_info=`getchassisconfig`

ischassis=`echo $chassis_info | \
        sed -n -e 's/.*Chassis based system: //gp' | \
        sed -n -e 's/ .*//gp'`

OPTION=$*
#get reboot reason
REASON="Reboot"
FAST=""
YLD=""
SKIP_PROMPT="No"
OPTS="FALSE"
CREBOOT="No"
while getopts r:sfc c > /dev/null
do
    OPTS="TRUE"
	case $c in
	r)	REASON=$OPTARG
		;;
	s) 	SKIP_PROMPT="Yes"
	    ;;
	f) 	FAST="-f"
		YLD="Yes"
	    ;;
	c) CREBOOT="Yes"
	    ;;
   	*)
	   echo "usage: reboot"
	   exit 1
	   ;;
	esac
done
if [ $# -eq 0 ]
then
	OPTS="TRUE"
fi
if [ "$OPTS" = "FALSE" ]
then
	echo "$0: illegal option -- $*"
	echo "usage: reboot"
	exit 2
fi
#OPTION=$*
#remove -s and -r option
#echo $REASON
#OPTION=`echo $OPTION | /bin/sed "s/-r[ \t]\{1,\}[^ ]\{1,\}//;s/[ \t]*-s[ \t]\{1,\}//;s/[ \t]-s$//"`
OPTION=$FAST
#echo $OPTION

# #############################
# This is added to support the forced reset of the Zues FPGA, note that
# cpu reset (as done by this reboot) is not sufficient.
# The details of how and when are hidden in the driver which has exported
# this /proc/node
#

# swbd
#
# Retrieve the current system platform name, of the form "SWBDn", where n
# is cardinal number, assuming a sin/hinv input stream.
#
swbd() {
    /bin/sed -n -e 's/^.*\(SWBD[[:digit:]]\{1,\}\).\+$/\1/gp'
}

# Determine the system platform identifier.
SWBD=$(sin | swbd 2> /dev/null)

case ${SWBD##SWBD} in

    '76' )
	zeus_fault=`cat /proc/fabos/blade/0/info | grep "blade fault reason" | grep 53`
	if [ "$zeus_fault" ]
	then
		/sbin/fpga_update.sh
	fi

	if [ -e /etc/init.d/zeus_reconfig ]
	then
		/etc/init.d/zeus_reconfig start
		/etc/init.d/zeus_reconfig stop
	fi

    ;;

esac

#
# Only prompt user on active cp on a chassis based system
# this reboot request was not called from "chassisReboot"
#
	hashow_cmd="hashow -l"

	case "$(hashow -l)" in
	*ACTIVE*HA_ENABLED*)
		is_active="Yes"
		is_hadisabled="No"
		;;
	*STANDBY*HA_ENABLED*)
		is_active="No"
		is_hadisabled="No"
		;;
	*STANDBY*HA_DISABLED*)
		is_active="No"
		is_hadisabled="Yes"
		;;
	*ACTIVE*HA_DISABLED*)
		is_active="Yes"
		is_hadisabled="Yes"
		;;
	esac

if [ "$ischassis" = "Yes" -a "$CREBOOT" = "No" ]; then

	if [ "$is_active" = "Yes" -a "$SKIP_PROMPT" = "No" ]; then
		if [ "$is_hadisabled" = "No" ]; then
			# text here revised per defect 78391
			echo "Warning: This command is being run on a control processor (CP)"
			echo "based system and will cause the active CP to reboot."
			if [ -n $OPTION ] && [ "$OPTION" != "-f" ]; then
				echo "This will cause disruption to all traffic in this chassis."
			fi
		else
			echo "Warning: This command is being run on a control processor (CP)"
			echo "based system. Because HA is disabled, it will cause both active"
			echo "and the standby CP to reboot."
			echo "HA will be enabled after boot up."
		fi

		read -p "Are you sure you want to reboot the active CP [y/n]?" in
		if [ "$in" != "y" -a "$in" != "Y" ]; then
			exit
		fi
	fi

	# If this is this is active CP and HA is currently then reboot
	# the chassis and retain mastership.
	if [ "$is_active" = "Yes" -a "$is_hadisabled" = "Yes" ]; then
		/fabos/cliexec/chassisreboot -m -f
	fi
else
	if [ "$SKIP_PROMPT" = "No" ]; then
		echo "Warning: This command would cause the switch to reboot"
		echo "and result in traffic disruption."

		read -p "Are you sure you want to reboot the switch [y/n]?" in
		if [ "$in" != "y" -a "$in" != "Y" ]; then
			exit
        else
			#
			# The devices that are connected to the embedded switches take more time to
			# detect that the switch has been rebooted. This is because, the OLS is sent
			# only when the chip is initialized during the next reboot session. So, issue
			# a switchdisable to bring down the ports explicitly before the reboot to
			# allow the hosts to detect the reboot scenario much earlier.
			#
            if [ "$SWBD" = "72" ]; then
                /fabos/cliexec/switchdisable
            fi  
		fi

	fi
fi

#record boot reason
/sbin/hainfo RebootReason "$REASON"
#record a raslog for reboot.
/fabos/cliexec/errlogtest -i RAS-1007 -f
sync
if [ -e /mnt/etc/audit_up_dwn ]; then
	echo "Downgrade"
	/fabos/sbin/auditdump -d
	/bin/rm /mnt/etc/audit_up_dwn 2> /dev/null
fi
#Executing platform yield which will shutdown ASIC dma on active CP
#we will do at run level 6 if not force reboot
if [ "$YLD" = "Yes" -a "$is_active" = "Yes" ]; then
		/fabos/cliexec/platformyield
fi
sleep 3
sync; sync
echo "Rebooting! `/bin/date`"
/sbin/reboot $OPTION

