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


ECHO="/bin/echo"

#
# copied from /fabos/src/setup/profile
#
setcontext()
{
	if [ "$1" != "chassis" ]
	then
		eval $(lscfg_util --switch $1)
	else
		eval $(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
				$ECHO "** $1 $2 $3 $4 $5 $6 $7 $8 $9: Command:$rc **"
			fi
		fi
	CUR_SW_INST=`expr $CUR_SW_INST + 1`
	done
}

frustate () {
$ECHO "myid:"
/fabos/cliexec/myid
$ECHO ""
$ECHO "uptime:"
/fabos/cliexec/uptime
$ECHO ""
$ECHO "date:"
/fabos/cliexec/date
$ECHO ""
$ECHO "firmwareshow:"
/fabos/cliexec/firmwareshow -v
$ECHO ""
}

frulicense () {
$ECHO "licenseidshow:"
/fabos/cliexec/licenseidshow
$ECHO ""
$ECHO "licenseshow:"
/fabos/cliexec/licenseshow
$ECHO ""
}

frusensor () {
$ECHO "tempshow:"
/fabos/cliexec/tempshow
$ECHO ""
$ECHO "sensorshow:"
/fabos/cliexec/sensorshow
$ECHO ""
$ECHO "psshow:"
/fabos/cliexec/psshow
$ECHO ""
$ECHO "fanshow:"
/fabos/cliexec/fanshow
$ECHO ""
}

frusys () {
$ECHO "switchshow:"
$SWITCHCMD /fabos/bin/switchshow
$ECHO ""
$ECHO "sfpshow -all:"
$SWITCHCMD /fabos/cliexec/sfpshow -all
$ECHO ""
$ECHO "porterrshow:"
$SWITCHCMD /fabos/cliexec/porterrshow
$ECHO ""
$ECHO "slotshow:"
/fabos/cliexec/slotshow -d576
$ECHO ""
$ECHO "chassisshow:"
/fabos/cliexec/chassisshow
$ECHO ""
$ECHO "historyshow:"
/fabos/cliexec/historyshow
$ECHO ""
$ECHO "errdumpall:"
/fabos/cliexec/errdumpall
$ECHO ""
$ECHO "hadump:"
/fabos/cliexec/hadump
$ECHO ""
$ECHO "mapsdb:"
$SWITCHCMD /fabos/sbin/mapsdb --show all
$ECHO ""
}

frusupportftp () {
if [ $usrarg == 1 ] || [ $passwdarg == 1 ] || [ $transarg == 1 ] || [ $hostarg == 1 ] || [ $dirarg == 1 ]
then
	fruusage
	exit 1
fi

supportftp -c
if [ $? -eq 1 ];then
	fruusage
	exit 1
fi

supportsave -c -n -i frudump.conf
}

fruusage () {
	$ECHO 'usage: frudump -u <user> [-p <passwd>] -h <host> -d <dir> -l <ftp|scp|sftp>'
	$ECHO '       frudump -c'
	$ECHO '       The -c option is mutually exclusive with -u, -p, -l, -h, and -d.'
	$ECHO '       Uses the parameters saved by the supportftp command.'
}

#
# Main script starts
#

# Check EOS permission on command
/fabos/sbin/eos_check
if [ $? -eq 1 ];then
	# EOS time reached
	# Exit
	exit
fi

if [ -e /fabos/link_sbin/lscfg_test ]
then
	/bin/rm /fabos/link_sbin/lscfg_test
fi

/bin/ln -s /fabos/cliexec/lscfg_util /fabos/link_sbin/lscfg_test 2> /dev/null

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

usrarg=0
passwdarg=0
hostarg=0
dirarg=0
transarg=0
supportftparg=0

while getopts ":u:p:h:d:l:cioearfs" opt; do
    case $opt in 
       a  ) frustate
			frulicense
			frusensor
			frusys
			exit 0 ;;
       r  ) frustate
			exit 0 ;;
       f  ) frulicense
			exit 0 ;;
       s  ) frusensor
			exit 0 ;;
	   o  ) frusys
			exit 0 ;;
	   u  ) usrarg=1
	   		fruusr=$OPTARG ;;
	   p  ) passwdarg=1
	   		frupasswd=$OPTARG ;;
	   h  ) hostarg=1
	   		fruhost=$OPTARG ;;
	   d  ) dirarg=1
	   		frudir=$OPTARG ;;
	   l  ) transarg=1
			frutrans=$OPTARG ;;
	   c  ) supportftparg=1 ;;
       \? ) fruusage
            exit 1 ;;
    esac
done

if [ $supportftparg == 1 ]
then
	frusupportftp
	exit 0
fi

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

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

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

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


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

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


if [ $transarg != 1 ]
then
	supportsave -u $fruusr -p $frupasswd -n -h $fruhost -l ftp -d $frudir -i frudump.conf
else
	if [ "$frutrans" != "ftp" -a "$frutrans" != "scp" -a "$frutrans" != "sftp" ]
	then
		fruusage
		exit 1
	fi
	supportsave -u $fruusr -p $frupasswd -n -h $fruhost -l $frutrans -d $frudir -i frudump.conf
fi
exit 0

