#!/bin/bash -f
function usage {
echo "
   botch is like the unix command watch, but does not clear the screen.

   That makes it useful for Batch performance measurement of a given
   process.

   Usage:
     
      botch [-h] [-n number] [-tb or -a] command

      Terminate the execution with Ctrl-C (or some other signal).

   Options:
      h       Help
      n       The sleeptime between executions of command.
      t       Executes command in a timer, giving output of time -p.
      b       Backround the output of the given command, 
              (redirects the stdout to dev/null)
      a       Tablular output. Overrides options t and b. 
              This option prints the date/time, the execution Elapsed time,
              and the command executed, but not the output, as a table.
              The output is then suitable for plotting.   
   Examples:

      botch -h                          Prints this help

      botch caget QUAD:LI21:401:BACT    Executes the caget every 5 seconds

      botch -n 1 caget QUAD:LI21:401:BACT  Execute every 1 second

      botch -tn 10 ls                   Execute ls every 10 seconds, time 
                                        each exection.

      botch -tbn 10                     As above, but don't print the 
                                        the result of the ls.

      botch -a aidaget modelRmats//Design.Fullmachine
                                        Tabulate results. stdout is redirected
                                        to dev/null. Table consists of
                                        date time elapsed time (hh:mm.ss)
                                        botch_co (the name of botch co 
                                        routine, and the command. 
 
      botch -a aidaget QUAD:LI21:401//twiss | tee testdata 
                                        Using botch to execute aidaget to
                                        get some model data, capturing
                                        performance data on screen and in 
                                        a file.  

   Auth: Greg White, 16-Feb-2011
"
}
#----------------------------------------------------------------
# Mod: 
#================================================================
if [ $# -eq 0 ]; then
   echo "No arguments, see botch -h for help"
   exit 1;
fi

# Set Defaults
sleeptime=5
timer=
background=
tabulate=0

while getopts htabn: opt; do
   case "$opt" in
       h) usage; exit 0 ;;
       t) timer='time ' ;;
       n) sleeptime="${OPTARG}" ;;
       b) background=">/dev/null" ;;
       a) tabulate=1 ;; 
       *) echo "Unknown Arguments, see botch -h"; exit 1;;
   esac
done
shift $((OPTIND-1))
args="${*}"

if [ $tabulate -eq  0 ]; then
    cmd=${timer}${args}${background}
    while [ 1 ]
      do
      echo
      echo
      echo `date` ${args}
      eval ${cmd}
      sleep $sleeptime
    done
else
    while [ 1 ]
      do
      res=$( ( exec time -f'%e %C' botch_co ${args} 1>/dev/null ) 2>&1 )
      echo `date` $res
      sleep $sleeptime
    done
fi

