print_usage()
{
	echo "  Usage: db [-h|-f] slot#/mini#/[chip#] module_name cmd arg1 ..."
	echo "  where  -h: help"
	echo "         -f: fast"
	echo "         Default chip #= 0"				
	echo "         Do 'db slot#/mini#/[chip#]' to display modules available"
	echo
	echo "  for switch related instructions"
	echo "  Usage: db switch# sw help ..."
	echo
	echo "  for fc related instructions"
	echo "  Usage: db fc# fc help ..."
    echo
    echo "  for iu related instructions"
    echo "  Usage: db 0 iu help ..."
	echo
	echo "  for blade related instructions"
	echo "  Usage: db bl# bl help ..."
	echo
	echo "  for changing default trace levels of switch,blade,fc drivers"
	echo " 	edit /etc/rc.d/init.d/fabos"
}

do_fast=0

#
# Process 1st argument
#


if [ "$2" = "sw" ]; then
	dirpath=$1
	
	if test ! -f /proc/fabos/switch/$dirpath/sw_debug; then
        	echo "no such file"
		if [ ! $DB_SILENT_ERR ]; then
                	echo "  db: switch# $dirpath values are invalid"
                	echo
                	print_usage
        	fi  
        	exit 0
	fi
	shift
	echo $* > /proc/fabos/switch/$dirpath/sw_debug
	cat /proc/fabos/switch/$dirpath/sw_debug
	exit 0
fi

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

    if test ! -f /proc/fabos/iu/iu_debug; then
            echo "no such file"
            exit 0
    fi
    shift
    echo $* > /proc/fabos/iu/iu_debug
    cat /proc/fabos/iu/iu_debug
    exit 0
fi


if [ "$2" = "fc" ]; then
        dirpath=$1
        if test ! -f /proc/fabos/fc/$dirpath/fc_debug; then
                echo "no such file"
		if [ ! $DB_SILENT_ERR ]; then
                        echo "  db: fc# $dirpath values are invalid"
                        echo
                        print_usage
                fi  
                exit 0
        fi  
        shift
        echo $* > /proc/fabos/fc/$dirpath/fc_debug
        cat /proc/fabos/fc/$dirpath/fc_debug
        exit 0
fi

if [ "$2" = "bl" ]; then
        dirpath=$1
        if test ! /proc/fabos/blade/$dirpath; then
                echo "no such file"
                if [ ! $DB_SILENT_ERR ]; then
                        echo "  db: bl# $dirpath values are invalid"
                        echo
                        print_usage
			exit 0
                fi
                exit 0
        fi
        cd /proc/fabos/blade/$dirpath
	prefix=`find . -type f -iname "*_debug" | sed s/_debug// | sed s_./__| xargs echo`
        
	filename=`find . -type f -iname "*_debug"`
        shift
	ans1=$1" "$2
	shift 
	shift
	ans=$ans1" "$prefix" "$dirpath" "$*
	echo $ans > /proc/fabos/blade/$dirpath/$filename
	cat /proc/fabos/blade/$dirpath/$filename
        exit 0
fi


if [ "$1" = "-h" ]; then
	print_usage
	exit 0
elif [ "$1" = "-f" ]; then
	do_fast=1
	shift
	dirpath=$1
	shift
else
	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
fi

#
# Need to turn on debug entry?
#
if [ -f /proc/fabos/blade/$dirpath/debug ]; then
	do_fast=1
else
	do_fast=0
	/fabos/rbin/debugentryon --slot $slot > /dev/null
fi

#
# Validate /proc entry existence
#
if test ! -f /proc/fabos/blade/$dirpath/debug; then
	if [ ! $DB_SILENT_ERR ]; then
		echo "  db: slot#/mini#/chip# <$dirpath> values are invalid"
		echo
		print_usage
	fi

	if [ $do_fast -eq 0 ]; then
		debugentryoff --slot $slot > /dev/null
	fi

	exit 0
fi

#
# Workaround for PASS1
#
if [[ $# -gt 3 && $1 == "anv_stat" && $2 == "dump" &&
	($3 == "prc" || $3 == "stats" || $3 == "all") ]]; then
	if test -f /proc/driver/anvil_rev; then
		rev=`cat /proc/driver/anvil_rev`
		if [[ $rev == "1.0"  || $rev == "1.1" ]]; then
			echo "Is traffic off on all ports? [y/n]"
			read ans

			case "$ans" in 
			y*|Y*)
				;;
			*)
				echo "*** Make sure traffic is off. Needed for ANVIL_PASS1. ***"
				echo "Dumping stats abandoned"
				exit 0
			esac
		fi
	fi
fi

#
# Send command and display results
#
echo $* > /proc/fabos/blade/$dirpath/debug
cat /proc/fabos/blade/$dirpath/debug

#
# Clean up
#
if [ $do_fast -eq 0 ]; then
	debugentryoff --slot $slot > /dev/null
fi
