#!/bin/sh
#
#
#    Copyright (c) 1996-2000 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#
# NAME
#      diagsetburnin - initializes the blade for a burnin run.
# 
# SYNOPSIS
#      diagsetburnin [ --slot [ slotno ]] [ script | -current ]
# 
# AVAILABILITY
#      admin
# 
# DESCRIPTION
#      This  sets up the blade burnin parameters for the registered
#      burnin script. The name of burnin script  can  be  set  with
#      burninname  command  alternatively. The burnin will start at
#      the next run of POST(Power-On Self-Test) on  the  designated
#      blade(s).
# 
#      The errors and activity logs are stored in non-volatile mem-
#      ory.   The  activity  log  of  the  script   is   saved   in
#      /var/log/scriptname.slot.log.    The   errors  produced  are
#      available from burninerrshow command on a per  blade  basis.
#      When power cycles occur, the burnin activity is restarted at
#      the test that was interrupted  at  the  time  of  the  power
#      cycle.  This  command  does  not  require  a  reboot to take
#      effect.
# 
# OPTIONS
#      --slot [ slotno ]
#                Specify which slot to update.  If  slotno  is  not
#                specified  then  the current slot will be assumed.
#                You can set the current slot by execution of  set-
#                slot  command.  If this option is not specified at
#                all, then all slots on the switch  are  setup  for
#                burnin.
# 
#      script    Specify the name of the burnin script to run.
# 
#      -current  Set  the  name  of burnin script to current burnin
#                script.
# 
# EXAMPLES
#      > diagsetburnin --slot 1 -current
#      existing script is: /fabos/share/switchess.sh
#      Burnin mode is Enabled.
#      Removing all log files in /var/log for slot 1
#      Slot 1 burnin name is now /fabos/share/switchess.sh
#      Config update Succeeded
# 
# SEE ALSO
#      burninerrshow(1d),  burninname(1d),  diagsetcycle(1d),  set-
#      slot(1d)
# 
# NOTES
#      Boards  must  be installed prior to running this command and
#      diagsetcycle must be run prior to diagsetburnin if you  wish
#      to use both commands.
# 

#
#
# Load library -- must be first.
#

#	Set the name of the script running, and save the initialized variables
#     $1 = slot to operate on
#     $2 = name of the script operating on the slot
#
do_a_slot()
{
	if [ $multiBlade = TRUE ]
	then
	    slot_message=" for slot $1"
	else
	    slot_message=""
	fi
        # Strip the .sh from the script name
        BaseFileName=${2%%.*}

	# Find path to log files
	_tmp_dir=`getConfig diag.mode.burnin.logpath $STRING "/var/log"`
	if [ ! -d $_tmp_dir ]
	then
		_tmp_dir="/var/log"
	fi
	_log_path=`getConfig diag.$BaseFileName.LogFilePath $STRING "$_tmp_dir"`
	if [ ! -d $_log_path ]
	then
		_log_path=$_tmp_dir
	fi
	/bin/echo "Removing all log files in $_log_path$slot_message."
	# Remove the log and PID of the last burnin run
	/bin/rm -f $_log_path/*.$1.log
				 
	/bin/rm -f /tmp/burnin_PID.$1
	# Remove log file on other CP if available
	_cmd=`commandPath haShow`
	if [ "`$_cmd | /bin/grep Standby`" != "" ]
	then
		/bin/echo "Removing all log files in $_log_path $slot_message on other CP"		
		other_slot=`$_cmd | /bin/grep Remote | /usr/bin/cut -d" " -f4 | /usr/bin/cut -d"," -f1`
		/usr/bin/rsh 10.0.0.$other_slot "/bin/rm -f /$_log_path/*.$1.log"
		/usr/bin/rsh 10.0.0.$other_slot "/bin/rm -f /tmp/burnin_PID.$1"
	fi

	# setup for the next burnin run
	burninSetName $2 $1 -noupdate
	saveBladeParams $1
}

if [ ! $?FABOSHOME ]
then
	export FABOSHOME=/fabos
fi

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
multiBlade=`isMultiBlade`

if [ $multiBlade = TRUE ]
then
syntax="`/bin/basename $0` [ --slot [ slotno ]] [ script \| -current ]"
else
syntax="`/bin/basename $0` [ script \| -current ]"
fi

#
# Process the arguments
# Get slot number
#

def_slot=0

if [ $# = 0 ]
then
	multiple_slots=true
else
	s=`/usr/bin/expr "$1" : "[-]*slot"`
	if [ "0" -ne "$s" ]
	then
		x=`/usr/bin/expr "$2" : "\([0-9]\+\)"`
		if [ -z $x ] ; then
		   ans="`isValidSlot $FABOS_SLOTNO`"
		   if [ "$ans" = TRUE ] ; then
			slot=$FABOS_SLOTNO
		   else
			 slot=$def_slot
		   fi  

		else
		   ans="`isValidSlot $x`"
		   avail=`getAvailSlots`
		   if [ "$ans" = TRUE ] ; then
		        slot=$x
		   else 
		        err "$x is an invalid slot number"
			if [ $multiBlade = TRUE ] ; then
			     /bin/echo "Available slots are: $avail."
			fi
		        exit 4
		    fi
		fi
		FileName="$3"
		multiple_slots=false
	else
		FileName=$1
		multiple_slots=true
	fi
	
fi
#
# Setup the desired script if specified
#
updatescript=false
if [ "$FileName" != "" ]
then
	if [ "$FileName" = "-current" ]
	then
		FileName=`getConfig diag.script.SWITCH.BURNIN $STRING /fabos/share/switchburnin.sh`
		if [ "$FileName" = "-none-" ]
		then
			updatescript=true
			FileName=/fabos/share/switchburnin.sh
		fi
		/bin/echo "existing script is: $FileName"
	else
		updatescript=true
	fi
else
	_num_choice=`getNumAvailableScripts DIAGSETCYCLE_CAPABLE -config`
	_answer=-1
	while [	$_answer -lt 0 -o $_answer -gt $_num_choice ]
	do
		/bin/echo "Choices are:"
		showAvailableScripts DIAGSETCYCLE "diag.script.SWITCH.BURNIN" switchburnin.sh
		/bin/echo "Make selection (0-$_num_choice)"
		read _answer
		if [ ! -z "$_answer" ]
		then
			valid=`validate_integer $_answer 0 $_num_choice`
			if [ "$valid" != "TRUE" ]
			then
				/Bin/echo "Invalid selection. valid selection are between 0 and $_num_choice"
				/bin/echo " Please make another selection"
				_answer=-1
			else
				if [ $_answer -eq 0 ]
				then
					exit 1
				fi
				FileName=`getScriptSelection $_answer DIAGSETCYCLE "diag.script.SWITCH.BURNIN" switchburnin.sh`
				/bin/echo script selection: $FileName
			fi
		else
		    _answer=-1
		fi
	done
	updatescript=true
 fi
#
# Setup the initialized variables
#
# Initialize the blade burnin parameters
State=INITIALIZED
RunNumber=1
RunStatus=0
CumulativeStatus=0
StartTime=0
RunTime=0
TotalThermal=0
TotalVib=0
TotalCmdNumber=0
CmdNumber=1

#
# Process the slots specified
#

if [ $multiple_slots = false ]
then
	if [ "$updatescript" = "true" ]
	then
		burninSetScript SWITCH BURNIN $FileName -noupdate
	fi
	burninSetLevel 1 -noupdate
	do_a_slot $slot $FileName
else
	slots=`getAvailSlots`
	if [ -z "$slots" ] ;
	then
		/bin/echo "diagBurninStart: No slots to test, please check switch: $FABOS_SWITCHNO"
		exit 3 ;
	fi
	if [ $multiBlade = TRUE ]
	then
		/bin/echo "Enabling burnin on switch: $FABOS_SWITCHNO, slots: $slots"
	else
		/bin/echo "Enabling burnin on switch."
	fi
	if [ "$updatescript" = "true" ]
	then
		burninSetScript SWITCH BURNIN $FileName -noupdate
	fi
	burninSetLevel 1 -noupdate
	for slot in $slots
	do
		do_a_slot $slot $FileName
	done
fi
updateConfig
exit 0
