#!/bin/bash

declare -a lep_PID_arr
declare -a evg_PID_arr

loop=true
echo "Start Carolina's test to find the bug"
while [ "$loop" = "true" ];do
	echo "Start Acquisition"
	caput EDEF:SYS0:14:CTRL 1
	#Selecting a random value between 100 and 2700
	num=$(python -S -c "import random; print random.randrange(100,2700)")       
	lepton_nuse=$(caget LEPTON:1:COUNTERHST14.NUSE | cut -d " " -f6 )
	evg_nuse=$(caget PATT:SYS0:1:PULSEIDHST14.NUSE | cut -d " " -f3 )
	echo "I am going to manual OFF this acquisition after acquiring at least $num of data "
	while [ "$evg_nuse" -lt $num ];do
	#	echo "still processing $evg_nuse"
		evg_nuse=$(caget LEPTON:1:COUNTERHST14.NUSE | cut -d " " -f6 )
		done
	caput EDEF:SYS0:14:CTRL 0
	echo "manual OFF"	
	lepton_nuse=$(caget LEPTON:1:COUNTERHST14.NUSE | cut -d " " -f6 )
	evg_nuse=$(caget PATT:SYS0:1:PULSEIDHST14.NUSE | cut -d " " -f3 )
	echo "Lepton NUSE is :  $lepton_nuse and EVG NUSE is : $evg_nuse "
	if [ "$lepton_nuse" -ne "$evg_nuse" ]
		then echo "I Found the bug !"
		loop=false	
	fi
	lepton_PID=$(caget LEPTON:1:COUNTERPIDHST14)
	#lep_PID_arr=$(echo $lepton_PID)
	
        evg_PID=$(caget PATT:SYS0:1:PULSEIDHST14)
	#evg_PID_arr=$(echo $evg_PID)
	#count=0
	#while [ "$count" -lt $num ]
        #if [ "${lep_PID_arr[$count]}" -ne "${evg_PID[$count]}" ]
        #        then echo "I Found the bug !"
        #        loop=false
        #fi
	echo "Lepton PID is :  $lepton_pid and EVG PID is : $evg_pid "
	echo "Done testing this acquisition, ready for the next one :)"
done



