#!/bin/sh
PATH=/bin:/usr/bin:/sbin:/usr/sbin
print_usage()
{
	echo "  Usage: fcipledtest [--slot slot# | all]"
}
print_usage2()
{
	echo "  Usage: fcipledtest [all]"
}
# Check RBAC permission on command
#/fabos/libexec/rbac_check `/bin/basename $0`

#if [ $? -ne 0 ]; then
#    exit 127
#fi
#

#
# Load library -- must be first.
#
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
	echo "Error -- could not locate $util"
	exit 3
fi


if [ "$1" = "help" ] ; then
	print_usage;
	exit 0
fi

#
# Platform check
#
SWBD=$(getSWBD)

if [ $SWBD != "SWBD62" ] && [ $SWBD != "SWBD77" ] && [ $SWBD != "SWBD83" ] && [ $SWBD != "SWBD141" ] && [ $SWBD != "SWBD142" ] && [ $SWBD != "SWBD165" ] && [ $SWBD != "SWBD166" ]; then
	echo "Not supported for this platform"
	exit 0
fi

# switch mode check
#
#
mode=`switchshow | grep switchState: | sed -ne 's/switchState:.//pg' | xargs echo`
for m in $mode; do
    if [ $m = Online ]; then
         cat <<!!WARNING!!
   WARNING: This test may not be executed on a network
   operational switch blade.  You must first disable
   the chassis using the chassisdisable command
!!WARNING!!
         exit -1
    fi
done

# 
#
slotipaddr(){
    case $SWBD in
    SWBD62 | SWBD141) #neptune
        if [ `isActiveCP` != "TRUE" ]; then
            echo Not an active CP
            exit -1
        fi
        if [ $1 -eq 0 ] || [ $1 -gt 12 ]; then
             echo Invalid slot number
             exit -1;
        fi
        _cmd=`commandPath haShow`
        cp_slot=`$_cmd | grep "Active" |\
             sed -e 's/.*Slot\ //' |\
             cut -d "," -f 1`
        ;;
    SWBD77 | SWBD142) #pluto
        if [ $1 -eq 0 ] || [ $1 -gt 8 ]; then
             echo Invalid slot number
             exit -1;
        fi
        if [ `isActiveCP` != "TRUE" ]; then
            echo Not an active CP
            exit -1
        fi
        _cmd=`commandPath haShow`
        cp_slot=`$_cmd | grep "Active" |\
             sed -e 's/.*Slot\ //' |\
             cut -d "," -f 1`
        ;;
    SWBD83) #spike
        if [ $1 -ne 0 ]; then
             echo Invalid slot number
             exit -1;
        fi
        cp_slot=0
        ;;
    *)
        echo $SWBD not supported  
    esac

    net=`expr $cp_slot + 10`
    host=`expr $1 + 1`
    echo "127.1.$net.$host"
}

#
#
getFcipSlots(){
    _cmd=`commandPath slotshow -d575`
    # get all blade slots
    list=`$_cmd | tr -s " " |\
         grep -v "OUT" | grep -v "FAULT" |\
         grep "BLADE" | cut -d " " -f 2 |\
         xargs echo`
    set $list
    # loop through blade slots and get strikers
    ids=`$_cmd | tr -s " " |\
        grep -v "OUT" | grep -v "FAULT" |\
        grep "BLADE" | cut -d " " -f 5 |\
        xargs echo`
    i=1
    ll=""
    for s in $ids; do
        if [ $s = 75 ] || [ $s = 186 ]; then
           ll="$ll \$$i"
        fi 
        i=`expr $i + 1`
    done
    eval echo $ll
}


#
# get sys info for multi blade
#
multiBlade=`isMultiBlade`
slot='all'
if [ $multiBlade = TRUE ]
then
	if [ $# -gt 2 -o "$#" -le 0 ] ; then
		/bin/echo "  !!!!Invalid arguments"
		print_usage;
		exit 0
    elif [ $# -eq 1 -a "$1" != all ] || [ $# -eq 2 -a "$1" != --slot ]; then
        /bin/echo "  !!!!Invalid arguments"
        print_usage;
        exit 0
    elif [ $# -eq 2 -a "$1" = --slot ]; then
        slot=$2
	fi
	
	_cmd=`commandPath slotshow`
	if [ "$slot" != "all" ]; then
		let line_no=$slot+3;
		slot_info=`$_cmd | tail -n +$line_no | head -n1`
		is_ap=`echo $slot_info | cut -f4 -d " "`
		vacant=`echo $slot_info | cut -f3 -d " "`
		if [ "$vacant" == "VACANT" ]; then
			echo "slot $slot is $vacant"
			echo ""
			exit 0
		fi
		if [ "$is_ap" != "75" ] && [ "$is_ap" != "186" ]; then
            echo "not supported platform in slot $slot"
			exit 0
		fi
		if [ "$is_ap" = "186" ]; then
			/fabos/bin/userconfig --show | grep -e "user" -e "zoneadmin" -e "BasicSwitchAdmin" -e "SecurityAdmin"  > /dev/null 2>&1
			_tmp_path=$?
			if [ $_tmp_path -eq 0 ]; then
				echo "RBAC permission denied"
				exit 0
			fi
			/fabos/cliexec/exec_diag fcipledtest --slot $slot
		else
		   bladeid=$is_ap;
		   ipaddr=`slotipaddr $slot`;
           ping -c 1 $ipaddr > /dev/null 2>&1
           if [ $? = 0 ]; then
             rsh $ipaddr /fabos/bin/fcipled_test
             if [ $? = 0 ]; then
                  echo fcipledtest on slot $slot PASSED
             else
                  echo fcipledtest on slot $slot FAILED
             fi
           else
             echo $slot is off
           fi
       fi
	else #if (all slots)
        /fabos/bin/userconfig --show | grep -e "user" -e "zoneadmin" -e "BasicSwitchAdmin" -e "SecurityAdmin"  > /dev/null 2>&1
        _tmp_path=$?
        if [ $_tmp_path -eq 0 ]; then
           echo "RBAC permission denied"
           exit 0
        fi
        list=`getFcipSlots`
        if [ "$list" = "" ]; then
            echo "No AP Blade Found"
            exit 0
        fi
        for slot in $list; do
             if [ "$SWBD" = "SWBD165" ] || [ "$SWBD" = "SWBD166" ]; then
                   /fabos/cliexec/exec_diag fcipledtest --slot $slot
                   /bin/sleep 2
            else
               ipaddr=`slotipaddr $slot`  
               ping -c 1 $ipaddr > /dev/null 2>&1
               if [ $? = 0 ]; then
                        rsh $ipaddr /fabos/bin/fcipled_test
                        if [ $? = 0 ]; then 
                            echo "fcipledtest on slot $slot PASSED"
                        else
                            echo "fcipledtest on slot $slot FAILED"
                        fi
               else
                echo $slot is off
               fi
            fi
        done

	fi # if (all slots)
else # pizzabox
	if [ $# -gt 1 ] ; then
                /bin/echo "  !!!!Invalid arguments"
                print_usage2;
                exit 0
        fi
	if [ $# -eq 1 -a "$1" != "all" ] ; then
		print_usage2
		exit 0
	fi

	ipaddr=`slotipaddr 0`
    ping -c 1 $ipaddr > /dev/null 2>&1
    if [ $? = 0 ]; then
         rsh $ipaddr /fabos/bin/fcipled_test
         if [ $? = 0 ]; then
              echo fcipledtest PASSED
         else
              echo fcipledtest FAILED
         fi
    else
         echo fcipledtest FAILED 
    fi

fi
