devtools/data/xvfb-run.sh
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 13 Mar 2012 14:11:40 +0100
changeset 8299 e5d636081571
parent 8261 d4d9c88d4a5f
child 8327 4d76492508f3
permissions -rwxr-xr-x
[datafeed xml parser] fix typo
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8261
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     1
#!/bin/sh
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     2
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     3
# $Id: xvfb-run 2027 2004-11-16 14:54:16Z branden $
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     4
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     5
# This script starts an instance of Xvfb, the "fake" X server, runs a command
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     6
# with that server available, and kills the X server when done.  The return
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     7
# value of the command becomes the return value of this script.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     8
#
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
     9
# If anyone is using this to build a Debian package, make sure the package
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    10
# Build-Depends on xvfb, xbase-clients, and xfonts-base.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    11
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    12
set -e
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    13
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    14
PROGNAME=xvfb-run
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    15
SERVERNUM=99
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    16
AUTHFILE=
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    17
ERRORFILE=/dev/null
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    18
STARTWAIT=3
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    19
XVFBARGS="-screen 0 640x480x8"
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    20
LISTENTCP="-nolisten tcp"
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    21
XAUTHPROTO=.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    22
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    23
# Query the terminal to establish a default number of columns to use for
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    24
# displaying messages to the user.  This is used only as a fallback in the event
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    25
# the COLUMNS variable is not set.  ($COLUMNS can react to SIGWINCH while the
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    26
# script is running, and this cannot, only being calculated once.)
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    27
DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    28
if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    29
    DEFCOLUMNS=80
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    30
fi
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    31
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    32
# Display a message, wrapping lines at the terminal width.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    33
message () {
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    34
    echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    35
}
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    36
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    37
# Display an error message.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    38
error () {
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    39
    message "error: $*" >&2
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    40
}
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    41
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    42
# Display a usage message.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    43
usage () {
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    44
    if [ -n "$*" ]; then
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    45
        message "usage error: $*"
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    46
    fi
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    47
    cat <<EOF
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    48
Usage: $PROGNAME [OPTION ...] COMMAND
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    49
Run COMMAND (usually an X client) in a virtual X server environment.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    50
Options:
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    51
-a        --auto-servernum          try to get a free server number, starting at
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    52
                                    --server-num
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    53
-e FILE   --error-file=FILE         file used to store xauth errors and Xvfb
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    54
                                    output (default: $ERRORFILE)
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    55
-f FILE   --auth-file=FILE          file used to store auth cookie
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    56
                                    (default: ./.Xauthority)
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    57
-h        --help                    display this usage message and exit
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    58
-n NUM    --server-num=NUM          server number to use (default: $SERVERNUM)
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    59
-l        --listen-tcp              enable TCP port listening in the X server
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    60
-p PROTO  --xauth-protocol=PROTO    X authority protocol name to use
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    61
                                    (default: xauth command's default)
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    62
-s ARGS   --server-args=ARGS        arguments (other than server number and
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    63
                                    "-nolisten tcp") to pass to the Xvfb server
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    64
                                    (default: "$XVFBARGS")
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    65
-w DELAY  --wait=DELAY              delay in seconds to wait for Xvfb to start
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    66
                                    before running COMMAND (default: $STARTWAIT)
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    67
EOF
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    68
}
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    69
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    70
# Find a free server number by looking at .X*-lock files in /tmp.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    71
find_free_servernum() {
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    72
    # Sadly, the "local" keyword is not POSIX.  Leave the next line commented in
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    73
    # the hope Debian Policy eventually changes to allow it in /bin/sh scripts
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    74
    # anyway.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    75
    #local i
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    76
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    77
    i=$SERVERNUM
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    78
    while [ -f /tmp/.X$i-lock ]; do
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    79
        i=$(($i + 1))
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    80
    done
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    81
    echo $i
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    82
}
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    83
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    84
# Clean up files
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    85
clean_up() {
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    86
    if [ -e "$AUTHFILE" ]; then
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    87
        XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >>"$ERRORFILE" 2>&1
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    88
    fi
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    89
    if [ -n "$XVFB_RUN_TMPDIR" ]; then
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    90
        if ! rm -r "$XVFB_RUN_TMPDIR"; then
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    91
            error "problem while cleaning up temporary directory"
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    92
            exit 5
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    93
        fi
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    94
    fi
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    95
    if [ -n "$XVFBPID" ]; then
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    96
        kill $XVFBPID
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    97
    fi
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    98
}
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
    99
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   100
# Parse the command line.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   101
ARGS=$(getopt --options +ae:f:hn:lp:s:w: \
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   102
       --long auto-servernum,error-file:,auth-file:,help,server-num:,listen-tcp,xauth-protocol:,server-args:,wait: \
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   103
       --name "$PROGNAME" -- "$@")
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   104
GETOPT_STATUS=$?
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   105
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   106
if [ $GETOPT_STATUS -ne 0 ]; then
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   107
    error "internal error; getopt exited with status $GETOPT_STATUS"
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   108
    exit 6
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   109
fi
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   110
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   111
eval set -- "$ARGS"
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   112
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   113
while :; do
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   114
    case "$1" in
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   115
        -a|--auto-servernum) SERVERNUM=$(find_free_servernum); AUTONUM="yes" ;;
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   116
        -e|--error-file) ERRORFILE="$2"; shift ;;
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   117
        -f|--auth-file) AUTHFILE="$2"; shift ;;
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   118
        -h|--help) SHOWHELP="yes" ;;
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   119
        -n|--server-num) SERVERNUM="$2"; shift ;;
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   120
        -l|--listen-tcp) LISTENTCP="" ;;
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   121
        -p|--xauth-protocol) XAUTHPROTO="$2"; shift ;;
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   122
        -s|--server-args) XVFBARGS="$2"; shift ;;
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   123
        -w|--wait) STARTWAIT="$2"; shift ;;
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   124
        --) shift; break ;;
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   125
        *) error "internal error; getopt permitted \"$1\" unexpectedly"
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   126
           exit 6
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   127
           ;;
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   128
    esac
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   129
    shift
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   130
done
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   131
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   132
if [ "$SHOWHELP" ]; then
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   133
    usage
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   134
    exit 0
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   135
fi
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   136
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   137
if [ -z "$*" ]; then
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   138
    usage "need a command to run" >&2
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   139
    exit 2
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   140
fi
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   141
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   142
if ! which xauth >/dev/null; then
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   143
    error "xauth command not found"
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   144
    exit 3
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   145
fi
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   146
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   147
# tidy up after ourselves
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   148
trap clean_up EXIT
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   149
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   150
# If the user did not specify an X authorization file to use, set up a temporary
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   151
# directory to house one.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   152
if [ -z "$AUTHFILE" ]; then
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   153
    XVFB_RUN_TMPDIR="$(mktemp -d -t $PROGNAME.XXXXXX)"
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   154
    # Create empty file to avoid xauth warning
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   155
    AUTHFILE=$(tempfile -n "$XVFB_RUN_TMPDIR/Xauthority")
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   156
fi
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   157
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   158
# Start Xvfb.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   159
MCOOKIE=$(mcookie)
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   160
tries=10
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   161
while [ $tries -gt 0 ]; do
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   162
    tries=$(( $tries - 1 ))
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   163
    XAUTHORITY=$AUTHFILE xauth source - << EOF >>"$ERRORFILE" 2>&1
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   164
add :$SERVERNUM $XAUTHPROTO $MCOOKIE
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   165
EOF
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   166
    XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >>"$ERRORFILE" 2>&1 &
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   167
    XVFBPID=$!
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   168
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   169
    sleep "$STARTWAIT"
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   170
    if kill -0 $XVFBPID 2>/dev/null; then
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   171
        break
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   172
    elif [ -n "$AUTONUM" ]; then
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   173
        # The display is in use so try another one (if '-a' was specified).
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   174
        SERVERNUM=$((SERVERNUM + 1))
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   175
        SERVERNUM=$(find_free_servernum)
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   176
        continue
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   177
    fi
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   178
    error "Xvfb failed to start" >&2
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   179
    exit 1
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   180
done
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   181
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   182
# Start the command and save its exit status.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   183
set +e
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   184
DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   185
RETVAL=$?
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   186
set -e
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   187
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   188
# Return the executed command's exit status.
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   189
exit $RETVAL
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   190
d4d9c88d4a5f [devtools] make a copy of the xvfb-run script so we can kill it properly
Julien Cristau <julien.cristau@logilab.fr>
parents:
diff changeset
   191
# vim:set ai et sts=4 sw=4 tw=80: