1#!/bin/sh
2
3# This is a sample wrapper allowing to emulate the "xmessage" utility by
4# using "Xdialog" in the closest possible way. Note that when the "-buttons"
5# option is passed to this wrapper, "xmessage" is actually invoked as there
6# is (currently) no way to emulate exactly this option with Xdialog...
7
8OPTIONS="$*"
9BUTTONS=""
10FILE=""
11TITLE="Xmessage"
12WMCLASS="xmessage"
13XSIZE=80
14YSIZE=40
15PLACEMENT="--auto-placement"
16# It looks like "xmessage" does not like non-monospacing fonts and refuses
17# any "-fn" option with proportional fonts as parameter. We will not have to
18# worry about parsing the X "-fn" option then...
19FONT="--fixed-font"
20TIMEOUT=""
21
22if [ "$1" = "" ] ; then
23	echo "usage: Xmessage [-options] [message]
24
25where options include:
26  -file filename			file to read message from, \"-\" for stdin
27  -center				pop up at center of screen
28  -nearmouse				pop up near the mouse cursor
29  -timeout secs				exit with status 0 after "secs" seconds
30
31as well as folowing X generic options (the X options not listed are ignored):
32  -geometry WIDTHxHEIGHT[+X0+Y0]	size and origin for Xmessage window
33  -name ressource_name			sets the ressource name for Xmessage
34					(defaults to \"xmessage\")
35  -title string				title for the Xmessage window
36"
37	exit 0
38fi
39
40for i in $OPTIONS ; do
41
42	case $1 in
43		-file)
44			shift 1
45			FILE="$1"
46			;;
47		-center)
48			PLACEMENT="--screen-center"
49			;;
50		-nearmouse)
51			PLACEMENT="--under-mouse"
52			;;
53		-timeout)
54			shift 1
55			TIMEOUT="$1"
56			;;
57		-buttons|-default)
58			shift 1
59			BUTTONS="true"
60			;;
61		-print)
62			BUTTONS="true"
63			;;
64		-geometry)
65			shift 1
66			YSIZE="$1"
67			XSIZE=""
68			;;
69		-title)
70			shift 1
71			TITLE="$1"
72			;;
73		-name)
74			shift 1
75			WMCLASS="$1"
76			;;
77		-display|-bg|-background|-bd|-bordercolor|-bw|-borderwidth|-fg|-foreground|-fn|-font|-xnllanguage|-xrm)
78			echo "Xmessage: '$1' option ignored." 1>&2
79			shift 1
80			;;
81		-iconic|-rv|-reverse|+rv|-selectionTimeout|-synchronous)
82			echo "Xmessage: '$1' option ignored." 1>&2
83			;;
84		*)
85			if [ "$1" != "" ] ; then
86				TEXT="$1"
87			fi
88			;;
89	esac
90
91	shift 1
92done
93
94# We cannot emulate the "-buttons" option, so give up and use the true "xmessage"
95# if this option was specified in the command line...
96if [ "$BUTTONS" = "true" ] ; then
97	echo "Buttons related options (-buttons, -default, -print) not supported,"
98	echo "invoking \"xmessage\" intead..." 1>&2
99	xmessage "$OPTIONS"
100	exit $?
101fi
102
103# Now use "Xdialog"...
104if [ "$TIMEOUT" != "" ] ; then
105	# If a "-timeout" option was passed, then we must emulate it because
106	# only infoboxes use a timeout in Xdialog and we will use here either
107	# the textbox or the tailbox.
108	#
109	# So first start Xdialog as an asynchronous process...
110	if [ "$FILE" = "" ] ; then
111		echo "$TEXT" | \
112		Xdialog --title "$TITLE" --wmclass "$WMCLASS" $PLACEMENT $FONT --no-cancel \
113			--tailbox "-" $YSIZE $XSIZE &
114	else
115		Xdialog --title "$TITLE" --wmclass "$WMCLASS" $PLACEMENT $FONT --no-cancel \
116			--textbox "$FILE" $YSIZE $XSIZE &
117	fi
118	# Get Xdialog PID and build the source for the awk command that
119	# will be used to see if Xdialog is still running.
120	XDIALOG_PID=$!
121	SOURCE="{ if ( \$1 == $XDIALOG_PID ) print \$1 }"
122	# Now, as long as the TIMEOUT is not 0, sleep for one second, check
123	# for Xdialog still being there (if not then exit immediately) and
124	# decrement the TIMEOUT.
125	while (( $TIMEOUT > 0 )) ; do
126		sleep 1
127		STILL_THERE=`ps | awk --source "$SOURCE"`
128		if [ "$STILL_THERE" = "" ] ; then
129			exit 0
130		fi
131		let TIMEOUT=$TIMEOUT-1
132	done
133	# Time is over !   Kill Xdialog and exit.
134	kill $XDIALOG_PID 2>/dev/null
135	exit 0
136else
137	# No timeout, just start Xdialog synchronously then...
138	if [ "$FILE" = "" ] ; then
139		echo "$TEXT" | \
140		Xdialog --title "$TITLE" --wmclass "$WMCLASS" $PLACEMENT $FONT --no-cancel \
141			--tailbox "-" $YSIZE $XSIZE
142	else
143		Xdialog --title "$TITLE" --wmclass "$WMCLASS" $PLACEMENT $FONT --no-cancel \
144			--textbox "$FILE" $YSIZE $XSIZE
145	fi
146fi
147
148exit $?
149