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