7a4251c30daf0bfd25eb3c19c828f38de494ea95
[kivitendo-erp.git] / xvfb-run
1 #!/bin/sh
2
3 # This script starts an instance of Xvfb, the "fake" X server, runs a command
4 # with that server available, and kills the X server when done.  The return
5 # value of the command becomes the return value of this script.
6
7 # This is a stripped-down version of the 'xvfb-run' script provided by
8 # Debian's 'xvfb' package which itself is licensed under the GPL, just as
9 # Lx-Office is. It is unclear who wrote the original script, but the
10 # CVS Id tag mentioned 'branden'.
11
12 set -e
13
14 PROGNAME=xvfb-run
15 SERVERNUM=99
16 AUTHFILE=
17 ERRORFILE=/dev/null
18 STARTWAIT=3
19 XVFBARGS="-screen 0 640x480x8"
20 LISTENTCP="-nolisten tcp"
21 XAUTHPROTO=.
22
23 # Query the terminal to establish a default number of columns to use for
24 # displaying messages to the user.  This is used only as a fallback in the event
25 # the COLUMNS variable is not set.  ($COLUMNS can react to SIGWINCH while the
26 # script is running, and this cannot, only being calculated once.)
27 DEFCOLUMNS=$(stty size 2>/dev/null | awk '{print $2}') || true
28 if ! expr "$DEFCOLUMNS" : "[[:digit:]]\+$" >/dev/null 2>&1; then
29     DEFCOLUMNS=80
30 fi
31
32 # Display a message, wrapping lines at the terminal width.
33 message () {
34     echo "$PROGNAME: $*" | fmt -t -w ${COLUMNS:-$DEFCOLUMNS}
35 }
36
37 # Display an error message.
38 error () {
39     message "error: $*" >&2
40 }
41
42 # Find a free server number by looking at .X*-lock files in /tmp.
43 find_free_servernum() {
44     # Sadly, the "local" keyword is not POSIX.  Leave the next line commented in
45     # the hope Debian Policy eventually changes to allow it in /bin/sh scripts
46     # anyway.
47     #local i
48
49     i=$SERVERNUM
50     while [ -f /tmp/.X$i-lock ]; do
51         i=$(($i + 1))
52     done
53     echo $i
54 }
55
56 SERVERNUM=$(find_free_servernum)
57
58 PATH=$PATH:/usr/bin/x11:/usr/X11R6/bin
59
60 if ! which xauth >/dev/null; then
61     error "xauth command not found"
62     exit 3
63 fi
64
65 # If the user did not specify an X authorization file to use, set up a temporary
66 # directory to house one.
67 XVFB_RUN_TMPDIR="${TMPDIR:-/tmp}/$PROGNAME.$$"
68 if ! mkdir -p -m 700 "$XVFB_RUN_TMPDIR"; then
69   error "temporary directory $XVFB_RUN_TMPDIR already exists"
70   exit 4
71 fi
72 AUTHFILE=$(tempfile -n "$XVFB_RUN_TMPDIR/Xauthority")
73
74 # Start Xvfb.
75 MCOOKIE=$(mcookie)
76 XAUTHORITY=$AUTHFILE xauth add ":$SERVERNUM" "$XAUTHPROTO" "$MCOOKIE" \
77   >"$ERRORFILE" 2>&1
78 XAUTHORITY=$AUTHFILE Xvfb ":$SERVERNUM" $XVFBARGS $LISTENTCP >"$ERRORFILE" \
79   2>&1 &
80 XVFBPID=$!
81 sleep "$STARTWAIT"
82
83 # Start the command and save its exit status.
84 set +e
85 DISPLAY=:$SERVERNUM XAUTHORITY=$AUTHFILE "$@" 2>&1
86 RETVAL=$?
87 set -e
88
89 # Kill Xvfb now that the command has exited.
90 kill $XVFBPID
91
92 # Clean up.
93 XAUTHORITY=$AUTHFILE xauth remove ":$SERVERNUM" >"$ERRORFILE" 2>&1
94 if [ -n "$XVFB_RUN_TMPDIR" ]; then
95     if ! rm -r "$XVFB_RUN_TMPDIR"; then
96         error "problem while cleaning up temporary directory"
97         exit 5
98     fi
99 fi
100
101 # Return the executed command's exit status.
102 exit $RETVAL