1b89ad
#!/bin/sh
1b89ad
# --- T2-COPYRIGHT-NOTE-BEGIN ---
1b89ad
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
1b89ad
# 
1b89ad
# T2 SDE: package/.../xorg-server/xvfb-run.sh
1b89ad
# Copyright (C) 2005 The T2 SDE Project
1b89ad
# Copyright (C) XXXX - 2005 Debian
1b89ad
# 
1b89ad
# More information can be found in the files COPYING and README.
1b89ad
# 
1b89ad
# This program is free software; you can redistribute it and/or modify
1b89ad
# it under the terms of the GNU General Public License as published by
1b89ad
# the Free Software Foundation; version 2 of the License. A copy of the
1b89ad
# GNU General Public License can be found in the file COPYING.
1b89ad
# --- T2-COPYRIGHT-NOTE-END ---
1b89ad
1b89ad
# $Id$
1b89ad
# from: http://necrotic.deadbeast.net/xsf/XFree86/trunk/debian/local/xvfb-run
1b89ad
1b89ad
# This script starts an instance of Xvfb, the "fake" X server, runs a command
1b89ad
# with that server available, and kills the X server when done.  The return
1b89ad
# value of the command becomes the return value of this script.
1b89ad
#
1b89ad
# If anyone is using this to build a Debian package, make sure the package
1b89ad
# Build-Depends on xvfb, xbase-clients, and xfonts-base.
1b89ad
1b89ad
set -e
1b89ad
1b89ad
PROGNAME=xvfb-run
1b89ad
SERVERNUM=99
1b89ad
AUTHFILE=
1b89ad
ERRORFILE=/dev/null
1b89ad
STARTWAIT=3
1b89ad
XVFBARGS="-screen 0 640x480x8"
1b89ad
LISTENTCP="-nolisten tcp"
1b89ad
XAUTHPROTO=.
1b89ad
1b89ad
# Query the terminal to establish a default number of columns to use for
1b89ad
# displaying messages to the user.  This is used only as a fallback in the event
1b89ad
# the COLUMNS variable is not set.  ($COLUMNS can react to SIGWINCH while the
1b89ad
# script is running, and this cannot, only being calculated once.)
1b89ad
DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
1b89ad
if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
1b89ad
    DEFCOLUMNS=80
1b89ad
fi
1b89ad
1b89ad
# Display a message, wrapping lines at the terminal width.
1b89ad
message () {
1b89ad
    echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
1b89ad
}
1b89ad
1b89ad
# Display an error message.
1b89ad
error () {
1b89ad
    message "error: $*" >&2
1b89ad
}
1b89ad
1b89ad
# Display a usage message.
1b89ad
usage () {
1b89ad
    if [ -n "$*" ]; then
1b89ad
        message "usage error: $*"
1b89ad
    fi
1b89ad
    cat <
1b89ad
Usage: $PROGNAME [OPTION ...] COMMAND
1b89ad
Run COMMAND (usually an X client) in a virtual X server environment.
1b89ad
Options:
1b89ad
-a        --auto-servernum          try to get a free server number, starting at
1b89ad
                                    --server-num
1b89ad
-e FILE   --error-file=FILE         file used to store xauth errors and Xvfb
1b89ad
                                    output (default: $ERRORFILE)
1b89ad
-f FILE   --auth-file=FILE          file used to store auth cookie
1b89ad
                                    (default: ./.Xauthority)
1b89ad
-h        --help                    display this usage message and exit
1b89ad
-n NUM    --server-num=NUM          server number to use (default: $SERVERNUM)
1b89ad
-l        --listen-tcp              enable TCP port listening in the X server
1b89ad
-p PROTO  --xauth-protocol=PROTO    X authority protocol name to use
1b89ad
                                    (default: xauth command's default)
1b89ad
-s ARGS   --server-args=ARGS        arguments (other than server number and
1b89ad
                                    "-nolisten tcp") to pass to the Xvfb server
1b89ad
                                    (default: "$XVFBARGS")
1b89ad
-w DELAY  --wait=DELAY              delay in seconds to wait for Xvfb to start
1b89ad
                                    before running COMMAND (default: $STARTWAIT)
1b89ad
EOF
1b89ad
}
1b89ad
1b89ad
# Find a free server number by looking at .X*-lock files in /tmp.
1b89ad
find_free_servernum() {
1b89ad
    # Sadly, the "local" keyword is not POSIX.  Leave the next line commented in
1b89ad
    # the hope Debian Policy eventually changes to allow it in /bin/sh scripts
1b89ad
    # anyway.
1b89ad
    #local i
1b89ad
1b89ad
    i=$SERVERNUM
1b89ad
    while [ -f /tmp/.X$i-lock ]; do
1b89ad
        i=$(($i + 1))
1b89ad
    done
1b89ad
    echo $i
1b89ad
}
1b89ad
1b89ad
# Parse the command line.
1b89ad
ARGS=$(getopt --options +ae:f:hn:lp:s:w: \
1b89ad
       --long auto-servernum,error-file:auth-file:,help,server-num:,listen-tcp,xauth-protocol:,server-args:,wait: \
1b89ad
       --name "$PROGNAME" -- "$@")
1b89ad
GETOPT_STATUS=$?
1b89ad
1b89ad
if [ $GETOPT_STATUS -ne 0 ]; then
1b89ad
    error "internal error; getopt exited with status $GETOPT_STATUS"
1b89ad
    exit 6
1b89ad
fi
1b89ad
1b89ad
eval set -- "$ARGS"
1b89ad
1b89ad
while :; do
1b89ad
    case "$1" in
1b89ad
        -a|--auto-servernum) SERVERNUM=$(find_free_servernum) ;;
1b89ad
        -e|--error-file) ERRORFILE="$2"; shift ;;
1b89ad
        -f|--auth-file) AUTHFILE="$2"; shift ;;
1b89ad
        -h|--help) SHOWHELP="yes" ;;
1b89ad
        -n|--server-num) SERVERNUM="$2"; shift ;;
1b89ad
        -l|--listen-tcp) LISTENTCP="" ;;
1b89ad
        -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;;
1b89ad
        -s|--server-args) XVFBARGS="$2"; shift ;;
1b89ad
        -w|--wait) STARTWAIT="$2"; shift ;;
1b89ad
        --) shift; break ;;
1b89ad
        *) error "internal error; getopt permitted \"$1\" unexpectedly"
1b89ad
           exit 6
1b89ad
           ;;
1b89ad
    esac
1b89ad
    shift
1b89ad
done
1b89ad
1b89ad
if [ "$SHOWHELP" ]; then
1b89ad
    usage
1b89ad
    exit 0
1b89ad
fi
1b89ad
1b89ad
if [ -z "$*" ]; then
1b89ad
    usage "need a command to run" >&2
1b89ad
    exit 2
1b89ad
fi
1b89ad
1b89ad
if ! which xauth >/dev/null; then
1b89ad
    error "xauth command not found"
1b89ad
    exit 3
1b89ad
fi
1b89ad
1b89ad
# If the user did not specify an X authorization file to use, set up a temporary
1b89ad
# directory to house one.
1b89ad
if [ -z "$AUTHFILE" ]; then
41b630
    XVFB_RUN_TMPDIR="$(mktemp --directory --tmpdir $PROGNAME.XXXXXX)"
41b630
    AUTHFILE=$(mktemp -p "$XVFB_RUN_TMPDIR" Xauthority.XXXXXX)
1b89ad
fi
1b89ad
1b89ad
# Start Xvfb.
1b89ad
MCOOKIE=$(mcookie)
41b630
41b630
XAUTHORITY=$AUTHFILE xauth source - << EOF >>"$ERRORFILE" 2>&1
41b630
add :$SERVERNUM $XAUTHPROTO $MCOOKIE
41b630
EOF
41b630
XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >>"$ERRORFILE" \
1b89ad
  2>&1 &
1b89ad
XVFBPID=$!
1b89ad
sleep "$STARTWAIT"
1b89ad
1b89ad
# Start the command and save its exit status.
1b89ad
set +e
1b89ad
DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1
1b89ad
RETVAL=$?
1b89ad
set -e
1b89ad
1b89ad
# Kill Xvfb now that the command has exited.
1b89ad
kill $XVFBPID
1b89ad
1b89ad
# Clean up.
1b89ad
XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >"$ERRORFILE" 2>&1
1b89ad
if [ -n "$XVFB_RUN_TMPDIR" ]; then
1b89ad
    if ! rm -r "$XVFB_RUN_TMPDIR"; then
1b89ad
        error "problem while cleaning up temporary directory"
1b89ad
        exit 5
1b89ad
    fi
1b89ad
fi
1b89ad
1b89ad
# Return the executed command's exit status.
1b89ad
exit $RETVAL
1b89ad
1b89ad
# vim:set ai et sts=4 sw=4 tw=80: