#!/bin/sh
# copyright 2009 Alkis Georgopoulos <http://users.sch.gr/alkisg/>
# distributed under the terms of the GNU General Public License version 3
# or any later version.

if [ -z "$1" ]
then
	echo \
"Usage: $0 action [playback-method]

Valid actions are:
  start	Merge all user .Xauthority files to the callee user's .Xauthority,
	and modify gstreamer default video output device so that *any* video
	that is played with e.g. totem is broadcasted to all the LTSP displays.
  stop	Restore the default output device to 'autovideosink'.

Valid playback-methods are:
  ximagesink, xvimagesink, sdlvideosink...
"
	exit
elif [ "$1" = "stop" ]
then
	gconftool --unset /system/gstreamer/0.10/default/videosink
	exit
fi

playback_method="$2"
playback_method=${playback_method:-xvimagesink}

users=$(users | xargs -n 1 echo | uniq | grep -v $(whoami))

gst_videosink="tee name=t"

for user in $users
do
	pid=$(pgrep -u $user metacity)	# TODO: this is gnome-specific...
	display=$(sudo grep -z DISPLAY /proc/$pid/environ | sed -e 's/DISPLAY=//')
	echo "user=$user, display=$display"
	user_home=$(getent passwd "$user" | cut -d':' -f6)
	sudo xauth -f "$user_home/.Xauthority" extract - "$display" | xauth merge -
#	sudo cat "$user_home/.Xauthority" | xauth merge -
	gst_videosink="$gst_videosink ! queue ! ffmpegcolorspace ! $playback_method display=$display t."
done

gst_videosink="$gst_videosink ! queue ! ffmpegcolorspace ! xvimagesink t."
echo "gst_videosink=$gst_videosink"
gconftool --type=string --set /system/gstreamer/0.10/default/videosink "$gst_videosink"
echo "OK, now I'll open gstreamer-properties, go to the [Video] tab and press the [Test] button to test the Video [Default Output] device.
If it fails, try with ximagesink instead of the default xvimagesink.
If it succeeds, anything you open with totem will be automatically broadcasted."
gstreamer-properties
