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