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


ECHO="/bin/echo"
RM=/bin/rm

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

unset_lscfg_test()
{
  if [ -e /fabos/link_sbin/lscfg_test ]; then
	$RM /fabos/link_sbin/lscfg_test 2> /dev/null
  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/sbin/license --show -lid
$ECHO ""
$ECHO "licenseshow:"
/fabos/sbin/license --show
$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
	unset_lscfg_test
	exit 1
fi

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

supportsave -c -n -i frudump.conf
}

valid_ip () {
	local ip=$1
	local stat=0
	#ipv4
	if expr "$ip" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
        	for i in 1 2 3 4; do
            		if [ $(echo "$ip" | cut -d. -f$i) -gt 255 ]; then
                		return 1
			fi
        	done
	else 
		#ipv6
		WORD="[0-9A-Fa-f]\{1,4\}"
		FLAT="^${WORD}\(:${WORD}\)\{7\}$"
		COMP2="^\(${WORD}:\)\{1,1\}\(:${WORD}\)\{1,6\}$"
		COMP3="^\(${WORD}:\)\{1,2\}\(:${WORD}\)\{1,5\}$"
		COMP4="^\(${WORD}:\)\{1,3\}\(:${WORD}\)\{1,4\}$"
		COMP5="^\(${WORD}:\)\{1,4\}\(:${WORD}\)\{1,3\}$"
		COMP6="^\(${WORD}:\)\{1,5\}\(:${WORD}\)\{1,2\}$"
		COMP7="^\(${WORD}:\)\{1,6\}\(:${WORD}\)\{1,1\}$"
		EDGE_TAIL="^\(\(${WORD}:\)\{1,7\}\|:\):$"
		EDGE_LEAD="^:\(:${WORD}\)\{1,7\}$"
 	
		echo $1 | grep --silent "\(${FLAT}\)\|\(${COMP2}\)\|\(${COMP3}\)\|\(${COMP4}\)\|\(${COMP5}\)\|\(${COMP6}\)\|\(${COMP7}\)\|\(${EDGE_TAIL}\)\|\(${EDGE_LEAD}\)"
		if [ $? != 0 ]; then
        		return 1
        	fi       
	fi
	return $stat
}
fruusb () {
        if [ $dirarg != 1 ]
        then
                echo 'Please specify a USB storage directory'
                fruusage
                unset_lscfg_test
                exit 1
        fi
        supportsave -U -d $frudir -i frudump.conf
        unset_lscfg_test
        exit 0
}

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.'
	$ECHO '       frudump -U -d <dir>'
}

#
# Main script starts
#

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
numargs=$#
numargs=$#
usrarg=0
passwdarg=0
hostarg=0
dirarg=0
transarg=0
supportftparg=0
usbarg=0

while getopts ":u:p:h:d:l:Ucioearfs" opt; do
    case $opt in 
       a  ) frustate
			frulicense
			frusensor
			frusys
			unset_lscfg_test
			exit 0 ;;
       r  ) frustate
			unset_lscfg_test
			exit 0 ;;
       f  ) frulicense
			unset_lscfg_test
			exit 0 ;;
       s  ) frusensor
			unset_lscfg_test
			exit 0 ;;
	   o  ) frusys
			unset_lscfg_test
			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 ;;
	   U  ) usbarg=1 ;;
       \? ) fruusage
			unset_lscfg_test
            exit 1 ;;
    esac
done
optargs=$OPTIND

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

if [ $usbarg == 1 ]
then
	fruusb
fi

if [ $usrarg == 1 ] || [ $hostarg == 1 ] || [ $dirarg == 1 ] || [ $transarg == 1 ]
then
	if [ -z "${fruusr}" ] || [ -z "${fruhost}" ] || [ -z "${frudir}" ] || [ -z "${frutrans}" ]
	then
		fruusage
		unset_lscfg_test
		exit 1
	fi
fi

$ECHO "This command collects RASLOG, FRU dump, core file, FFDC data"
$ECHO "and then transfer them to a FTP/SCP/SFTP server or a USB device."
$ECHO "This operation can take several minutes."

for i in {1..3}
do
	read -p "OK to proceed? (yes, y, no, n): [no] " option
	if [ $option == "n" ] || [ $option == "no" ]
	then
		unset_lscfg_test
		exit 0;
	fi
	 if [ $option == "y" ] || [ $option == "yes" ]
		then
			break
	else
		if [ $i ==  3 ]
		then
			$ECHO "Invalid input"
			unset_lscfg_test
			exit 0;
		fi
		$ECHO "Input not acceptable, please re-enter"
	fi
done

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

if [ $usrarg != 1 ] && [ $passwdarg != 1 ] && [ $transarg != 1 ] && [ $hostarg != 1 ] && [ $dirarg != 1 ]
then
        supportsave  -n -i frudump.conf
        unset_lscfg_test
        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 ]
then
	if [ "$fruusr" == "anonymous" ]
	then
		frupasswd=0
	else
		read -s -p "Password: " frupasswd
		passwdarg=1
		$ECHO ""
	fi
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
	unset_lscfg_test
	exit 1
fi

valid_ip $fruhost
        if [[ $? != 0 ]]
        then
                $ECHO "Invalid Host IP or Host Name"
                fruusage
                exit 0;
        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
		unset_lscfg_test
		exit 1
	fi
	supportsave -u $fruusr -p $frupasswd -n -h $fruhost -l $frutrans -d $frudir -i frudump.conf
fi
unset_lscfg_test
exit 0

