#!/bin/sh
#
#
#    Copyright (c) 1996-2001 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   systemVerification
#    Module name: diag/scripts
#
#    This file perofrms switchburnin on all active switch blades.
#
# NAME
#    systemverification - Supports these activities
#    1. Set burnin mode on all active switch blades
#    2. Allows parameter editing for switchburnin
#
# SYNOPSIS
#    systemverification [ diagsetcycle ]
#
# AVAILABILITY
#    swt_mfg
#
# DESCRIPTION
#    This command will manage the burnin activity. It contains and
#    manages the parameters needed this activity. It also cointains
#    the list of activities to perform during a single burnin run. 
#    It has the processing loop to execute the commands in the list.
#
#    The mode becomes active as soon as this command is executed.
#    It does not require a reboot to take effect.
#
# OPERANDS
#
#    "diagsetcycle"    This operand will trigger activity related to 
#                      editing the avialable parameters active during
#                      the burnin activity. The user will be prompted 
#                      to modify each parameter in the odrer specified 
#                      in the this script. The current value, default 
#                      value, and description of the parameter are 
#                      displayed. If the user does not want to modify 
#                      the value, then a <cr> is recognized as 
#                      "hold current value". Any other value for this
#                      parameter is ignored.
#
##################################
# Signal handler part of script
##################################
systemVerification_cleanup()
{
    /bin/echo "systemVerification: Stopping activity"
    if [ "$switch0_blades" != "" ]
    then
	export FABOS_SWITCHNO=0
	/fabos/sbin/diagstopburnin
    fi
    if [ "$switch1_blades" != "" ]
    then
	export FABOS_SWITCHNO=1
	/fabos/sbin/diagstopburnin
    fi

    /bin/echo "systemVerification: Outputting Status"
    /fabos/sbin/burninstatus
    exit
}

##################################
# Main body of script
# DIAGSETCYCLE_CAPABLE
##################################
# Load common functions
# Should be first to ensure the library 
# functions are available for use
##################################
home="/fabos/share"
util="diagcommon.sh"
ok=0
for f in "./$util" "$FABOSHOME/share/$util" "$home/$util" ; do
	if [ -r $f ] ; then
		. $f
		ok=1
		break;
	fi
done
if [ $ok -ne 1 ] ; then
	err "Could not locate $util"
	exit 3
fi
switch1_blades=""
################################
# get sys info for multi blade
################################
multiBlade=`isMultiBlade`
export syntax="$0 burninScript"

################################
# Set switch to always available and see if there are blades installed
################################
export FABOS_SWITCHNO=0
switch0_blades=`getAvailSlots`

if [ $multiBlade = TRUE ]
then
    export FABOS_SWITCHNO=1
    switch1_blades=`getAvailSlots`
fi

if [ "$switch0_blades" = "" -a "$switch1_blades" = "" ]
then
    /bin/echo "Could not find any thing to test."
fi

################################
# Make sure switches are in Offline state before proceeding
################################
if [ "$switch0_blades" != "" ]
then
    export FABOS_SWITCHNO=0
    if [ "`switchshow | /bin/grep switchState | /bin/grep Online`" != "" ]
    then
	/bin/echo "Can not proceed on switch 0, switch is Online."
	/bin/echo "Please use switchdisable to take the switch Offline so the fabric is not disturbed."
	exit 1
    fi
fi

if [ "$switch1_blades" != "" ]
then
    export FABOS_SWITCHNO=1
    if [ "`switchshow | /bin/grep switchState | /bin/grep Online`" != "" ]
    then
	/bin/echo "Can not proceed on switch 1, switch is Online."
	/bin/echo "Please use switchdisable to take the switch Offline so the fabric is not disturbed."
	exit 1
    fi
fi

################################
# Do the diagsetcycle if requested
################################
if [ "$1" = diagsetcycle ]
then
    if [ "$switch0_blades" != "" ]
    then
	export FABOS_SWITCHNO=0
	/fabos/sbin/diagsetcycle switchburnin.sh -label SYSTEMVERIFICATION
	/fabos/sbin/diagsetcycle switchburnin.sh
	################################
	# Copy the diagsetcycle to other switch if there is one
	################################
	if [ "$switch1_blades" != "" ]
	then
	    /bin/echo "systemVerification: Aligning burnin parameters between the switches"
	    args=`/fabos/bin/configshow 2>/dev/null | \
		/bin/grep "diag.switchburnin" | \
		/usr/bin/cut -d"." -f3 | \
		/usr/bin/tr ":" " " | \
		/bin/sed "s/^/-/g"`
	    export FABOS_SWITCHNO=1
	    /fabos/sbin/diagsetcycle switchburnin.sh -default -label SYSTEMVERIFICATION $args > /dev/null
	fi
    else
	if [ "$switch1_blades" != "" ]
	then
	    export FABOS_SWITCHNO=1
	    /fabos/sbin/diagsetcycle switchburnin.sh
	fi
    fi
fi

/bin/echo "systemVerification: burnin parameters."
/fabos/sbin/diagsetcycle switchburnin.sh -show

################################
# Arm the burnin
################################
/bin/rm -f /tmp/burnin_PID.*
if [ "$switch0_blades" != "" ]
then
    export FABOS_SWITCHNO=0
    /fabos/sbin/diagsetburnin /fabos/share/switchburnin.sh
fi
if [ "$switch1_blades" != "" ]
then
    export FABOS_SWITCHNO=1
    /fabos/sbin/diagsetburnin /fabos/share/switchburnin.sh
fi

################################
# Set abort path before the scripts are started
################################
trap systemVerification_cleanup INT TERM

################################
# Start the switchburnin.sh on all active blades
################################
if [ "$switch0_blades" != "" ]
then
    export FABOS_SWITCHNO=0
    for slot in $switch0_blades
    do
	export FABOS_SLOTNO=$slot
	/fabos/share/switchburnin.sh -slot $slot | /bin/grep Command &
    done
fi

if [ "$switch1_blades" != "" ]
then
    export FABOS_SWITCHNO=1
    for slot in $switch1_blades
    do
	export FABOS_SLOTNO=$slot
	/fabos/share/switchburnin.sh -slot $slot | /bin/grep Command &
    done
fi
################################
# Wait for all blades to complete
################################
sleep 30
more=TRUE
while [ $more = TRUE ]
do
	# Terminate all burnin activity found on this switch
	more=FALSE
	for file in /tmp/burnin_PID.*
	do
		sleep 120
		switch_no=`/bin/echo $file | /usr/bin/cut -d"." -f2`
		slot_no=`/bin/echo $file | /usr/bin/cut -d"." -f3`
		if [ "$slot_no" != "" -a "$switch_no" != "" ]
		then
			if [ $multiBlade = TRUE ]
			then
			    _slot_message="Switch $switch_no, Slot $slot_no"
			else
			    _slot_message="Switch"
			fi
			# if the child process exists, then kill it
			target=`/bin/cat $file | /usr/bin/cut -d" " -f1`
			if [ "`/bin/ps -p $target | /bin/grep -w $target`" != "" ]
			then
			    more=TRUE
			    /bin/echo "systemVerification: $_slot_message is ACTIVE."
			else
			    /bin/echo "systemVerification: $_slot_message is complete."
			fi
		fi
	done
done

################################
# Output results
################################
/bin/echo "systemVerification: Outputting Status"
/fabos/sbin/burninstatus
