#!/bin/bash
# Combined command for graceful shutdown

#Typical value is 
#Local CP (Slot 6, CP1): Active, Cold Recovered
#Remote CP (slot 5, CP0); Standby, Healthy
#HA enabled, Heartbeat Up, HA State synchronized

#For using command ledset:
#ledset slot_number LED_status
#LED_status: 2 standys for LED ON

PATH=$PATH:/bin

# Check RBAC permission on command
/fabos/libexec/rbac_check `basename $0`

if [ $? -ne 0 ]; then
    echo "Permission Denied!"
    exit 127
fi

HASHOW="$(hashow)"

swbd() {
    sed -n -e 's/^.\+\(SWBD[[:digit:]]\{1,\}\).\+$/\1/gp'
}

SWBD=`/sbin/sin | swbd`

case ${SWBD##SWBD} in
	'72' | '73' | '75' | '134' | '156' | '157' | '167')
	echo "##### This command is not supported on this platform #####"
	exit 2
	;;
esac

cp0_slot(){
    case ${SWBD##SWBD} in
    '62')
	printf 6
	;;
    '77')
	printf 4
        ;;
    '42' | '21')
	printf 5
	;;
    '165' | '166')
	printf 0
	;;
    *)
	printf 0
	;;
    esac
}

otherhost(){
    case ${SWBD##SWBD} in
    '62' | '77')
	printf 127.1.1.$((1+$(otherCpSlot)))
	;;
    '42' | '21')
	printf 10.0.0.$(otherCpSlot)
	;;
    '141' | '142')
	printf 127.1.$((10+$(otherCpSlot))).$((10+$(otherCpSlot)))
	;;
    '165' | '166')
	printf 127.3.1.$(otherCpSlot)
	;;
    *)
	printf 0.0.0.0
	;;
    esac
}


#
# Get othercp's id, returns:
# 0 - cp0
# 1 - cp1
#
othercp(){
	that=${HASHOW#*Remote CP \(Slot}
	that="${that%%,*}"
	printf $(($that - $(cp0_slot)))

}

localCpSlot(){
	that=${HASHOW#*Local CP \(Slot}
	that="${that%%,*}"
	echo $that
}

otherCpSlot(){
	that=${HASHOW#*Remote CP \(Slot}
	that="${that%%,*}"
	echo $that
}

#
# Stop an AP blade
# $1 is the slot number for the AP
#
stopblade(){
	bladeslot=$1
	bladeip=127.1.$((10+$(localCpSlot))).$((1+$bladeslot))

	echo "Shutting down blade in slot:"$bladeslot", IP addr:"$bladeip
	/usr/bin/rsh -n $bladeip /usr/bin/shutdown -h now
}

maybeMarathon(){
	#Now we are at the active CP, we need to check if the switch is marathon
	#we check if there are any AP exist
	#echo "this might be a marathon"

	slotshow | while read Slot Blade rest

	do
	    case "$Blade" in
		AP*)
		    stopblade $Slot
		    ;;
	esac
	done 
}

local_active(){
	switchdisable
	/fabos/link_sbin/hadisable
	maybeMarathon

	/fabos/link_rbin/ledset $(localCpSlot) 2
	/fabos/link_rbin/ledset $(otherCpSlot) 2

	echo "Shutting down OCP at:"$(otherhost)
	/usr/bin/rsh $(otherhost) /sbin/hainfo RebootReason "sysshutdown"
	/usr/bin/rsh $(otherhost) /usr/bin/shutdown -h now
	
	/sbin/hainfo RebootReason "sysshutdown"
	/usr/bin/shutdown -h now
	exit 1
	
}

local_standby(){
	echo "This command is not supported on the standby CP."

	exit 2

	#rsh $(otherhost) /fabos/link_sbin/hadisable 
	#/usr/bin/shutdown -h now
}

shutdownseq(){
	 /sbin/hainfo RebootReason "sysshutdown"
	 /fabos/link_rbin/ledset 0 2
	 /usr/bin/shutdown -h now
}

confirmNot_ha(){
	printf "This command will shutdown the operating systems on your switch.\n"
	printf "You are required to power-cycle the switch in order to restore operation.\n"
	printf "Are you sure you want to shutdown the switch [y/n]?"
	read response
	if [ "$response" = "y" ]; then
	    switchdisable
	    maybeSprint
	    shutdownseq
	fi
	if [ "$response" = "Y" ]; then
	    switchdisable
	    maybeSprint
	    shutdownseq
	fi	

	exit 1

}

maybeSprint(){
	case $(/sbin/sin) in
	    *SWBD46*| *SWBD83* ) /usr/bin/rsh 127.1.10.1 /usr/bin/shutdown -h now;;
	esac
}

confirmAc(){
	printf "This command will shutdown the operating systems on your switch.\n"
	printf "You are required to power-cycle the switch in order to restore operation.\n"
	printf "Are you sure you want to shutdown the switch [y/n]?"
	read response
	if [ "$response" = "y" ]; then
	    local_active
	fi
	if [ "$response" = "Y" ]; then
	    local_active
	fi	

	exit 1
}

unknow(){
	printf "this is a unknown system" >&2
	exit 1
}


case "$(hashow| ( read a; echo $a ))" in
	*Local*Active*) confirmAc ;;
	*Local*Standby*) local_standby ;;
	*"Not supported"*) confirmNot_ha ;;
	*) unknow ;;
esac

