]> git.0d.be Git - empathy.git/blob - tools/with-session-bus.sh
Update tools from tp-glib 0.7.6
[empathy.git] / tools / with-session-bus.sh
1 #!/bin/sh
2 # with-session-bus.sh - run a program with a temporary D-Bus session daemon
3 #
4 # The canonical location of this program is the telepathy-glib tools/
5 # directory, please synchronize any changes with that copy.
6 #
7 # Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
8 #
9 # Copying and distribution of this file, with or without modification,
10 # are permitted in any medium without royalty provided the copyright
11 # notice and this notice are preserved.
12
13 set -e
14
15 me=with-session-bus
16
17 dbus_daemon_args="--print-address=5 --print-pid=6 --fork"
18
19 usage ()
20 {
21   echo "usage: $me [options] -- program [program_options]" >&2
22   echo "Requires write access to the current directory." >&2
23   exit 2
24 }
25
26 while test "z$1" != "z--"; do
27   case "$1" in
28   --session)
29     dbus_daemon_args="$dbus_daemon_args --session"
30     shift
31     ;;
32   --config-file=*)
33     # FIXME: assumes config file doesn't contain any special characters
34     dbus_daemon_args="$dbus_daemon_args $1"
35     shift
36     ;;
37   *)
38     usage
39     ;;
40   esac
41 done
42 shift
43 if test "z$1" = "z"; then usage; fi
44
45 exec 5> $me-$$.address
46 exec 6> $me-$$.pid
47
48 cleanup ()
49 {
50   pid=`head -n1 $me-$$.pid`
51   if test -n "$pid" ; then
52     echo "Killing temporary bus daemon: $pid" >&2
53     kill -INT "$pid"
54   fi
55   rm -f $me-$$.address
56   rm -f $me-$$.pid
57 }
58
59 trap cleanup INT HUP TERM
60 dbus-daemon $dbus_daemon_args
61
62 { echo -n "Temporary bus daemon is "; cat $me-$$.address; } >&2
63 { echo -n "Temporary bus daemon PID is "; head -n1 $me-$$.pid; } >&2
64
65 e=0
66 DBUS_SESSION_BUS_ADDRESS="`cat $me-$$.address`"
67 export DBUS_SESSION_BUS_ADDRESS
68 "$@" || e=$?
69
70 trap - INT HUP TERM
71 cleanup
72
73 exit $e