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"
}

do_fast=0

#
# Process 1st argument
#
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_emu
cat /proc/fabos/blade/$dirpath/debug_emu

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