#######################################################
#  Read arguments and store in valiables:
#   arg 0 = desired rate (rate)
#
#
####################################################### 

rate="$1"
#  Check if any of the engines has the desired rate 
#  already programmed in it
#eng_num=00
#((eng_num++))
#eng_pv='TPG:SYS2:2:DST00:SEQ'
#foot='DESC'
#if [ $eng_num -gt 10 ]
#  then 
#    eng=$eng_pv$eng_num$foot
#    echo "more than 10" $eng
#  else
#    zero=0
#    eng=$eng_pv$zero$eng_num$foot
#    echo $eng
#fi
#desc=$(caget TPG:SYS2:2:DST00:SEQ00DESC)
#desc=$(caget $eng)
#desc_value=$(echo $desc | cut -d' ' -f2)
#echo $desc_value

#  Compare the description of the engine with the desired rate 
#if [ $desc_value = $rate ]
#  then 
#    echo "The rate is already programmed in the engine"
#  else
#    echo "the rate needs to be scheduled"
#fi
#################################################################
#  variables:   						#
#  done      This variables is used as a flag to exit the	#
#            loop when we identify that there is a timing 	#
#            engine scheduled with the desired rate.		#
#  eng_num   This variable is used to identify a specific 	#
#	     sequence engine.					#
#################################################################
echo "start checking"
done=0 
eng_num=0
while [ $eng_num -le 63 ] #and $done == 0 ]
do
  eng_pv='TPG:SYS2:2:DST00:SEQ'
  desc='DESC'
  index='IDX'
  echo "engine number $eng_num"
    if [ $eng_num -gt 9 ]
      then
        eng_desc=$eng_pv$eng_num$desc
	echo "more than 9" $eng_desc
        eng_idx=$eng_pv$eng_num$index
	echo "more than 9" $eng_idx
      else # if less than 10
        zero=0
        eng_desc=$eng_pv$zero$eng_num$desc
        echo $eng_desc
	eng_idx=$eng_pv$zero$eng_num$index
	echo $eng_idx
    fi
  #sleep 3
  #read back description PV per sequence engine
  desc=$(caget $eng_desc)
  desc_value=$(echo $desc | cut -d' ' -f2)
  echo $desc_value
  #read back index PV per sequence engine
  index=$(caget $eng_idx)
  index_value=$(echo $index | cut -d' ' -f2)
  echo $index_value
  #  Compare the description of the engine with the desired rate 
  if [ $index_value -gt -1 ]
    then
      if [ $desc_value = $rate ]
        then
          echo "The rate is already programmed in the engine"
          done=1
          break	
        else
          echo "the rate needs to be scheduled"
      fi
    else
      echo "this sequence hasn't been used."
      done=2
      break
  fi
  ((eng_num++))
done
echo "Done = $done"
#Schedule the pre-programmed timing pattern 
dest_pv='TPG:SYS2:2:DST00'
if [ $done -eq  1 ]
  then 
    echo "scheduling the pre-programmed timing pattern" 
    idx_run=':RUNIDX'
    force_reset=':FORCERESET'
    idx_run=$dest_pv$idx_run
    force_reset=$dest_pv$force_reset
    #specify the sequence's index to run
    caget $idx_run
    caput $idx_run $index_value
    #force reset
    caget $force_reset
    caput $force_reset 1
    echo "timing pattern scheduled"
    caput $force_reset 0
  else 
    if [[ "$done" -eq 2 ]]
      then
        #the timing pattern requested hasn't been scheduled yet and we still have available seqeng
        source $rate
        echo "the requested timing pattern has been scheduled into the tpg"
    fi
fi
#end of code.
