#!/bin/sh 
#
#
#    Copyright (c) 2001 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   ledScript
#    Module name: diag/scripts
#
#    This script implements the LED Test command. It will run ALL
#    available LED tests on the switch blades and WWNs.
#
# NAME
#      ledtest - run LED diagnostics on a switch blade & WWNs
# 
# SYNOPSIS
#      ledtest
# 
# AVAILABILITY
#      admin
# 
# DESCRIPTION
#      This command will run a suite of LED tests.
# 
# OPTIONS
#      NONE
# 
# EXAMPLES
#      > ledtest
#      Disabling switch
#      launch ledScript.tmp script in background (PID is 17974)
#      Enter <cr> to ABORT
# 
# SEE ALSO
#      portledtest(1d)
# 

TmpX=/tmp/ledScript.tmp
home="/fabos/sbin"
pCntr=1

#
#######################################################################
#
#  FUNCTION - Setup temp LED script file (called from main script).
#
#######################################################################
#
setScriptFile()
{

export DIAG_POST=1
export DCOMM_USR_FAILLIM=16

#  setup temp script file - 
#
cat > $TmpX << END_OF_file
#!/bin/sh 
#
#  function to process trapped signal received. Upon entry, KILL the
#  child PID and set abort flag trap end script operation.
#
ledcleanup()
{
/bin/echo ""
/bin/echo "LedScript:  Exiting. ABORTED by User"
/bin/echo "teminating switch 0 child process \$mychild0"
/bin/kill -KILL \$mychild0

if [ "\`isMultiBlade\`" = TRUE ]
then
     /bin/echo "teminating switch 1 child process \$mychild1"
     /bin/kill -KILL \$mychild1
fi

_max_attempts=30
_attempts=0
done0=0
done1=0
while [ \$_attempts -lt \$_max_attempts ]
do
	if [ "\`/bin/ps -p \$mychild0 | /bin/grep -w \$mychild0\`" != "" ]
	then
		/bin/echo " Waiting for switch 0 portledtest process (PID \$mychild0) to stop"
	else
		done0=1
	fi
	if [ "\`/bin/ps -p \$mychild1 | /bin/grep -w \$mychild1\`" != "" ]
	then
		/bin/echo " Waiting for switch 1 portledtest process (PID \$mychild1) to stop"
	else
		done1=1
	fi
	if [ \$done0 -eq 1 -a \$done1 -eq 1 ]
	then
	  break
	fi
	_attempts=\`/usr/bin/expr \$_attempts + 1\`
done

if [ \$_attempts -ge \$_max_attempts ]
then
	if [ "\`/bin/ps -p \$mychild0 | /bin/grep -w \$mychild0\`" != "" ]
	then
	  /bin/echo " Could not kill for switch 0 portledtest process (PID \$mychild0)"
	fi
	if [ "\`/bin/ps -p \$mychild1 | /bin/grep -w \$mychild1\`" != "" ]
	then
	  /bin/echo " Could not kill for switch 1 portledtest process (PID \$mychild1)"
	fi
fi
/bin/kill -KILL \$myotherchild
_attempts=0
while [ \$_attempts -lt \$_max_attempts ]
do
	if [ "\`/bin/ps -p \$myotherchild | /bin/grep -w \$myotherchild\`" != "" ]
	then
		/bin/echo " Waiting for xtool process (PID \$myotherchild) to stop"
	else
		break;
	fi
	_attempts=\`/usr/bin/expr \$_attempts + 1\`
done

if [ \$_attempts -ge \$_max_attempts ]
then
	/bin/echo " Could not kill for xtool process (PID \$myotherchild)"
fi
#
#  restore Blades ATTN LEDs
#
$xcmd -d8

/bin/echo ""
/bin/echo "LedScript:  Exited. ABORTED by User"
/bin/echo ""
exit 0
}

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
#
#  setup signal trap to call cleanup() if the followings:
#	control/d entered at the same window
#	kill <pid> is entered
#
trap ledcleanup INT TERM KILL

#
#  Main LED Diagnostic
#
export FABOS_SWITCHNO=0
#
#  disable switch before testing
#
/bin/echo ""
/bin/echo "Disabling switch 0"
switchDisable

portledtest -npass 5000 &
export mychild0=\$!
/bin/echo "Launching my switch 0 child (PID \$mychild0)"
if [ \`isMultiBlade\` = TRUE ]
then
  export FABOS_SWITCHNO=1
  #
  #  disable switch before testing
  #
  /bin/echo ""
  /bin/echo "Disabling switch 1"
  switchDisable

  portledtest -npass 5000 &
  export mychild1=\$!
  /bin/echo "Launching my switch 1 child (PID \$mychild1)"
fi
#
#  cycle WWN Power LEDs
#
$xcmd -d7 -t 750 -np 5000 &
export myotherchild=\$!
wait \$!

/bin/kill -KILL \$mychild0
if [ \`isMultiBlade\` = TRUE ]
then
     /bin/kill -KILL \$mychild1
fi
#
END_OF_file
}
homecleanup()
{
    kill -TERM $child 2>&1
    max_attempts=30
    attempts=0
    while [ $attempts -lt $max_attempts ]
    do
	if [ "`/bin/ps -p $child | /bin/grep -w $child`" != "" ]
	then
		/bin/echo " Waiting for script process (PID $child) to stop"
	else
		break;
	fi
	attempts=`/usr/bin/expr $attempts + 1`
    done

    if [ $attempts -ge $max_attempts ]
    then
	/bin/echo " Could not kill for script process (PID $child)"
    fi

    /bin/echo "ledScript ENDed, exit script(PID is $child)"

    #  remove temp script file 
    #
    if [ -f $TmpX ]
    then
	/bin/rm -f $TmpX
    fi
    exit
}

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
xcmd=`commandPath xdiag`
if [ $? != 0 ]
then
     /bin/echo "LED:  ABORT - could not locate xdiag program"
     exit 3
fi
cmd=`commandPath exec_diag`
if [ $? != 0 ]
then
     /bin/echo "LED:  ABORT - could not locate exec_diag program"
     exit 3
fi
#
# Allow diag tests to operate in background
#
export DCOMM_USR_FAILLIM=64
export DIAG_POST=1

#
#  construct LED test script (temp script)
#
setScriptFile

#  setup signal trap to call homecleanup() if the followings:
#	control/d entered at the same window
#	kill <pid> is entered
#
trap homecleanup INT TERM KILL

/bin/chmod 777 $TmpX
$TmpX > /dev/null 2>&1 &
export child=$!
/bin/echo "launch ledScript.tmp script in background (PID is $child)"

#  input a line from user
#
/bin/echo "Enter <cr> to ABORT"
read line
homecleanup
