#!/bin/sh
# dogtail-run-headless
#
# Usage: dogtail-run-headless ./some-dogtail-script.py
#
# This script launches an X server using the virtual framebuffer, allowing 
# dogtail scripts to be run on headless servers. The server starts a minimal
# GNOME session, based on Nat Friedman's notes here: 
#  http://nat.org/2005/october/#Keep-It-Simple-Stupid
# Dogtail scripts are run in the current directory. After the script 
# terminates, so does the X server.
#
# The X server can be connected to using VNC via the following steps:
#
#  port=`"x11vnc -display :1 -viewonly -bg" | grep PORT)`
#  port=`echo "$port" | sed -e's/PORT=//'`
#  port=`expr $port - 5900`
#
#  vncviewer localhost:$port
#
# Author: Zack Cerza <zcerza@redhat.com>
#

A11Y_ENABLED=`gconftool-2 --get /desktop/gnome/interface/accessibility`

if test x$A11Y_ENABLED = xfalse; then 
	echo "Enabling accessibility support for GNOME"
	gconftool-2 --set --type bool /desktop/gnome/interface/accessibility true
fi

EXIT_CODE_FILE=/tmp/dogtail-run-headless.exitcode

mv -f ~/.xinitrc ~/.xinitrc-user 2>/dev/null && \
 echo "Moving ~/.xinitrc to ~/.xinitrc-user temporarily"

# xinit doesn't allow specifying an alternate xinitrc
cat << EOF > ~/.xinitrc
#!/bin/sh
gnome-settings-daemon &
gnome-panel &
nautilus -n &
metacity &
gconftool-2 --set --type bool /desktop/gnome/interface/accessibility true
sleep 10
cd $PWD && dogtail-detect-session && sh -c "$@"; echo -n \$? > $EXIT_CODE_FILE
EOF

echo "Starting GNOME Session..."
xinit -- /usr/X11R6/bin/Xvfb :1 -screen 0 1024x768x24 -fbdir /tmp :1

if test x$A11Y_ENABLED = xfalse; then
	echo "Re-disabling accessibility support for GNOME"
	gconftool-2 --set --type bool /desktop/gnome/interface/accessibility false
fi

rm -f ~/.xinitrc
mv -f ~/.xinitrc-user ~/.xinitrc 2>/dev/null && \
 echo "Restored ~/.xinitrc"

exit `cat $EXIT_CODE_FILE`
