if [ -x /bin/which ] ; then which=/bin/which
elif [ -x /usr/bin/which ] ; then which=/usr/bin/which
else which=which ; fi

dirname=`$which dirname`
[ -x "$dirname" ] || dirname=/bin/dirname
cfg="`$dirname \"$0\"`/config"

usage() {
	echo "Usage: tailoutput [ -h host] UUID" >&2
	exit 1
}

if [ "$1" = "-h" ] ; then
	shift

	if [ $# -ne 2 ] ; then
		usage
	fi

	host=$1 ; shift
	id=$1; shift

	. "$cfg"

	(
        	echo "id=\"$id\"" ;
        	echo "prefix=\"$rprefix\"" ;
        	cat "$bindir/tailoutput.sh"
	) | eval $sshcmd
	exitcode=$?
	if [ $exitcode -eq 255 ] ; then
		echo "SSH client has failed for '$host'. Check that user and host names are correct, and the host is reachable. Click on 'Push SSH Key' button in Table View to verify remote host identity." >&2
		exit 255
	else
		exit $exitcode
	fi
elif [ $# -eq 1 ] ; then
	id="$1" ; shift

	. "$cfg"

	## The worker script will use $id and $prefix variables
	. "$bindir/tailoutput.sh"
else
	usage
fi
