#!/bin/bash
########################################################################
# filename:	coswatch
#
# description:	This script is used to run in the background and will
#               set an endrecord flag after detecting a falling COS
#
# arguments:	number - seconds to wait for valid COS start
#          eg  ./coswatch 20 will start, if nothing for 20 secs, abort
#
# history:
# 20090704  V1.00    vk8sj  Original ...
#
#
########################################################################
#
source /home/irlp/custom/my-functions.sh   # test user & load environment
#
rm -f $LOCAL/endrecord                     # make sure flag file clear
if [ "$#" = "0" ] ; then                   # no timing data...
   echo "No timing data entered. Defaulting to 30 Sec"
   tictoc_stop="120"
  else
   let tictoc_stop=$1*4
fi
tictoc=1                                   # init var for loops
while [ TRUE ] ; do                        # create endless loop
   if ! $BIN/cosstate ; then               # wait for COS active..
    echo "COS Detected"                   # DEBUG only - uncomment to test
     while ! $BIN/cosstate ; do            # if COS still active..
       usleep 500                          # Take a nap (Short!)
     done                                  # loop till it drops
    echo "COS Released.."                 # DEBUG only - uncomment to test
     usleep 250000                         # napping again!
     while ! $BIN/cosstate ; do            # if COS came back..
       usleep 500                          # Take a nap (Short!)
     done                                  # loop till it drops
# add more loops if needed..
     echo "ok" > $LOCAL/endrecord          # set ready flag
     break                                 # end the endless loop (?)
   fi                                      # outa here..
usleep 250000
let tictoc+=1
if [ "$tictoc" = "$tictoc_stop" ] ; then   # times up yet?
   echo "fail" > $LOCAL/endrecord          # set flag as failure
   break                                   # jailbreak!!
fi
done                                       # continue endless loop

