#!/bin/sh 
# this is called by checkArduino to test and set configuration to proper state
# config file entries have the form:
#   assert command  action
# on boot the assert text is looked for in the ipmice status output
# if not found, ipmice <command> is run and 
# if action is specified, it is taken

cfg=$1
dno=$2
ipmice=`dirname $0`/ipmice
if [[ -f $cfg ]]; then
  while read line
  do
    chk=`echo $line | awk '{print $1}'`
    cmd=`echo $line | awk '{print $2}'`
    act=`echo $line | awk '{print $3}'`
    if [ "$chk" = "#" ]; then continue ; fi
    echo "checkArduinoConfig dev $dno chk $chk cmd $cmd act $act"
    stat=`$ipmice status $dno`
    if [ "`echo $stat | grep -i $chk`" = "" ]; then
      $ipmice $cmd $dno
      if [ "$act" = "REBOOT" ]; then touch /tmp/iceos.REBOOT ; fi
      if [ "$act" = "CYCLE" ]; then touch /tmp/iceos.POWERCYC ; fi
    fi
  done < "$cfg"
fi
