#!/bin/sh

# ddxwatch [-v var] [-d delay] [var]
# Shell script to watch the store status or a variable.
#
# Usage:
#  ddxwatch [-v var] [-d delay] [var]
#  - [var]: variable to watch ('get <var>')
#  - [delay]: watch delay (default = 0.5sec)
#  The default if no variable is specified is to watch the variable
#  counts
#
# Author:
#  Felix Duvallet
#  April 2008
#
# This is a rewrite of the old ddxwatch.

USAGE="\nddxwatch [-v var] [-d delay] [var]\n"
USAGE="$USAGE Shell script to watch the store status or a store variable.\n"
USAGE="$USAGE Usage: ddxwatch [-v var] [-d delay] [var]\n"
USAGE="$USAGE           - var: variable to watch.\n"
USAGE="$USAGE           - delay: delay for watch (default: 0.5sec).\n"
USAGE="$USAGE The default if no var is specified is to print the variable counts.\n"
USAGE="$USAGE \n"
USAGE="$USAGE Author: Felix Duvallet\n"

# Default values for delay, empty string for var
var=""
delay=0.5

# Parse the options (-v: variable, -d: delay)
while getopts v:d:h f
do
    case $f in
	v)
	    var=$OPTARG;;
	d)
	    delay=$OPTARG;;
	h)
	    echo "$USAGE";
	    exit 1;;
	\?)
	    echo "$USAGE";
	    exit 1;
	esac
done
shift `expr $OPTIND - 1`

# One argument only (no option letter) means to watch that variable.
if [ $# -gt 0 ]
then
    var=$1
fi

# Wath the variable or the variable counts (if strlen(var) = 0)
if [ $var ]
then
    watch -n $delay "echo 'get $var' | ddxsh"
else
    watch -n $delay "echo 'ls -l' | ddxsh"
fi

exit 0;
