#!/bin/sh
#
#
#    Copyright (c) 1996-2005 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 chassis 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.
# 

# Check whether this is invoked internally.
# If yes, continue after shifting the parameters.
# Else, exit with the no longer support message.
checkLocal=`/usr/bin/expr "$1" : "[-]*local"`
if [ "0" -ne "$checkLocal" ] ; then
    shift
else
    echo "This command is no longer supported as of Fabric OS v6.2.0"
    exit 3
fi

#
#
# 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 [ "$MyName" = "-current" ]
  then
    MyName=`getConfig diag.mode.burnin.$1.name $STRING burnin`
    if [ "$MyName" = "-none-" ]
    then
      updatescript=true
      MyName=burnin
    fi
    /bin/echo "existing script is: $MyName"
  fi
  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
  _log_path=`burninLogPath $BaseFileName`
  /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`

    _platform=`getSWBD`
    case "$_platform" in
      "SWBD62" )
	   other_slot=`expr $other_slot + 1`
      /usr/bin/rsh 127.1.1.$other_slot "/bin/rm -f /$_log_path/*.$1.log"
      /usr/bin/rsh 127.1.1.$other_slot "/bin/rm -f /tmp/burnin_PID.$1"
	  ;;
    
     "SWBD166" | "SWBD165" )
     other_slot=`expr $other_slot + 1`
     /usr/bin/rsh 127.3.1.$other_slot "/bin/rm -f /$_log_path/*.$1.log"
     /usr/bin/rsh 127.3.1.$other_slot "/bin/rm -f /tmp/burnin_PID.$1"
     ;;

	  "SWBD141" | "SWBD142" )
      #For Yoda use slot no + 10 for standby CP
      other_slot=`expr $other_slot + 10`
      /usr/bin/rsh 127.1.$other_slot.$other_slot "/bin/rm -f /$_log_path/*.$1.log"
      /usr/bin/rsh 127.1.$other_slot.$other_slot "/bin/rm -f /tmp/burnin_PID.$1"
      ;;

      "SWBD77" )
       other_slot=`expr $other_slot + 1`
      /usr/bin/rsh 127.1.1.$other_slot "/bin/rm -f /$_log_path/*.$1.log"
      /usr/bin/rsh 127.1.1.$other_slot "/bin/rm -f /tmp/burnin_PID.$1"
          ;;

      * )
	  /bin/echo "Going into Default Platform case"
      /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"
	  ;;
    esac
  fi

  # setup for the next burnin run
  burninSetName $MyName $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
#############################
# source the parameter list #
#############################
FileName=common_test_list
util=$FileName
ok=0
for f in "./$util" "$FABOSHOME/share/$util" "$home/$util"
do
  if [ -r $f ]
  then
    name_list=`/bin/cat $f | /usr/bin/tr -s "	 " "  "| /bin/grep "N SCRIPTNAME" | /usr/bin/cut -d " " -f3-`
    ok=1
    break;
  fi
done
if [ $ok -ne 1 ]
then
  err "Could not locate $util"
  exit 3
fi
if [ "$name_list" = "" ]
then
  err "Could not locate script name list."
  exit 3
fi

slots=`getAvailSlots -chassis`
if [ -z "$slots" ] ;
then
  /bin/echo "diagBurninStart: No slots to test, please check switch: $FABOS_SWITCHNO"
  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 -chassis`"
      if [ "$ans" = TRUE ]
      then
        slot=$FABOS_SLOTNO
      else
        slot=$def_slot
      fi  

    else
      ans="`isValidSlot $x -chassis`"
      if [ "$ans" = TRUE ]
      then
        slot=$x
      else 
        err "$x is an invalid slot number"
        if [ $multiBlade = TRUE ]
        then
          /bin/echo "Available slots are: `getAvailSlots -chassis`."
        fi
        exit 4
      fi
    fi
    MyName="$3"
    multiple_slots=false
  else
    MyName=$1
    multiple_slots=true
  fi
  
fi
#
# Setup the desired script if specified
#
updatescript=false
if [ "$MyName" != "" ]
then
  if [ "$MyName" != "-current" ]
  then
    updatescript=true
  fi
else
  match_script=""
  match=TRUE
  for _slot in $slots
  do
    current_script=`getConfig diag.mode.burnin.$_slot.name $STRING burnin`
    if [ "$match_script" = "" ]
    then
      match_script=$current_script     
    else
      if [ "$match_script" != "$current_script" ]
      then
        match=FALSE    
        break
      fi
    fi
  done
  if [ $match = TRUE ]
  then
    _num_choice=`getNumAvailableScripts -config "$name_list"`
  else
    _num_choice=`getNumAvailableScripts -noconfig "$name_list"`
  fi
  _answer=-1
  while [  $_answer -lt 0 -o $_answer -gt $_num_choice ]
  do
    /bin/echo "Choices are:"
    if [ $match = TRUE ]
    then
      showAvailableScripts "diag.mode.burnin.$_slot.name" verify "$name_list"
    else
      showAvailableScripts -noconfig "$name_list"
    fi
    /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
        if [ $match = TRUE ]
        then
          MyName=`getScriptSelection $_answer "diag.mode.burnin.$_slot.name" verify "$name_list"`
        else
          MyName=`getScriptSelection $_answer -noconfig "$name_list"`
        fi
        /bin/echo script selection: $MyName
      fi
    else
        _answer=-1
    fi
  done
  updatescript=true
 fi
# Convert old script name to process step name
MyName=`/bin/echo $MyName | /bin/sed -e "s/switch//g" -e "s/\.sh//g" -e "s/\.//g"`
MyName=`/bin/basename $MyName`
#
# 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

/bin/echo ""
/bin/echo "IMPORTANT: Before starting a burnin run:"
/bin/echo "IMPORTANT: Please insure that the burnin error log"
/bin/echo "IMPORTANT: is empty by executing \"burninerrclear\""
/bin/echo ""

# Check the burnin state
burninstate=`burninSetLevel -show | \
      /usr/bin/cut -d" " -f4 | \
      /usr/bin/cut -d"." -f1`
if [ "$burninstate" != "0" ]
then
  /bin/echo "Testing already in-progress."
  /bin/echo "Please wait till the test is completed."
  /bin/echo "Or use diagstopburnin to terminate testing."
  exit 5
fi

#
# Process the slots specified
#

if [ $multiple_slots = false ]
then
  burninSetLevel 1 -noupdate
  do_a_slot $slot $MyName
else
  if [ $multiBlade = TRUE ]
  then
    /bin/echo "Enabling burnin on slots: $slots"
  else
    /bin/echo "Enabling burnin on switch."
  fi
  burninSetLevel 1 -noupdate
  for slot in $slots
  do
    do_a_slot $slot $MyName
  done
fi

updateConfig
exit 0
