#! /bin/bash

## TODO:
## add --print-diff-of-outputA-and-outputB


##
## debug mode on/off
##
#set -x


## on Linux use external $ACSROOT/bin/echo
if [ "`uname`" = "Linux" ]; 
	then enable -n echo
fi


THIS=`basename $0`


###
### ----------- Command Line Parsing ---------------------

#
# These will contain the parsing results (CL_XXX, CL = command line)
#
CL_HELP=
CL_PRINTPROG=
CL_PRINTTIME=
CL_EAGER=


#
# These options can be recognized (longopts comma-separated. colon means argument is required)
#
LONGOPTS=help,progress,time,eager
SHORTOPTS=hpte

#
# Usage info. Be nice and keep this up-to-date!
#
function printUsage {
   echo "Monitors the output (stdout & stderr) of a command for changes"
   echo ""
   echo "Usage: $THIS [OPTIONS] COMMAND"
   echo "Options: "
   echo "   -p | -progress             print dots to screen while monitoring"
   echo "   -t | -time                 print timestamps when output occurs"
   echo "   -e | -eager                monitor at high frequency (more cpu load)"
   echo "   -h | -help                 prints this help and exits"
}

#
# Run getopt (posixly_correct needed). We run twice:
# First run is simply to check the commandline for correctness
# Second run is the real deal which replaces the command line args with getopt's output
export POSIXLY_CORRECT=1

getopt -n `basename $0` -Q -u -a -l $LONGOPTS $SHORTOPTS "$@" || {
   printUsage
	exit 43;
}

set -- `getopt -u -a -l $LONGOPTS $SHORTOPTS "$@"`

#
# Iterate over getopt's output and set CL_XXX variables accordingly
#
while : 
do
	case "$1" in
	--progress)      CL_PRINTPROG=true ;;
	-p)              CL_PRINTPROG=true ;;
	--time)          CL_PRINTTIME=true ;;
	-t)              CL_PRINTTIME=true ;;
	--eager)         CL_EAGER=true ;;
	-e)              CL_EAGER=true ;;
	--help)          CL_HELP=true ;; 
	-h)              CL_HELP=true ;; 
	--) break ;;
	esac
	shift
done
shift

# restore 
export POSIXLY_CORRECT=
unset POSIXLY_CORRECT

if [ "$CL_HELP" ] ; then
   printUsage
   exit 0
fi

##
## read command
##
command=$@
if [ ! "$command" ] ; then
   printUsage
   exit 43
fi

### ---------- End of Command Line Parsing -------------




sleeptime=

## on Linux we can sleep for fractions of seconds
## but seems bash can only calculate with integers
if [ "`uname`" = "Linux" ] ; then
   if [ "$CL_EAGER" ] ; then
      sleeptime=0.5
   else
      sleeptime=1.5
   fi
else
   if [ "$CL_EAGER" ] ; then
      sleeptime=1
   else
      sleeptime=3
   fi
fi




##
## run command, show output
##
$command
retval=$?


##
## problems executing the command?
##
if [ ! $retval == 0 ] ; then
   exit $retval
fi



##
## repeat command
##

if [ "$CL_PRINTTIME" ] ; then
   echo ":: (`date`) ::"
fi
echo -n "::::     waiting for changes    ::::"


outputA=`$command 2>&1`
outputB=$outputA

while [ x"$outputB" == x"$outputA" ] ; do 

   sleep $sleeptime

   if [ "$CL_PRINTPROG" ] ; then
      echo -n "."
   fi
   
   outputB=`$command 2>&1`

done

echo ""
if [ "$CL_PRINTTIME" ] ; then
   echo ":: (`date`) ::"
fi


##
## run command, show output
##
$command



