#!/bin/sh
##
##
##    Broadcom Proprietary and Confidential.
##    Copyright © 2019-2020 Broadcom. All Rights Reserved.
##    The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
##
##    File name:   mdb_cli_script
##


#
# Print usage
#
print_usage()
{
	echo "For internal debug use only, used by authorized support engineers."
	echo
}

#
# Parse arguments
#
if [ "$2" == "sw" ]; then
	dirpath=$1

	if test ! -f /proc/fabos/switch/$dirpath/sw_mdbg; then
		print_usage
		exit 0
	fi
	shift

	echo $* > /proc/fabos/switch/$dirpath/sw_mdbg
	cat /proc/fabos/switch/$dirpath/sw_mdbg
	exit 0
fi

if [ "$2" == "iu" ]; then
	dirpath=$1
	helpmsg=$3

	if test ! -f /proc/fabos/iu/iu_debug; then
		print_usage
		exit 0
	fi
	shift

	# TODO: Remove this after implementing separate debug proc entry for mdb command
	if [ "$helpmsg" == "help" ]; then
		print_usage
		exit 0
	fi

	echo $* > /proc/fabos/iu/iu_debug
	cat /proc/fabos/iu/iu_debug
	exit 0
fi

if [ "$2" == "fc" ]; then
        dirpath=$1
	helpmsg=$3

        if test ! -f /proc/fabos/fc/$dirpath/fc_debug; then
		print_usage
		exit 0
        fi
        shift

	# TODO: Remove this after implementing separate debug proc entry for mdb command
	if [ "$helpmsg" == "help" ]; then
		print_usage
		exit 0
	fi

        echo $* > /proc/fabos/fc/$dirpath/fc_debug
        cat /proc/fabos/fc/$dirpath/fc_debug
        exit 0
fi

if [ "$1" == "dbg" ] || [ "$2" == "dbg" ]; then
	dirpath=0/0/0
	slot=0
	spec=`echo $1 | sed 's/[0-9]\{1,\}/X/g'`

	if [ "$spec" = "X/X" ]; then
		dirpath=$1/0
		slot=`echo $1 | cut -f1 -d/`
		shift
	elif [ "$spec" = "X/X/X" ]; then
		dirpath=$1
		slot=`echo $1 | cut -f1 -d/`
		shift
	fi

	# Validate /proc entry existence
	if test ! -f /proc/fabos/blade/$dirpath/debug_mdb; then
		print_usage
		exit 0
	fi

	# Send command and display results
	echo $* > /proc/fabos/blade/$dirpath/debug_mdb
	cat /proc/fabos/blade/$dirpath/debug_mdb
	exit 0
fi

print_usage
exit 0
