#!/bin/bash

# This shell script is written by Panu Kalliokoski after xwall.pl, which
# was probably written by David L. Parsley, taken from a rvglug list
# posting.

# xwall.pl is a simple script for displaying a message on all X terminals
# connected to the server.  The first argument is a string to match in the
# output of ps to find all X terminals; there must be one per terminal.
# for GNOME, I use 'gnome-session'; for KDE you might try 'startkde'.

if test -z "$2"; then
	cat <<USAGE
Usage:	$0 match-string xmessage-params...
	match-string specifies the processes to examine
	xmessage-params are parameters passed to xmessage
USAGE
        exit 1
fi

procenv() {
	tr \\000 \\012 </proc/$1/environ | \
	grep "^$2=" | cut -d= -f2-
}

pattern=$1
shift
for pid in $(pidof -- $pattern); do
	dpy=$(procenv $pid DISPLAY)
	test -z "$dpy" && continue
	user=$(procenv $pid USER)
	xauth=$(procenv $pid XAUTHORITY)
	test -z "$xauth" && xauth=$(procenv $pid HOME)/.Xauthority
	echo $user $dpy $xauth
done | sort -u | tee /dev/stderr | \
while read user dpy xauth; do
	su $user -c \
	"env -i 'PATH=$PATH' 'DISPLAY=$dpy' 'XAUTHORITY=$xauth' xmessage $@" &
done

