#!/bin/sh
#
#    Copyright (c) 2001-2012 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
POSTGRES_VERSION=`ls /usr/local/postgresql | cut -d '/' -f1`
PATH=$PATH:/usr/local/postgresql/${POSTGRES_VERSION}/bin
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/fabos/lib:/usr/local/postgresql/${POSTGRES_VERSION}/lib

#Logging Support
LOG_FILE="/var/log/reboot.log"

#Init Log file
initLog() {
	if [ -f "$LOG_FILE" ]; then
		/bin/mv $LOG_FILE $LOG_FILE.save
	fi

	echo "Reboot Script invoked at `/bin/date +%D-%T` with $@"> $LOG_FILE
}

printLog() {
	echo "`/bin/date +%D-%T` $1" >> $LOG_FILE
}

initLog $@

chassis_info=`getchassisconfig`

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

SWBD=`/sbin/sin 2>/dev/null | /bin/grep SWBD | /usr/bin/cut -d ' ' -f 2 | /usr/bin/cut -d ',' -f 1`

#get reboot reason
REASON="Reboot"
FAST=""
SKIP_PROMPT="No"
CREBOOT="No"
GREBOOT="No"
SKIP_REBOOT="No"
while getopts r:sfgkc c > /dev/null
do
	case $c in
	r) REASON=$OPTARG
	   ;;
	s) SKIP_PROMPT="Yes"
	    ;;
	f) FAST="-f"
	   ;;
	c) CREBOOT="Yes"
	   ;;
	g) GREBOOT="Yes"
	   ;;
	k) SKIP_REBOOT="Yes"
	   ;;
   	*)
	   echo "usage: reboot"
	   exit 1
	   ;;
	esac
done

#OPTION=$*
#remove -s and -r option
#OPTION=`echo $OPTION | /bin/sed "s/-r[ \t]\{1,\}[^ ]\{1,\}//;s/[ \t]*-s[ \t]\{1,\}//;s/[ \t]-s$//"`
OPTION=$FAST

#
# Only prompt user on active cp on a chassis based system
# this reboot request was not called from "chassisReboot"
#
if [ "$ischassis" = "Yes" -a "$CREBOOT" = "No" ]; then
	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 [ "$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."
		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
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
		fi
	fi
fi

#Invoke AL ShutDown state to shutdown the AL applcation
printLog "AL applcation shut down."
/fabos/sbin/alt -iShutDown
printLog "Done with AL Shut Down."

#
#For vLAG support, call to send out notification of pending reboot to all
#nodes in the fabric.
#
printLog "Invoking vcs node disable."
/fabos/cliexec/fab_vcs fab_vcsd_node_disable
sleep 2
printLog "Done with node disable."

if [ "$ischassis" = "Yes" -a "$is_active" = "Yes" ]; then
	printLog "Disable HA."
	/fabos/sbin/hashutdown
	printLog "Ha hs been shutdown."
fi

#Invoke nos_debug script to do necessary data collection
printLog "Collecting NOS Debug Data."
/fabos/bin/nos_debug -r
printLog "Done with collecting nos debug data."

#Perform graceful DB shut down for Dcmd & Ccmd
printLog "Performing graceful DB shutdown."
/fabos/bin/shutdowndcmdb
printLog "DB has been shutdown gracefully."


#record a raslog for reboot.
printLog "Triggering RAS-1007."
/fabos/cliexec/errlogtest -i RAS-1007
printLog "Done."

# the delay serve two purposes
# 1. haSyncStop need 5 sec for peer MM timeout
# 2  file system flush before reboot
printLog "Invoking FS Sync"
sync; sleep 3; sync; sleep 3;

#
# shut down underling ASIC port when pizzabox is going down
#
if [ "$GREBOOT" = "No" ]; then
	if [ "$ischassis" != "Yes" ]; then
		printLog "Disabling Slot."
		/fabos/sbin/slotdisable 0
		printLog "Done with Slot disable."
		sleep 1
	fi
fi

# Temporary work-around to fix the reload issue in castor.
# Standby needs to perform its pre-reboot operations without GOS reset
# Later Active will perform partman -r for system wide reset.
if [ "$SKIP_REBOOT" = "Yes" ]; then
	printLog "Skipping Reboot."
	exit 0
fi

#record boot reason
/sbin/hainfo RebootReason "$REASON"

if [ -x /bin/partman ]; then
	if [ "$GREBOOT" = "Yes" ]; then
		/sbin/reboot -f
	elif [ "x$OPTION" = "x-f" ]; then
		/bin/partman -r
	fi
fi
/sbin/reboot $OPTION
