#!/bin/sh 
#
#
#    Copyright (c) 1996-2017 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   femdump
#


# Test the blade capability. ( TODO)
# Need to identify if blade support proc-entry based db cmd, and
# Need to identify blade feature capability
# Input: slot#
# Return:  If Ok, blade_type & blade_id is set properly for the slot
# Otherwise, bladeid ==0, and blade_type == ""

SSCMD="/fabos/cliexec/sscmd"
femutil_log="/tmp/fem"
RM=/bin/rm

cmd_root()
{
    export ROLE_ID=root
    export CHASSIS_ROLEID=0
    export LOGIN_ID=root
    $SSCMD $@ "--sscommand=1" -l $femutil_log
}

ROOTCMD="cmd_root"

asic_db_blade_type_() {

   # initialize to unknown blade id & type
	bladeid=0
	blade_type=""
	num_anvils=0
	num_zeus=0
	num_condors=0
	num_pinballs=0
	num_c2s=0
	num_c3s=0
	num_c4s=0
	num_c4ps=0
	num_ge4s=0
	num_ge5s=0
	num_c5s=0
	num_blasters=0

	if test -f /proc/fabos/blade/$1/info; then

		# ref. blade id is defined in linux/include/brocade/swbd.h		
		bladeid=`cat /proc/fabos/blade/$1/info | /bin/grep -a bladeId | sed 's/[^0-9]//g'`

      case "$bladeid" in
		"37" ) blade_type="despina";num_c2s=1;;
		"55" ) blade_type="larissa";num_c2s=2;;
		"51" ) blade_type="triton";num_c2s=2;;
		"46" ) blade_type="hydra";num_c2s=2;;
		"56" ) blade_type="spirit";num_c2s=1;;
		"59" ) blade_type="tomahawk";num_c2s=1;;
		"53" ) blade_type="thor";num_c2s=9;;
		"52" ) blade_type="proteus";num_c2s=4;;
		"29" ) blade_type="sprint";num_condors=2;num_pinballs=2;; # sprint
		"27" ) blade_type="viking";num_condors=6;; # viking
		"24" ) blade_type="marathon";num_condors=2;num_pinballs=2;;   # marathon
		"33" ) blade_type="scimitar";num_condors=2;num_pinballs=2;; 
		"38" ) blade_type="sabre";num_condors=2;num_pinballs=2;;
        "42" ) blade_type="mace";num_c2s=2;; 
        "43" ) blade_type="lance";num_c2s=2;; 
		"45" ) blade_type="elara";num_c2s=1;num_anvils=3;num_zeus=3;; # Elara
		"74" ) blade_type="europa";num_c2s=1;num_anvils=3;num_zeus=3;;
		"69" ) blade_type="spike";num_c2s=1;num_blasters=1;;
		"75" ) blade_type="striker";num_c2s=1;num_blasters=2;;
		"77" ) blade_type="quattro";num_c2s=4;; # Quattro blade
		"31" ) blade_type="ironman";num_condors=1;;   # ironman
		"18" ) blade_type="electron";num_condors=2;;   # 32-ports condor
		"16" ) blade_type="neutron";num_condors=2;;  # core condors
		"36" ) blade_type="positron";num_condors=2;;  # 48-ports condor
		"39" ) blade_type="xenon";num_condors=2;;  # xenon
		"96" ) blade_type="thalassa";num_c3s=2;;  # 48-port condor3
		"97" ) blade_type="galatea";num_c3s=2;;  # 32-port condor3
		"98" ) blade_type="neso";num_c3s=4;;  # condor3 core on DCX
		"99" ) blade_type="charon";num_c3s=2;;  # condor3 core on Pluto 
		"100" ) blade_type="stinger";num_c3s=1;;  # condor3 pizzabox
		"120" ) blade_type="tomtoo";num_c3s=1;; # 24-port condor3 pizzabox 
		"125" ) blade_type="galatea8G";num_c3s=2;; #32-port condor3 8g
		"126" ) blade_type="thalassa8G";num_c3s=2;; #48-port condor3 8g
		"171" ) blade_type="wedge";num_c4s=1;; #condor4 pizzabox
		"176" ) blade_type="mundi";num_c4s=2;; #condor4 core on Venator
		"177" ) blade_type="fisto";num_c4s=4;; #condor4 core on Allegiance
		"178" ) blade_type="skywalker";num_c4s=2;; #48-port condor4 8g		
		"186" ) blade_type="harpoon";num_c4s=1;num_blitzer=2;;  # condor4 based extension blade
		"196" ) blade_type="chewbacca";num_ge4s=1;;  # goldeneye4 pizzabox
		"213" ) blade_type="awing";num_ge4s=1;;  # extn pizzabox based on ge4
		"201" ) blade_type="wedge_plus";num_c4ps=1;;	# condor4p based pizzabox
		"202" ) blade_type="tyr";num_c4ps=5;;	# condor4p based pizzabox
		"204" ) blade_type="anakin";num_c4ps=2;;	#condor4p based 32-port blade
		"214" ) blade_type="chirrut";num_c5s=2;;	#condor5 based core blade 4 slot chassis
		"215" ) blade_type="baze";num_c5s=4;;	#condor5 based core blade 8 slot chassis
		"216" ) blade_type="rey";num_c5s=2;;	#condor5 based 48-port blade
		"217" ) blade_type="cassian";num_c5s=1;;	#condor5 based pizza box
		"218" ) blade_type="rey32";num_c5s=2;;  # Rey32 Gen 7 port blade
		"222" ) blade_type="tyr2";num_c5s=6;;	# condor5 based pizza box
		"223" ) blade_type="gimlet";num_ge5s=1;;	# GoldenEye 5 based pizza box
		"224" ) blade_type="cassianX86";num_c5s=1;;  # Gen 7 X86 CP pizzabox
		"226" ) blade_type="vesperX86";num_ge5s=12;;  # Gen 7 goldeneye5 based edge core X86 CP pizzabox
		"227" ) blade_type="gimletX86";num_ge5s=1;;  # Gen 7 goldeneye5 based X86 CP pizzabox
		"225" ) blade_type="dart";num_c5s=1;;  # Gen 7 extension pizzabox
		* ) blade_type="others";num_condors=1;; # pulsar/stealth/proton
      esac

	fi
	# return status
	if [ "$blade_type" = "" ]; then
		echo_xx 2> /dev/null;
	else
		echo 2> /dev/null
	fi	
}

unset_lscfg_test()
{
	if [ -e /fabos/link_sbin/lscfg_test ]; then
		$RM /fabos/link_sbin/lscfg_test 2> /dev/null
	fi

}

#
# copied from /fabos/src/setup/profile
#
setcontext()
{
    if [ "$1" != "chassis" ]
    then
        eval $(/fabos/cliexec/lscfg_util --switch $1)
    else
        eval $(/fabos/cliexec/lscfg_util --chassis)
    fi
}

#
# debug routine to show time for each CLI
#
#
cmd_nonvf() {
     echo -e "$1 $2 $3 $4 $5 $6 $7 $8 $9:"
     $1 $2 $3 $4 $5 $6 $7 $8 $9
     rc=$?
     if [ $rc -ne 0 ]; then
	echo "** $1 $2 $3 $4 $5 $6 $7 $8 $9: Command:$rc **"
     fi
}

#
# cmd to iterate through all switch instances
#
#
cmd_iterate_all() {
  NUM_SW_INSTANCE=`lscfg_test --max`
  CUR_SW_INST=0
  while [ $CUR_SW_INST -le $NUM_SW_INSTANCE ]
  do
    FABID=`lscfg_test --fid $CUR_SW_INST`

    if [ $FABID -ne -1 ]
    then
      setcontext $FABID

      echo "CURRENT CONTEXT -- $CUR_SW_INST , $FABID"
	echo -e "$1 $2 $3 $4 $5 $6 $7 $8 $9:"
	$1 $2 $3 $4 $5 $6 $7 $8 $9
	rc=$?
	if [ $rc -ne 0 ]; then
	   /bin/echo "** $1 $2 $3 $4 $5 $6 $7 $8 $9: Command:$rc **"
	fi
    fi
    CUR_SW_INST=`expr $CUR_SW_INST + 1`
  done
}

femchip () {
	cd /proc/fabos/blade
	c5regdmpcnt=0
	c4pregdmpcnt=0
  	ge4regdmpcnt=0
  	ge5regdmpcnt=0
	c4regdmpcnt=0
	pbregdumpcount=0
	pbregdumpindex=0
	blsregdumpcount=0
	blsregdumpindex=0

	/bin/echo "********************************************************"
	/bin/echo "F_SSHOW_ASICDB: femchip"
	date
	/bin/echo "********************************************************"
	/bin/echo "SWITCHCMD sloterrshow -c2 -r"
	date
	/bin/echo "********************************************************"
	$ROOTCMD /fabos/rbin/sloterrshow -c2 -r

	for i in `ls -d [0-9]* | sort -n` ; do
	#
	# no shell cmd should intervene between asic_db_blade_type and 
	# check for return status stored in $?.
	# Note: "db" work on proc path slot#/mini#/chip#
	# This script assumes the following for ALL blade type.
	# The script has to be modified if the assumption becomes false.
	# 1) The following chip assignment per miniswitch
		# chip# = 0 => condor
		# chip# = 1 => pinball
		# chip# = 2 => flipper
	# 2) each mini has only 1 instance of a certain asic.

	# Capture bladeportmap for each blade to aid debugging backend links.
	/bin/echo
	/bin/echo "********************************************************"
	/bin/echo "SWITCHCMD bladeportmap $i"
	date
	/bin/echo "********************************************************"
	$ROOTCMD /fabos/rbin/bladeportmap $i

	asic_db_blade_type_ $i
	if [ "$?" -eq 0 ]; then
		count=0
		echo "num_c5s:$num_c5s"
		while [ $count -lt $num_c5s ]
		do	
			echo "INSIDE C5 #######################################"
			/fabos/link_rbin/c5regdump "$i"/$count/0
			mv /var/regdump.dmp /var/c5regdump.dmp."$i".$count
			/fabos/rbin/gzip -f /var/c5regdump.dmp."$i".$count
			(( c5regdmpcnt += 1 ))
			(( count += 1 ))
		done
		count=0
		echo "num_c4ps:$num_c4ps"
		while [ $count -lt $num_c4ps ]
		do	
			echo "INSIDE C4P#######################################"
			/fabos/link_rbin/c4pregdump "$i"/$count/0
			mv /var/regdump.dmp /var/c4pregdump.dmp."$i".$count
			/fabos/rbin/gzip -f /var/c4pregdump.dmp."$i".$count
			(( c4pregdmpcnt += 1 ))
			(( count += 1 ))
		done
		count=0
		while [ $count -lt $num_ge5s ] 
		do 
            /fabos/rbin/ge5regdump "$i"/$count/0
			mv /var/regdump.dmp /var/ge5regdump.dmp."$i".$count
			/fabos/rbin/gzip -f /var/ge5regdump.dmp."$i".$count
			(( ge5regdmpcnt += 1 ))
			(( count += 1 ))
		done
		count=0
		while [ $count -lt $num_ge4s ] 
		do 
            /fabos/rbin/ge4regdump "$i"/$count/0
			mv /var/regdump.dmp /var/ge4regdump.dmp."$i".$count
			/fabos/rbin/gzip -f /var/ge4regdump.dmp."$i".$count
			(( ge4regdmpcnt += 1 ))
			(( count += 1 ))
		done
		count=0
		while [ $count -lt $num_c4s ]
		do	
			/fabos/link_rbin/c4regdump "$i"/$count/0 
			mv /var/regdump.dmp /var/c4regdump.dmp."$i".$count
			/fabos/rbin/gzip -f /var/c4regdump.dmp."$i".$count
			(( c4regdmpcnt += 1 ))
			(( count += 1 ))
		done
		count=0
                # for pinball chip
		count=0
		while [ $count -lt $num_pinballs ] 
		do 
		   let pbregdumpindex=$count+2
            /fabos/sbin/pbregdump "$i"/$count/1
            mv /var/regdump.dmp /var/pbregdump.dmp."$i".$pbregdumpindex
            /fabos/rbin/gzip -f /var/pbregdump.dmp."$i".$pbregdumpindex
            (( pbregdumpcount += 1 ))
			(( count += 1 ))
		done
		# for blaster chip
		count=0
		while [ $count -lt $num_blasters ]
		do
		let blsregdumpindex=$count+1
		if [ "$blade_type" = "striker" ]
		then
		let fcipchip=$count+1
		/fabos/sbin/blsregdump "$i"/$fcipchip/0
		else
		/fabos/sbin/blsregdump "$i"/$count/1
		fi
		mv /var/regdump.dmp /var/blsregdump.dmp."$i".$blsregdumpindex
		/fabos/rbin/gzip -f /var/blsregdump.dmp."$i".$blsregdumpindex
		(( blsregdumpcount += 1 ))
			(( count += 1 ))
		 done	
	fi
	done


    if [ $c5regdmpcnt -gt 0 ]
    then
    #
    # Move all reg dumps into a tar ball
    #
    tar -cvPf /tmp/c5regdump.tar /var/c5regdump*gz 2> /dev/null
    /bin/rm /var/c5regdump*gz

    else
    /bin/echo "No condor5 chips on this switch" > /var/c5emptyregdump.txt
    tar -cvf /tmp/c5regdump.tar /var/c5emptyregdump.txt 2> /dev/null
    /bin/rm /var/c5emptyregdump.txt
    fi

    if [ $c4pregdmpcnt -gt 0 ]
    then
    #
    # Move all reg dumps into a tar ball
    #
    tar -cvPf /tmp/c4pregdump.tar /var/c4pregdump*gz 2> /dev/null
    /bin/rm /var/c4pregdump*gz

    else
    /bin/echo "No condor4 plus chips on this switch" > /var/c4pemptyregdump.txt
    tar -cvf /tmp/c4pregdump.tar /var/c4pemptyregdump.txt 2> /dev/null
    /bin/rm /var/c4pemptyregdump.txt
    fi

    if [ $ge5regdmpcnt -gt 0 ]
    then
    #
    # Move all reg dumps into a tar ball
    #
    tar -cvPf /tmp/ge5regdump.tar /var/ge5regdump*gz 2> /dev/null
    /bin/rm /var/ge5regdump*gz

    else
    /bin/echo "No Geye5 chips on this switch" > /var/ge5emptyregdump.txt
    tar -cvf /tmp/ge5regdump.tar /var/ge5emptyregdump.txt 2> /dev/null
    /bin/rm /var/ge5emptyregdump.txt
    fi

    if [ $ge4regdmpcnt -gt 0 ]
    then
    #
    # Move all reg dumps into a tar ball
    #
    tar -cvPf /tmp/ge4regdump.tar /var/ge4regdump*gz 2> /dev/null
    /bin/rm /var/ge4regdump*gz

    else
    /bin/echo "No Geye4 chips on this switch" > /var/ge4emptyregdump.txt
    tar -cvf /tmp/ge4regdump.tar /var/ge4emptyregdump.txt 2> /dev/null
    /bin/rm /var/ge4emptyregdump.txt
    fi

    if [ $c4regdmpcnt -gt 0 ]
    then
    #
    # Move all reg dumps into a tar ball
    #
    tar -cvPf /tmp/c4regdump.tar /var/c4regdump*gz 2> /dev/null
    /bin/rm /var/c4regdump*gz

    else
    /bin/echo "No condor4 chips on this switch" > /var/c4emptyregdump.txt
    tar -cvf /tmp/c4regdump.tar /var/c4emptyregdump.txt 2> /dev/null
    /bin/rm /var/c4emptyregdump.txt
    fi

	if [ $pbregdumpcount -gt 0 ]
	then
		#
		# Move all PB reg dumps into a tar ball
		#

		tar -cvPf /tmp/pbregdump.tar /var/pbregdump*gz 2> /dev/null

		/bin/rm /var/pbregdump*gz
 	else
  	/bin/echo "No pinball chips on this switch" > /var/pbemptyregdump.txt
	tar -cvf /tmp/pbregdump.tar /var/pbemptyregdump.txt 2> /dev/null
	/bin/rm /var/pbemptyregdump.txt
	fi
	if [ $blsregdumpcount -gt 0 ]
	then
		 #
	         # Move all Blaster reg dumps into a tar ball
	         #
		tar -cvPf /tmp/blsregdump.tar /var/blsregdump*gz 2> /dev/null					  
	
		/bin/rm /var/blsregdump*gz
	else
        /bin/echo "No blaster chips on this switch" > /var/blsemptyregdump.txt
	tar -cvf /tmp/blsregdump.tar /var/blsemptyregdump.txt 2> /dev/null
	/bin/rm /var/blsemptyregdump.txt
	fi
	sync
}

femfab () {
/bin/echo "********************************************************"
/bin/echo "F_SSHOW_FABRIC: femfab"
date
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD fabricshow"
date
/bin/echo "********************************************************"
$SWITCHCMD /fabos/cliexec/fabricshow
/bin/echo
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD islshow"
date
/bin/echo "********************************************************"
$SWITCHCMD /fabos/cliexec/islshow
/bin/echo
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD trunkshow"
date
/bin/echo "********************************************************"
$SWITCHCMD /fabos/cliexec/trunkshow
/bin/echo
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD cfgsize"
date
/bin/echo "********************************************************"
$SWITCHCMD /fabos/cliexec/cfgsize
/bin/echo
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD zone stateshow"
date
/bin/echo "********************************************************"
$SWITCHCMD /fabos/cliexec/zone stateshow
/bin/echo
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD cfgshow"
date
/bin/echo "********************************************************"
$SWITCHCMD /fabos/link_bin/cfgshow
/bin/echo
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD porterrshow"
date
/bin/echo "********************************************************"
$SWITCHCMD /fabos/cliexec/porterrshow
/bin/echo

#
# Run following LF CLI's only if VF mode is ON.
#
if [ "$HOME_VF" != "" ]; then
/bin/echo
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD lfcfg --showall -lisl -v"
date
/bin/echo "********************************************************"
lfcfg --showall -lisl -v
/bin/echo
fi
}

femsys () {
/bin/echo "********************************************************"
/bin/echo "F_SSHOW_SYS: femsys"
date
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD switchshow"
date
/bin/echo "********************************************************"
$SWITCHCMD /fabos/bin/switchshow
/bin/echo
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD sfpshow -all"
date
/bin/echo "********************************************************"
$SWITCHCMD /fabos/cliexec/sfpshow -all
/bin/echo
}

femport () {
/bin/echo "********************************************************"
/bin/echo "F_SSHOW_PORT: femport"
date
/bin/echo "********************************************************"
be_port_skip=0
if [ "$HOME_VF" != "" ]; then
  NUM_SW_INSTANCE=`lscfg_test --max`
  CUR_SW_INST=0
  while [ $CUR_SW_INST -le $NUM_SW_INSTANCE ]
  do
    FABID=`lscfg_test --fid $CUR_SW_INST`
 
    if [ $FABID -ne -1 ]
    then
      setcontext $FABID

      echo "CURRENT CONTEXT -- $CUR_SW_INST , $FABID"
  	  if [ $multi_blade = 1 ]
      then
  	  	list=`/fabos/cliexec/exec_diag diagshow |grep " OK" | \
            cut -d " " -f 2`
	    portstatscmd="portstatsshow SP"
	  else
  	  	list=`/fabos/cliexec/exec_diag diagshow |grep " OK" | \
            cut -d " " -f 2 |cut -d "/" -f 2`
	    portstatscmd="portstatsshow"
	  fi
      
	  for port in $list
	  do
			  /bin/echo ""
			  echo "$portstatscmd $port"
			  /bin/echo ""
			  portstatsshow $port
	  done
      #ptstatsshow for Backendports
      slist=""
      ptstatscmd="ptstatsshow"
      slist=`ls /proc/fabos/blade/`
      for slot in $slist
      do
       if [ $be_port_skip != 1 ]
       then
               bpmaplist=`cat /proc/fabos/blade/$slot/portmap |   sed 's/^[ \t]*//' | tr -s " " " "| \
                          cut -d " " -f 2,3,14 | grep -v -e [a-z] -e [A-Z] -e == |  cut -d " " -f 1`
			   if [ "$bpmaplist" == "" ]
			   then
                    /bin/echo ""
					echo "=========================================================================="
                    /bin/echo ""
					echo "There is no BackEnd Ports and the stats will not  be collected for BE Ports"
                    /bin/echo ""
					echo "=========================================================================="
                    /bin/echo ""
			   fi
               for port in $bpmaplist
               do
                    /bin/echo ""
                    echo "$ptstatscmd $slot/$port"
                    /bin/echo ""
                    ptstatsshow $slot/$port
               done
       fi
      done
    fi
    CUR_SW_INST=`expr $CUR_SW_INST + 1`
    be_port_skip=1
  done
else
  	  if [ $multi_blade = 1 ]
      then
  	  	list=`/fabos/cliexec/exec_diag diagshow |grep " OK" | \
            cut -d " " -f 2`
	    portstatscmd="portstatsshow SP"
	  else
  	  	list=`/fabos/cliexec/exec_diag diagshow |grep " OK" | \
            cut -d " " -f 2 |cut -d "/" -f 2`
	    portstatscmd="portstatsshow"
	  fi

      for port in $list
      do
          /bin/echo ""
	      echo "$portstatscmd $port"
          /bin/echo ""
	      portstatsshow $port
      done
      #ptstatsshow for Backendports
      slist=""
      ptstatscmd="ptstatsshow"
      slist=`ls /proc/fabos/blade/`
      for slot in $slist
      do      
               bpmaplist=`cat /proc/fabos/blade/$slot/portmap |   sed 's/^[ \t]*//' | tr -s " " " "| \
                          cut -d " " -f 2,3,14 | grep -v -e [a-z] -e [A-Z] -e == |  cut -d " " -f 1`
			   if [ "$bpmaplist" == "" ]
			   then
                    /bin/echo ""
					echo "=========================================================================="
                    /bin/echo ""
					echo "There is no BackEnd Ports and the stats will not  be collected for BE Ports"
                    /bin/echo ""
					echo "=========================================================================="
                    /bin/echo ""
			   fi
               for port in $bpmaplist
               do      
                    /bin/echo ""
                    echo "$ptstatscmd $slot/$port"
                    /bin/echo ""
                    ptstatsshow $slot/$port
               done    
      done    
fi
}

femiswitch () {
/bin/echo "********************************************************"
/bin/echo "F_SSHOW_ISWITCH: femiswitch"
date
/bin/echo "********************************************************"
if [ "$HOME_VF" != "" ]; then
    FABID=`lscfg --show |grep switch |tr " " "\n" | grep bs | \
	       cut -d "(" -f 1`

    if [ "$FABID" != "" ]
    then
      setcontext $FABID

      echo "CURRENT CONTEXT -- $FABID"
	else
	  return
	fi
fi
  if [ $multi_blade = 1 ]
  then
  	list=`switchshow |grep " EX-Port" |sed 's/^[ \t]*//' | \
		tr -s " " " "|cut -d " " -f 2,3 |tr " " "/"`
  else
  	list=`switchshow |grep " EX-Port" |sed 's/^[ \t]*//' | \
		tr -s " " " "|cut -d " " -f 2`
  fi

  for port in $list
  do
      /bin/echo ""
      echo "portcfgexport $port"
      /bin/echo ""
      portcfgexport $port
  done

/bin/echo
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD fcrproxydevshow -a"
date
/bin/echo "********************************************************"
fcrproxydevshow -a
/bin/echo
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD fcrfabricshow"
date
/bin/echo "********************************************************"
fcrfabricshow
/bin/echo
}

femservice () {
/bin/echo "********************************************************"
/bin/echo "F_SSHOW_SERVICE: femservice"
date
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD nsshow -r"
date
/bin/echo "********************************************************"
$SWITCHCMD /fabos/cliexec/nsshow -r
/bin/echo
/bin/echo "********************************************************"
/bin/echo "SWITCHCMD nszonemember -a"
date
/bin/echo "********************************************************"
$SWITCHCMD /fabos/cliexec/nszonemember -a
/bin/echo
}

femsupportftp () {
if [ $usrarg == 1 ] || [ $passwdarg == 1 ] || [ $transarg == 1 ] || [ $hostarg == 1 ] || [ $dirarg == 1 ]
then
   	femusage
	unset_lscfg_test
    exit 1
fi

supportsave -c -n -i fem.conf
}

femusb () {
if [ $usbarg == 1 ]
then
	if [ $dirarg != 1 ]
	then
		echo 'Please specify a USB storage directory'
		femusage
		unset_lscfg_test
		exit 1
	fi
	supportsave -U -d $femdir -i fem.conf
	unset_lscfg_test
	exit 0
fi
}

femusage () {
	echo 'usage: femdump -u <user> [-p <passwd>] -h <host> -d <dir> -l <ftp|scp|sftp> -b <port> -c -a'
	echo '       The -c option is mutually exclusive with -u, -p, -l, -h, and -d.'
	echo '       Uses the FTP, SCP, or SFTP parameters saved by the supportFtp command.'
	echo '       femdump -U -d <dir>'
}

validateargs () {
	if [ $numargs != 1 ] || [ $USER != "root" ]; then femusage; unset_lscfg_test; exit 1; fi;
}

#
# Main script starts
#

/fabos/sbin/eos_check
if [ $? -eq 1 ];then
	# EOS time reached
	# Exit without changing the date
	exit
fi

if [ ! -e /fabos/link_sbin/lscfg_test ]
then
	/bin/ln -s /fabos/cliexec/lscfg_util /fabos/link_sbin/lscfg_test 2> /dev/null
fi

export PATH=/fabos/link_bin:/bin:/usr/bin:/sbin:/usr/sbin:/fabos/link_abin:/fabos/link_sbin:/fabos/link_rbin:/fabos/factory:/fabos/xtool
if [ "$(lscfg_test --vf_enabled 2> /dev/null)" = 'VF is enabled.' ]; then
	HOME_VF=128
else
	HOME_VF=
fi

if [ "$HOME_VF" = "" ]
then
SWITCHCMD="cmd_nonvf"
else
SWITCHCMD="cmd_iterate_all"
fi

if slotshow |grep "Not supported" > /dev/null
then
	multi_blade=0
else
	multi_blade=1
fi

numargs=$#
usrarg=0
passwdarg=0
hostarg=0
dirarg=0
transarg=0
supportftparg=0
usbarg=0
craarg=0
portarg=0
skippwd=0
ssavestr="$*"

while getopts ":u:p:h:d:l:b:Ucagrfsoei" opt; do
    case $opt in 
	   g  ) validateargs
		femchip
		femfab
		femsys
		femport
		femservice
		femiswitch
		unset_lscfg_test
		exit 0 ;;
	   r  ) validateargs
		femchip
		unset_lscfg_test
		exit 0 ;;
	   f  ) validateargs
		femfab
		unset_lscfg_test
		exit 0 ;;
	   s  ) validateargs
		femsys
		unset_lscfg_test
		exit 0 ;;
	   o  ) validateargs
		femport
		unset_lscfg_test
		exit 0 ;;
	   e  ) validateargs
		femservice
		unset_lscfg_test
		exit 0 ;;
	   i  ) validateargs
		femiswitch
		unset_lscfg_test
		exit 0 ;;
	   u  ) usrarg=1
	   	femusr=$OPTARG ;;
	   p  ) passwdarg=1
	   	fempasswd=$OPTARG ;;
	   h  ) hostarg=1
	   	femhost=$OPTARG ;;
	   d  ) dirarg=1
	   	femdir=$OPTARG ;;
	   l  ) transarg=1
		femtrans=$OPTARG ;;
	   b  ) portarg=1
	        femportnum=$OPTARG ;;
	   c  ) supportftparg=1 ;;
	   U  ) usbarg=1 ;;
	   a  ) craarg=1 ;;
	   \? ) femusage
				unset_lscfg_test
                exit 1 ;;
    esac
done

optargs=$OPTIND

if [ $numargs -ge $optargs ]
then
	echo "Invalid argument!"
	femusage
	unset_lscfg_test
	exit 1
fi

if [ $numargs -gt 0 ] && [ $usrarg != 1 ] && [ $passwdarg != 1 ] && [ $transarg != 1 ] && [ $hostarg != 1 ] && [ $dirarg != 1 ] && [ $portarg != 1 ] && [ $supportftparg != 1 ] && [ $usbarg != 1 ] && [ $craarg != 1 ]
then
	femusage
	unset_lscfg_test
	exit 1
fi

if [ $supportftparg == 1 ]
then
	femsupportftp
	unset_lscfg_test
	exit 0
fi

femusb

if [ $usrarg != 1 ] && [ $passwdarg != 1 ] && [ $transarg != 1 ] && [ $hostarg != 1 ] && [ $dirarg != 1 ]
then
	ssavestr="supportsave $* -i fem.conf"
	$ssavestr
	unset_lscfg_test
	exit 0
fi

if [ $hostarg != 1 ]
then
	read  -p "Host IP or Host Name: " femhost
    hostarg=1
fi

if [ $usrarg != 1 ]
then
	read  -p "User Name: " femusr
    usrarg=1
fi

if [ $transarg != 1 ]
then
	read  -p "Protocol (ftp | scp | sftp): " femtrans
    transarg=1
fi

if [ "$femtrans" == "scp" ] || [ "$femtrans" == "sftp" ]
then
	skippwd=1
fi

# When User Name is anonymous no need to prompt Password field.
if [ $passwdarg != 1 ] && [ $femusr != "anonymous" ] && [ $skippwd != 1 ]
then
	read -s -p "Password: " fempasswd
    passwdarg=1
    echo ""
fi

if [ $dirarg != 1 ]
then
	read -p "Remote Directory: " femdir
    dirarg=1
fi

if [ $usrarg != 1 ] || [ $hostarg != 1 ] || [ $dirarg != 1 ]
then
    femusage
	unset_lscfg_test
    exit 1
fi

if [ $transarg != 1 ]
then
	ssavestr="supportsave -u $femusr -p $fempasswd -n -h $femhost -l ftp -d $femdir -i fem.conf"
else
	if [ "$femtrans" != "ftp" -a "$femtrans" != "scp" -a "$femtrans" != "sftp" ]
	then
		femusage
		unset_lscfg_test
		exit 1
	fi

#
# Skip password with scp or sftp protocol, SS will check public key authentication if no password is provided.
# If public key auth fails, SS will prompt for password automatically.
# For Ineractive mode; password should not be prompted when protocol is scp or sftp.
# For Non-interactive mode; when password is given then the same can be passed to SS.
#

	if [ $passwdarg != 1 ] && [ $skippwd == 1 ]
	then
		ssavestr="supportsave -u $femusr -n -h $femhost -l $femtrans -d $femdir -i fem.conf"
	else
		ssavestr="supportsave -u $femusr -p $fempasswd -n -h $femhost -l $femtrans -d $femdir -i fem.conf"
	fi
fi

if [ $craarg == 1 ]
then
	ssavestr="$ssavestr -a"
fi

if [ $portarg == 1 ]
then
	ssavestr="$ssavestr -b $femportnum"
fi

# Run supportsave
$ssavestr
unset_lscfg_test
exit 0
