#! /bin/sh

#
# ident "@(#)utxprop.sh	1.2 04/05/11 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

PATH=/bin:/usr/bin
export PATH

MYNAME="$0"
MYNAME=`basename $0 2>/dev/null`

########################################################################
#
# Functions
# 
MODE=""

setMode() {
	if [ -z "$MODE" ] ; then
		MODE="$1"
	elif [ "$MODE" != "$1" ] ; then
		echo 1>&2 "$MYNAME: option -$1 conflicts with -$MODE"
		return 1
	fi
	return 0
}

getMode() {
	echo "$MODE"
}


usage() {
	echo 1>&2 "$MYNAME: use \"$MYNAME { -c | -g | -r |-s | -p <propname> }\""
	echo 1>&2 "         use \"$MYNAME -h\" for help"
}


help() {
	echo "$MYNAME: use \"$MYNAME <option>\" where <option> is one of"
	echo ''
	echo '    -c             # show the connection information property'
	echo '    -g             # show the session geometry property'
	echo '    -r             # show the remotely-visible session ID'
	echo '    -s             # show the local session ID'
	echo '    -p <propname>  # show the root-window string property <propname>'
	echo ''
	echo '    -h             # help (show this message)'

	return 0
}

showprop() {
	for XPATH in /usr/openwin/bin /usr/X11R6/bin /usr/X11/bin ; do
		XPROP="$XPATH/xprop"
		if [ -x "$XPROP" ] ; then
			break
		fi
	done
	unset XPATH

	if [ ! -x $XPROP ] ; then
		echo 1>&2 "$0: cannot find an executable xprop"
		return 1
	fi

	RESULT=`$XPROP -root "$1" | head -n 1`

	# xprop's exit status is always 0 so we can't use that to
	# distinguish between successful completion and failure.
	#
	# The output of xprop is either an error message (on stdout,
	# so we can't infer failure from an absence of stdout data)
	# of the form:
	#
	#    <propname>:  no such atom on any window
	#
	# or a successful report of the form:
	#
	#    <propname>(<type>) = "<value>"
	#
	# We look for a regexp closely matching the second form and
	# extract the <value> part.  If the regexp doesn't match then
	# we consider the result to have been an error report.  This
	# is more robust than playing "spot the error report", in which
	# case we'd still have to figure out how to extract the <value>
	# anyway.  In order to deal with the (admittedly unlikely) case
	# where the property exists but has a value equal to the empty
	# string, on a successful regexp match we prepend '%' to the
	# extracted value.  This means that we can recognise success by
	# the fact that the extracted <value> has non-zero length.  Of
	# course we discard the tell-tale leading '%' when we emit the
	# result.
	#
	# In theory we only deal with properties of <type> STRING, so
	# the part in parens should always be (STRING) and we could
	# treat anything else as an error.  That would be a little
	# harsh.  This implementation accepts all property types.
	# In the unlikely case that there are multiple properties
	# of the same name but of different types then we emit
	# only the first one returned by 'xprop'.  If things are
	# that crazy then you shouldn't be using this command, you
	# should be using 'xprop' directly.

  	PROPVAL=`printf "%s\n" "$RESULT" | sed -n 's/'"$1"'(.*)[ 	]*=[ 	]*"\(.*\)".*/%\1/p'`

	if [ -z "$PROPVAL" ] ; then
		printf "%s\n" 1>&2 "$MYNAME: $RESULT"
		return 1
	else
		printf "%s\n" "$PROPVAL" | sed 's/^%//'
		return 0
	fi
}

########################################################################
#
# Execution starts in earnest here
#

while getopts Dchgp:rs OPTCH; do

	case "$OPTCH" in

		D)
			set -x
			;;

		c|g|h|r|s)
			setMode "$OPTCH"
			if [ $? -ne 0 ] ; then
				usage
				exit 1
			fi
			;;
		p)
			setMode "$OPTCH"
			if [ $? -ne 0 ] ; then
				usage
				exit 1
			fi
			PROPNAME="$OPTARG"
			;;
		*)
			usage
			exit 1
			;;
	esac
done
shift `expr $OPTIND - 1`

if [ 0 -lt "$#" ] ; then
	usage
	exit 1
fi

# Initialise RET to a 'fail' (non-zero) value, in case this structure
# gets broken under maintenance
#
RET=1

case `getMode` in
	
	c)	showprop "_SUN_SUNRAY_CONN_INFO"
		RET=$?
		;;
	g)	showprop "_SUN_SUNRAY_SESSION_GEOMETRY"
		RET=$?
		;;
	h)	help
		RET=$?
		;;
	p)	showprop "$PROPNAME"
		RET=$?
		;;
	r)	showprop "_SUN_SUNRAY_HOME"
		RET=$?
		;;
	s)	showprop "_SUN_SUNRAY_SESSION"
		RET=$?
		;;
esac

exit $RET

