#!/bin/sh

# xvfb-run - run the specified command in a virtual X server

# This script starts an instance of Xvfb, the "fake" X server, runs a
# command with that server available, and kills the X server when
# done.  The return value of the command becomes the return value of
# this script.
#
# If anyone is using this to build a Debian package, make sure the
# package Build-Depends on xvfb and xfonts-base.

set -e

if ! command -v xauth > /dev/null 2>&1; then
	echo "xvfb-run: xauth command not found; exiting." >&2
	exit 99
fi

# start Xvfb
rm -f ./Xauthority
MCOOKIE=$(mcookie)
XAUTHORITY=./Xauthority xauth add :99 . $MCOOKIE > /dev/null 2>&1
XAUTHORITY=./Xauthority Xvfb :99 -screen 0 640x480x8 -nolisten tcp \
	> /dev/null 2>&1 &
XVFBPID=$!
sleep 3

# start the command and save its exit status
set +e
echo $@
DISPLAY=:99 XAUTHORITY=./Xauthority $@ 2>&1
RETVAL=$?
set -e

# kill Xvfb and clean up
kill $XVFBPID
XAUTHORITY=./Xauthority xauth remove :99 > /dev/null 2>&1
rm ./Xauthority

# return the executed command's exit status
exit $RETVAL
