#!/bin/bash
################################################################################
#                                                                              #
#   Shell script: sprrestartboard -                                            #
#                     Individual Springware Board Restart Script.              #
#   Version 1: November 7, 2001.                                               #
#                                                                              #
#   DEPENDENCIES:                                                              #
#                                                                              #
#         1.  That dlstart has the variable TRACE set and exported             #
#             before running the command sctsassign so that the output can be  #
#             captured into the file $DLGCROOT/cfg/.sctsassign.output.         #
#                                                                              #
#   INPUTS:                                                                    #
#                                                                              #
#         Run as root                                                          #
#                                                                              #
#         ARG  PARAMETER  EXPLANATION                                          #
#         -b   BLT_ID     Springware only                                      #
#         -d              Debug which just generates and displays the commands #
#                         that would be executed.                              #
#         -t              More debug which does what debug (-d) and includes   #
#                         bash shell debug (set -x) turned on.                 #
#                                                                              #
#   OUTPUTS:                                                                   #
#                                                                              #
#         0  if successful                                                     #
#         >0 if there was a failure                                            #
#                                                                              #
#                                                                              #
#   SCRIPT FLOW:                                                               #
#                                                                              #
#         stopbd:      Stop each virtual board                                 #
#         genload:     Download the physical board                             #
#         startbd:     Start each virtual board                                #
#         timeslots:   Determine and prepare to create the timeslots for the   #
#                      physical board                                          #
#         sctsdtdxag:  Assign the timeslots for the physical board             #
#                                                                              #
#                                                                              #
#   "Copyright 2001.  Intel Corporation.                                       #
#                                                                              #
#   All Rights Reserved.  All names, products, and services mentioned          #
#   herein are the trademarks or registered trademarks of their respective     #
#   organizations and are the sole property of their respective owners."       #
#                                                                              #
################################################################################
   DLGCROOT=${DLGCROOT:-/usr/dialogic}      ; export DLGCROOT
DLGCROOTBIN=${DLGCROOTBIN:-${DLGCROOT}/bin} ; export DLGCROOTBIN
DLGCROOTCFG=${DLGCROOTCFG:-${DLGCROOT}/cfg} ; export DLGCROOTCFG
       PATH="$DLGCROOTBIN:$PATH"

  SCTTRACE=$DLGCROOTCFG/.sctsassign.output ### Created by dlstart with TRACE set
    VOXCFG=$DLGCROOTCFG/.voxcfg            ### Intel .voxcfg file
VOXCFGORIG=$VOXCFG.orig.$$                 ### Copy the original .voxcfg file
VOXCFG1BRD=$VOXCFG.1brd.$$                 ### Snapshot of 1 physical board
  TEMPFILE=/tmp/sprrestartboard.$$         ### Temp file for parsing devmapdump
EXIT_VALUE=0                               ### Exit value set to true
typeset -i DIS_BLT                         ### Decimal BLT display value

################################################################################
#Exit routine
################################################################################
alldone()
{	echo $0:  -b Springware_Physical_Board_Id
	[ "$1" ] && echo ERROR:  $1
	rm -f $VOXCFGORIG $VOXCFG1BRD $TEMPFILE
	let EXIT_VALUE=EXIT_VALUE+1 ### Always exit with at least 1 from here.
	exit $EXIT_VALUE
}

################################################################################
#Process the arguments, convert all blt to hex
################################################################################
unset BLT_LIST DEBUG
while getopts :b:dt INPUT
do
case $INPUT in
	b)	case $OPTARG in
			[0-9])      INHEX=$OPTARG;;
			[aA]|10)    INHEX=A;;
			[bB]|11)    INHEX=B;;
			[cC]|12)    INHEX=C;;
			[dD]|13)    INHEX=D;;
			[eE]|14)    INHEX=E;;
			[fF]|15)    INHEX=F;;
			*)	alldone "The board numbers can only be between 0(0X0) and 15(0XF).";;
		esac
		BLT_LIST="$BLT_LIST $INHEX"  ;;
	d) DEBUG=echo                    ;;
	t) DEBUG=echo ; set -x           ;;
	*) alldone "Bad arguments!"      ;;
esac
done
let NUMBER=OPTIND-1
shift $NUMBER
[     "$1"      ] && alldone "Extra arguments!"
[ ! "$BLT_LIST" ] && alldone "No physical board (BLT) ids were given!"

################################################################################
#Determine if linux commands are available:  awk, cp, rm, sed, sort
#type is a bash builtin with option -a to disregard hashing
################################################################################
for CMD in awk cp rm sed sort
do
	type -a $CMD >/dev/null 2>&1 || alldone "The command $CMD is not in path."
done

################################################################################
#Determine if .voxcfg and other files size > 0 and readable or executable
################################################################################
[ ! -r $VOXCFG   -o ! -s $VOXCFG   ] && alldone "NO $VOXCFG file or its empty."
[ ! -r $SCTTRACE -o ! -s $SCTTRACE ] && alldone "NO $SCTTRACE file or its empty."

for CMD in stopbd startbd genload sctsdtdxag devmapdump
do
	[ ! -x $DLGCROOTBIN/$CMD ] && alldone "NO $DLGCROOTBIN/$CMD executable."
done

################################################################################
#Copy .voxcfg so that it can be overwritten
################################################################################
cp $VOXCFG $VOXCFGORIG || alldone "Could not copy $VOXCFG to $VOXCFGORIG"

################################################################################
#For each hex BLT ID stop and restart the board and do time slot assignment.
################################################################################
for CUR_BLT in $BLT_LIST
do
	### Set the BLT variables for most cards.
	DIS_BLT=16#$CUR_BLT ### Convert to decimal for error display messages
	VOX_BLT=$CUR_BLT    ### Used for searching voxcfg file
	GEN_BLT=$CUR_BLT    ### Used as argument for genload

	### Get Offical board name from devmapserver
	if
		devmapdump -b $DIS_BLT -l 2 -n -t SPRINGWARE >$TEMPFILE
		RET=$?
		[ $RET != 0 ]
	then
		let EXIT_VALUE=EXIT_VALUE+$RET
		echo "Devmapdump failed for BLT $DIS_BLT(0X$CUR_BLT),"
		echo "skipping board!"
		continue
	fi

	### Parse the output to just the board name
	### Convert "/" in card names to "." to accommodate awk RE capabilities
	BOARD_NAME=`sed	-e 1,2d               \
			-e 's/^|.*\\\\-- <//' \
			-e 's/[ ]*#.*//'      \
			-e 's!/!.!g' $TEMPFILE`

	### Confirm board name for blt was available in devmapserver
	if
		[ ! "$BOARD_NAME" ]
	then
		let EXIT_VALUE=EXIT_VALUE+1
		echo "Board name not found for BLT $DIS_BLT(0X$CUR_BLT)"
		echo "in devmapserver, skipping board!"
		continue
	fi

	### Supercede the variables VOX_BLT and GEN_BLT for the special cards
	### D/82 and D/42 set BLT to 0 for voxcfg search
	### D/300 and D/600 gather extra board ids for genload
	case $BOARD_NAME in
		"D.82JCT-U"       ) VOX_BLT=0 ;;
		"D.82JCT-U Rev 2" ) VOX_BLT=0 ;;
		"D.42JCT-U"       ) VOX_BLT=0 ;;

		"D.300JCT-E1"     ) GEN_BLT=$CUR_BLT,2$CUR_BLT ;;
		"D.600JCT-2E1"    ) GEN_BLT=$CUR_BLT,2$CUR_BLT,1$CUR_BLT,3$CUR_BLT ;;

		"D.240JCT-T1"     ) GEN_BLT=$CUR_BLT,2$CUR_BLT ;;
		"D.480JCT-2T1"    ) GEN_BLT=$CUR_BLT,2$CUR_BLT,1$CUR_BLT,3$CUR_BLT ;;
	esac

	### Copy the virtual boards from voxcfg for this particular physical board
	### by using both the board name and the blt id for use by sctsdtdxag
	awk '$7 == "'"$VOX_BLT"'" && CARD_MATCH   { print > OUTPUTFILE ; }
		/^# Board Type - /                { CARD_MATCH=0       ; }
		/^# Board Type - '"$BOARD_NAME"'/ { CARD_MATCH=1       ; }
		' OUTPUTFILE=$VOXCFG1BRD $VOXCFG

	### Make sure $VOXCFG1BRD has content
	if
		[ ! -s $VOXCFG1BRD ]
	then
		let EXIT_VALUE=EXIT_VALUE+1
		echo "Board name <$BOARD_NAME> with BLT $DIS_BLT(0X$CUR_BLT)"
		echo "has no virtual boards in the file $VOXCFG, skipping board!"
		continue
	fi

	### Get a list of virtual boards from devmapserver
	if
		devmapdump -b $DIS_BLT -n -t SPRINGWARE >$TEMPFILE
		RET=$?
		[ $RET != 0 ]
	then
		let EXIT_VALUE=EXIT_VALUE+$RET
		echo "Board name <$BOARD_NAME> with BLT $DIS_BLT(0X$CUR_BLT)"
		echo "the executable devmapdump failed, skipping board!"
		continue
	fi

	### Parse the output to just the virtual board names
	VBOARD_LIST=`sed -e 1,3d              \
			-e 's/^|.*\\\\-- <//' \
			-e 's/>.*//' $TEMPFILE`

	### Confirm there is a list of virtual boards from devmapserver
	if
		[ ! "$VBOARD_LIST" ]
	then
		let EXIT_VALUE=EXIT_VALUE+1
		echo "Board name <$BOARD_NAME> with BLT $DIS_BLT(0X$CUR_BLT)"
		echo "has no virtual boards in devmapserver, skipping board!"
		continue
	fi

	### Stop the virtual boards
	for VBOARD_NAME in $VBOARD_LIST
	do
		if
			$DEBUG stopbd $VBOARD_NAME
			RET=$?
			[ $RET != 0 ]
		then
			let EXIT_VALUE=EXIT_VALUE+$RET
			echo "Board name <$BOARD_NAME> with BLT $DIS_BLT(0X$CUR_BLT)"
			echo "the executable stopbd failed, skipping board!"
			continue 2
		fi
	done

	### Download the physical base board
	if
		$DEBUG genload -i$GEN_BLT -CN
		RET=$?
		[ $RET != 0 ]
	then
		let EXIT_VALUE=EXIT_VALUE+$RET
		echo "Board name <$BOARD_NAME> with BLT $DIS_BLT(0X$CUR_BLT)"
		echo "the executable genload failed, skipping board!"
		continue
	fi

	### Start the virtual boards
	for VBOARD_NAME in $VBOARD_LIST
	do
		if
			$DEBUG startbd $VBOARD_NAME
			RET=$?
			[ $RET != 0 ]
		then
			let EXIT_VALUE=EXIT_VALUE+$RET
			echo "Board name <$BOARD_NAME> with BLT $DIS_BLT(0X$CUR_BLT)"
			echo "the executable startbd failed, skipping board!"
			continue 2
		fi
	done

	### Get a sorted list of timeslots for the physical base board. Sort has
	### -u because the time slot can be be listed twice in $SCTRACE
	TIMESLOTS=`
		for VBOARD_NAME in $VBOARD_LIST
		do
			### Skip fru virtual boards
			case $VBOARD_NAME in
				fru*)continue ;;
			esac
			### Get list of timeslots from TRACE output.
			### Valid timeslots are in lines that are
			### preceded by the virtual board name and
			### followed by a line that begins with "Assigned"
			awk "/^$VBOARD_NAME"'  */      { FLAG = 1 ; next ; }
				/^Assigned/            { FLAG = 0 ; next ; }
				$2 !~ "=" || $5 !~ "=" { FLAG = 0 ; next ; }
				{ if ( FLAG ) print $6 ; }' $SCTTRACE
		done | sort -gu`

	### Confirm that a list of timeslots were created
	if
		[ ! "$TIMESLOTS" ]
	then
		let EXIT_VALUE=EXIT_VALUE+1
		echo "Board name <$BOARD_NAME> with BLT $DIS_BLT(0X$CUR_BLT)"
		echo "has no time slots in $SCTTRACE, skipping board!"
		continue
	fi

	### Confirm no gap in time slot sequence and determine starting timeslot.
	COUNT=0 BASE=0 TEMP=0
	for TS in $TIMESLOTS
	do
		[ $COUNT -eq 0 ] && BASE=$TS
		let COUNT=COUNT+1
		let TEMP=BASE+COUNT-1
		if
			[ $TEMP -ne $TS ]
		then
			let EXIT_VALUE=EXIT_VALUE+1
			echo "Board name <$BOARD_NAME> with BLT $DIS_BLT(0X$CUR_BLT)"
			echo "has time slots that are not in order, skipping board!"
			continue 2
		fi
	done

	### The timeslot assignment program utilizes the .voxcfg file which just
	### contains the virtual boards for this physical board.
	cp $VOXCFG1BRD $VOXCFG || alldone "Could not copy the .voxcfg file."
	if
		$DEBUG sctsdtdxag -a -l -d -v -b$BASE -c$COUNT
		RET=$?
		[ $RET != 0 ]
	then
		let EXIT_VALUE=EXIT_VALUE+$RET
		echo "Board name <$BOARD_NAME> with BLT $DIS_BLT(0X$CUR_BLT)"
		echo "the executable sctsdtdxag failed, skipping board!"
		continue
	fi

	cp $VOXCFGORIG $VOXCFG || alldone "Could not copy the .voxcfg file."
	rm -f $VOXCFG1BRD ### Reset for next physical board
done
rm -f $VOXCFG1BRD $VOXCFGORIG $TEMPFILE
exit $EXIT_VALUE
