#!/bin/bash

#set -e

# Modify these as needed for your network of regression boards
ALL="M4200x8200 M4200x9490 M4200x8100 M4200x8160 M4500x8200 M4500x9490 M4500x8160 M4700x8200 M4700x8160 M4800x8200 M4800x8390 M4800x8160"
declare -A A DIO
M4200x8200=m4200x8200 ; A[M4200x8200]=ts4200
M4200x9490=m4200x9490 ; A[M4200x9490]=ts4200
M4200x8100=m4200x8100 ; A[M4200x8100]=ts4200-ts81x0-tsrelay8
M4200x8160=m4200x8160 ; A[M4200x8160]=ts4200-ts81x0

M4500x8200=m4500x8200 ; A[M4500x8200]=ts4500
M4500x9490=m4500x9490 ; A[M4500x9490]=ts4500
M4500x8160=m4500x8160 ; A[M4500x8160]=ts4500-ts81x0

M4700x8200=m4700x8200 ; A[M4700x8200]=ts4700
M4700x8160=m4700x8160 ; A[M4700x8160]=ts4700-ts81x0

M4800x8200=m4800x8200 ; A[M4800x8200]=ts4800
M4800x8390=m4800x8390 ; A[M4800x8390]=ts4800
M4800x8160=m4800x8160 ; A[M4800x8160]=ts4800-ts81x0

CAN=(m4500x8200 m4700x8200 m4800x8200 m4800x8390 m4800x8390:1 m4800x8160 m4800x8160:1 m7552) # m7553)
# m4800x8160
#
#m4500x8160 m4700x8160 m4800x8160 m4800x8160:1

# Step 1: make sure all regression boards are running the latest
# build.  If not, wait for them to.
for I in $ALL; do
    ARCH=${A[$I]}
    HOST=${!I}
    GOT=""
    while [ "$GOT" == "" ]; do
	eval `./product_linux86/tsctl $HOST System BuildInfo 2>/dev/null`
	T1=`date --date=@$BUILDTIME`
	TMP=`strings _$ARCH/tsctl.auto.o | grep "date:"`
	T2=${TMP:7:28}
	if [ "$T1" != "$T2" ]; then 
	    echo "Waiting for $HOST ($T1,$T2)"
	    TMP=`./rlock.sh ${!I} tsctl remote "true"`
	    sleep 1
	else
	    GOT="yes"
	fi
    done
done

# One-shot tests
# 1. verify that canctl --help terminates immediately
#for I in $ALL; do
#  echo -n -e "canctl $I\r"
#  TMP=`./rlock.sh ${!I} none remote "canctl --help"`
#done

i=0
SPACE0="                                         "
SPACE="                                                    "

DIO[ts4200]="32 32 32 34"
DIO[ts4500]="56 32"
DIO[ts4700]="62 128"
DIO[ts4800]="66 16 16 16 16"
DIO[ts81x0]="32"
DIO[tsrelay8]="8"

export PYTHONPATH=../tsctl_python
# ./SPITest.sh HOST
# works for m4200x8200, m4500x8200
while let $(( $i < 1000000 )); do 
    i=$(( $i+1 ))
    # 1. verify correct number of DIO
    echo -n -e "$SPACE$i    DIO Count   \r"
    for I in $ALL; do
	OIFS=$IFS
	IFS='-'
	DLIST=""
	TOTAL=0
	for J in ${A[$I]}; do
	    DLIST="$DLIST${DIO[$J]} "
	done
	IFS=$OIFS
	for K in $DLIST; do
	    let TOTAL=$TOTAL+$K
	done
	DLIST="$TOTAL $DLIST"
	#echo "${!I} - $DLIST"
	N=0
	for J in $DLIST; do
	    TMP=`./product_linux86/tsctl ${!I} DIO:$N Count`
	    if [ "$TMP" != "$J" ]; then
		echo;echo "$HOST DIO:$N Count is $TMP, expected $J ($DLIST)"
		exit
	    fi
	    let N=$N+1
	done
    done
    # 2. Run CAN test between all CAN instances
    for (( j=0; j<${#CAN[@]}; j++ )); do
	for (( k= $j+1; k<${#CAN[@]}; k++ )); do
            echo -n -e "$SPACE0$i CAN1-${CAN[$j]} ${CAN[$k]}   \r"
	    ./test/TestCAN.py ${CAN[$j]} ${CAN[$k]}
	    if [ "$?" != "0" ]; then exit; fi
	done
    done
    # 3. Run TestSystem.py on all boards
    for I in $ALL; do
	echo -n -e "$SPACE$i System-${!I}   \r"
	./test/TestSystem.py ${!I}
	if [ "$?" != "0" ]; then exit; fi
    done
    # 4. Run TestBus.py on all ts4500 boards
    for I in $ALL; do
        ARCH=`echo ${A[$I]} | cut -f1 -d-`
	if [ "$ARCH" != "ts4500" ]; then
	    continue
	fi
	echo -n -e "$SPACE $i   Bus-${!I}   \r"
	./test/TestBus.py ${!I}
	if [ "$?" != "0" ]; then exit; fi
    done
    # 5. Run TestBus3.py on all boards
    for I in $ALL; do
	echo -n -e "$SPACE  $i Bus3-${!I}   \r"
	./test/TestBus3.py ${!I}
	if [ "$?" != "0" ]; then exit; fi
    done
    # 6. diotest.py
    # This test currently has 5 failures
    #    echo -n -e "                                         $i DIO-42-82  \r"
    #    ./test/diotest.py $M4200x8200
    #    if [ "$?" != "0" ]; then exit; fi
    # This test currently has 14 failures
    #    echo -n -e "                                         $i DIO-42-94  \r"
    #    ./test/diotest.py $M4200x9490
    #    if [ "$?" != "0" ]; then exit; fi

    echo -n -e "$SPACE$i DIO-45-82   \r"
    ./test/diotest.py $M4500x8200
    if [ "$?" != "0" ]; then exit; fi
    echo -n -e "$SPACE$i DIO-45-94   \r"
    ./test/diotest.py $M4500x9490
    if [ "$?" != "0" ]; then exit; fi
    # This test currently has 2 failures
    echo -n -e "                                              $i DIO-47-82  \r"
    ./test/diotest.py $M4700x8200
    if [ "$?" != "0" ]; then exit; fi
    # This test currently has 12 failures
    #    echo -n -e "                                         $i DIO-47-82  \r"
    #    ./test/diotest.py $M4800x8200
    #    if [ "$?" != "0" ]; then exit; fi

    # config file is bad, can't find CN2_6
    #./test/diotest.py m4200x8160 ts8100.dioctl.config 8100
    echo -n -e "                                              $i DIO-47-8160\r"
    ./test/diotest.py m4700x8160 ts8100.dioctl.config 8100
    if [ "$?" != "0" ]; then exit; fi
    echo -n -e "                                              $i DIO-48-8160\r"
    ./test/diotest.py m4800x8160 ts8100.dioctl.config 8100
    if [ "$?" != "0" ]; then exit; fi
    # running diotest tests above without 8100 parms results in bad things

    # 7. Run TestPin.py on all ts4500 boards
    for I in $ALL; do
	#FIXME: fails on m4500x8160
        #ARCH=`echo ${A[$I]} | cut -f1 -d-`
        ARCH=${A[$I]}
	if [ "$ARCH" != "ts4500" ]; then
	    continue
	fi
	echo -n -e "$SPACE   $i Pin-${!I}   \r"
	./test/TestPin.py ${!I}
	if [ "$?" != "0" ]; then exit; fi
    done
    # 8. Run TestTime.py on all boards
    for I in $ALL; do
	echo -n -e "$SPACE  $i Time-${!I}   \r"
	./test/TestTime.py ${!I}
	if [ "$?" != "0" ]; then exit; fi
    done
    # 9. Run TestTime2.py on all boards
    for I in $ALL; do
	echo -n -e "$SPACE $i Time2-${!I}   \r"
	./test/TestTime2.py ${!I}
	if [ "$?" != "0" ]; then exit; fi
    done
    # 10. Run TestTWI.py on all boards
    for I in $ALL; do
	echo -n -e "$SPACE   $i TWI-${!I}   \r"
	./test/TestTWI.py ${!I}
	if [ "$?" != "0" ]; then exit; fi
    done
    # 11. Run TestTWI2.py on all TS-8160 boards
    for I in $ALL; do
        ARCH=`echo $I | cut -f2 -dx`
	if [ "$ARCH" != "8160" ]; then
	    continue
	fi
	echo -n -e "$SPACE  $i TWI2-${!I}   \r"
	./test/TestTWI2.py ${!I}
	if [ "$?" != "0" ]; then exit; fi
    done
    # 12. Run TestTWI3.py on all boards
    for I in $ALL; do
	echo -n -e "$SPACE  $i TWI3-${!I}   \r"
	./test/TestTWI3.py ${!I}
	if [ "$?" != "0" ]; then exit; fi
    done
    # 13. Test the MuxBus on all TS-81x0 boards
    for I in $ALL; do
        ARCH=`echo $I | cut -f2 -dx | cut -c1-2`
	if [ "$ARCH" != "81" ]; then
	    continue
	fi
	echo -n -e "$SPACE   $i MUX-${!I}   \r"
	TMP=`./product_linux86/tsctl ${!I} Bus:2 Peek16 0`
	if [ "$TMP" != "0x8100" ]; then 
	    echo "$TMP != 0x8100"
	    exit 1
	fi
    done
done
