#!/bin/sh

export PATH=$PATH:/fabos/link_bin:/bin:/usr/bin:/sbin:/usr/sbin:/fabos/link_abin:/fabos/link_sbin:/fabos/link_rbin:/fabos/factory:/fabos/xtool

#
# This script triggers regdump collection and copies the contents
# to core_files/ffdc_data
#
#!/bin/sh

# Need to identify if blade support proc-entry based db cmd, and
# Need to identify blade feature capability
# Input: slot#
# Return:  If Ok, blade_type & blade_id is set properly for the slot
# Otherwise, bladeid ==0, and blade_type == ""

asic_db_blade_type_() {

   # initialize to unknown blade id & type
	bladeid=0
	blade_type=""
	num_anvils=0
	num_zeus=0
	num_condors=0
	num_pinballs=0
	num_c2s=0

	if test -f /proc/fabos/blade/$1/info; then

		# ref. blade id is defined in linux/include/brocade/swbd.h		
		bladeid=`cat /proc/fabos/blade/$1/info | /bin/grep --binary-files=text bladeId | sed 's/[^0-9]//g'`

      case "$bladeid" in
		"37" ) blade_type="despina";num_c2s=1;;
		"55" ) blade_type="larissa";num_c2s=2;;
		"51" ) blade_type="triton";num_c2s=2;;
		"46" ) blade_type="hydra";num_c2s=2;;
		"56" ) blade_type="spirit";num_c2s=1;;
		"59" ) blade_type="tomahawk";num_c2s=1;;
		"53" ) blade_type="thor";num_c2s=9;;
		"52" ) blade_type="proteus";num_c2s=4;;
		"29" ) blade_type="sprint";num_condors=2;num_pinballs=2;; # sprint
		"27" ) blade_type="viking";num_condors=6;; # viking
		"24" ) blade_type="marathon";num_condors=2;num_pinballs=2;;   # marathon
		"33" ) blade_type="scimitar";num_condors=2;num_pinballs=2;; 
		"38" ) blade_type="sabre";num_condors=2;num_pinballs=2;; 
		"45" ) blade_type="elara";num_c2s=1;num_anvils=3;num_zeus=3;; # Elara
		"74" ) blade_type="europa";num_c2s=1;num_anvils=3;num_zeus=3;;
		"69" ) blade_type="spike";num_c2s=1;;
		"75" ) blade_type="striker";num_c2s=1;;
		"42" ) blade_type="mace";num_c2s=2;;
		"43" ) blade_type="lance";num_c2s=2;;
		"31" ) blade_type="ironman";num_condors=1;;   # ironman
		"18" ) blade_type="electron";num_condors=2;;   # 32-ports condor
		"16" ) blade_type="neutron";num_condors=2;;  # core condors
		"36" ) blade_type="positron";num_condors=2;;  # 48-ports condor
		"39" ) blade_type="xenon";num_condors=2;;  # xenon
		* ) blade_type="others";num_condors=1;; # pulsar/stealth/proton
      esac

	fi
	# return status
	if [ "$blade_type" = "" ]; then
		echo_xx 2> /dev/null;
	else
		echo 2> /dev/null
	fi	
}

start_asic_db_ffdc() {

	# Do not dump database on standby CP.
	/fabos/cliexec/hashow | /bin/grep Local | /bin/grep Standby > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		return
	fi
	
  # If db script not present, can't dump database
	if [ ! -e /fabos/link_sbin/db ]; then
		return 
	fi

  export DB_SILENT_ERR=1
  cd /proc/fabos/blade
  regdumpcount=0
  c1regdmpcnt=0
  for i in `ls -d [0-9]* | sort -n` ; do
	#
	# no shell cmd should intervene between asic_db_blade_type and 
	# check for return status stored in $?.
	# Note: "db" work on proc path slot#/mini#/chip#
	# This script assumes the following for ALL blade type.
	# The script has to be modified if the assumption becomes false.

	asic_db_blade_type_ $i
	if [ "$?" -eq 0 ]; then
		count=0
		while [ $count -lt $num_c2s ] 
		do 
			/fabos/sbin/c2regdump "$i"/$count/0
			mv /var/regdump.dmp /var/c2regdump.dmp."$i".$count
			gzip -f /var/c2regdump.dmp."$i".$count
			(( regdumpcount += 1 ))
			(( count += 1 ))
		done

		count=0
		while [ $count -lt $num_condors ] 
		do 
            /fabos/sbin/c1regdump "$i"/$count/0
            mv /var/regdump.dmp /var/c1regdump.dmp."$i".$count
            gzip -f /var/c1regdump.dmp."$i".$count
            (( c1regdmpcnt += 1 ))
			(( count += 1 ))
		done

		count=0
		anvil=4 # anvil starts at 4 for elara/europa
		while [ $count -lt $num_anvils ] 
		do 
			regdump "$i"/$anvil/0
			mv /var/regdump.dmp /var/avregdump.dmp."$i".$anvil
			gzip -f /var/avregdump.dmp."$i".$anvil 		
			(( count += 1 ))
			(( anvil += 1 ))
		done
	fi
  done
  unset DB_SILENT_ERR

  ffdc_time=`date +%Y-%m-%d-%H%M%S`	
  if [ $regdumpcount -gt 0 ]	
  then
  	#
	# Move all C2 reg dumps into a tar ball
	#
	/bin/cp /fabos/cliexec/c2hregdecode /var/ 
	/bin/cp /fabos/cliexec/c2hregcmp /var/ 

	tar -cvPf /core_files/ffdc_data/c2regdump-"$ffdc_time".tar.ffdc /var/c2regdump*gz /var/c2hregdecode /var/c2hregcmp > /dev/null 2>&1
	/bin/rm /var/c2regdump*gz
	/bin/rm /var/c2hregdecode
	/bin/rm /var/c2hregcmp
	sync
  fi

  if [ $c1regdmpcnt -gt 0 ]
  then
    #
    # Move all C1 reg dumps into a tar ball
    #
    /bin/cp /fabos/cliexec/c1hregdecode /var/
    /bin/cp /fabos/cliexec/c1hregcmp /var/

    tar -cvPf /core_files/ffdc_data/c1regdump-"$ffdc_time".tar.ffdc /var/c1regdump*gz /var/c1hregdecode /var/c1hregcmp > /dev/null 2>&1

    /bin/rm /var/c1regdump*gz
    /bin/rm /var/c1hregdecode
    /bin/rm /var/c1hregcmp
	sync
  fi

	#
	# Move all Anvil reg dumps into a tar ball
	#
	if [ $num_anvils -ne 0 ]; then
		tar -cvPf /core_files/ffdc_data/avregdump-"$ffdc_time".tar.ffdc /var/avregdump*gz > /dev/null 2>&1
		/bin/rm /var/avregdump*gz
		sync
	fi
}

start_asic_db_ffdc
