#!/bin/sh

PROG="${0##*/}"
WATCH=/usr/bin/watch

[ -z $WATCH_SEC ] && WATCH_SEC=5


if [ ! -e $WATCH ]; then
	echo "SORRY: $WATCH not found!!"
	exit 1
fi

print_usage()
{
    [ "$1" = 0 ] || exec >&2
    
    echo "Usage: ${0##*/} [-s watch_sec | -d | -c confile.xml ] ObjectID1[@node],ObjectID2,.."
    echo " -d - dump state. Print state and exit"
    echo " -c confile - Set confile."
#    echo ""
#    uniset2-admin --help

    [ -n "$1" ] && exit "$1" || exit
}

#parse command line options
TEMP=`getopt -n $PROG -o h,d,c:,s: -l help,dump,confile:,watch_sec:,uniset-port: -- "$@"` || exit 1
eval set -- "$TEMP"

dump=
confile=
uport=
while :; do
    case "$1" in
	-h|--help) print_usage 0
	    ;;
	-d|--dump)
	    dump=1
	    ;;
	-s|--watch_sec)
	    shift
		WATCH_SEC="$1"
	    ;;
	-c|--confile)
	    shift
		confile="$1"
	    ;;
	--uniset-port)
	    shift
	    uport="--uniset-port $1"
	    ;;
	--) shift; break
	;;
	*) "unrecognized option: $1"
	exit 1
	;;
    esac
    shift
done

[ -n "$confile" ] && confile="--confile $confile"


[ -z "$dump" ] && $WATCH -n $WATCH_SEC uniset2-admin $confile --oinfo $* -- $uport
[ -n "$dump" ] && uniset2-admin $confile --oinfo $* -- $uport
