Check for number of args
[slow/toolbox.git] / tracker-sandbox
1 #!/bin/sh
2
3 # Lightweight script for running test instances of Tracker. Less effort
4 # than running a full instance of gnome-session from jhbuild. Primary
5 # goal is to avoid messing with your actual data.
6
7 usage() {
8         echo "tracker-sandbox:"
9         echo "  Creates a lightweight test environment for Tracker, to avoid"
10         echo "  messing with your real data. The safest mechanism is to run"
11         echo "  the sandbox as a different user, but also supports running in"
12         echo "  your real user account but with a database in /tmp/tracker-test"
13         echo
14         echo "  Multiple instances of tracker-sandbox will share a session."
15         echo "  Currently the first instance owns the session and those started"
16         echo "  later will stop working once the first instance has exited."
17         echo
18         echo "Recommended usage:"
19         echo "  su <dummy user account>"
20         echo "  tracker-sandbox --user"
21         echo
22         echo "Alternative usage:"
23         echo "  tracker-sandbox"
24         echo
25         echo "Other options:"
26         echo "  --help                Show this information"
27         echo "  -p, --prefix DIR      Set up environment to use Tracker installed"
28         echo "                        in DIR (similar to 'jhbuild shell')"
29 }
30
31 PREFIX=
32 SEPARATE_USER_MODE=false
33
34 while [ $# -gt 0 ]; do
35         case $1 in
36                 --help)
37                         usage
38                         exit 0
39                         ;;
40                 --prefix|-p)
41                         shift
42                         if [ -z "$1" ]; then
43                                 echo "Error: --prefix option requires an argument"
44                                 exit 127
45                         fi
46                         PREFIX=$1
47                         ;;
48                 --user)
49                         SEPARATE_USER_MODE=true
50                         ;;
51                 *)
52                         echo "Error: unknown option $1"
53                         echo "Run '$0 --help' for help."
54                         exit 127
55                         ;;
56         esac
57         shift
58 done
59
60 DBUS_SESSION_BUS_PID=
61
62 set -o errexit
63
64 if [ "$SEPARATE_USER_MODE" != "true" ]; then
65         export DCONF_PROFILE=trackertest
66
67         TEMP_DIR=/tmp/tracker-test
68         mkdir -p $TEMP_DIR
69         export XDG_CACHE_HOME=$TEMP_DIR
70         export XDG_CONFIG_HOME=$TEMP_DIR
71         export XDG_DATA_HOME=$TEMP_DIR
72
73         export XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR:-$TEMP_DIR}
74
75         if [ ! -O "$XDG_RUNTIME_DIR" ]; then
76                 echo "Error: $XDG_RUNTIME_DIR is not writable by current user ($(whoami))."
77                 echo "Use the '--user' option if you have switched to a dummy user account."
78                 exit 1
79         fi
80 else
81         # We can't create a 'real' runtime dir without root, but for testing
82         # the security implications are irrelevant.
83         XDG_RUNTIME_DIR=/tmp/tracker-test-$(whoami)
84         mkdir -p $XDG_RUNTIME_DIR
85         export XDG_RUNTIME_DIR
86 fi
87
88 if [ -n "$PREFIX" ]; then
89         if [ ! -d "$PREFIX" ]; then
90                 echo "Error: unable to find prefix '$PREFIX'"
91                 exit 1
92         fi
93
94         # Interestingly, 'jhbuild run' *doesn't* alter PATH - I wonder why?
95         export PATH="$PREFIX/bin:$PATH"
96         export LD_LIBRARY_PATH="$PREFIX/lib:$PATH"
97         export XDG_DATA_DIRS="$PREFIX/share:$XDG_DATA_DIRS"
98
99         export TRACKER_DB_ONTOLOGIES_DIR="$PREFIX/share/tracker/ontologies"
100         export TRACKER_EXTRACTOR_RULES_DIR="$PREFIX/share/tracker/extract-rules"
101         export TRACKER_LANGUAGE_STOPWORDS_DIR="$PREFIX/share/tracker/languages"
102 fi
103
104 set -o nounset
105
106 if [ "$SEPARATE_USER_MODE" != "true" ]; then
107         echo -n "Running as $(whoami) with data in $TEMP_DIR"
108         [ -e "$TEMP_DIR/tracker/meta.db" ] && echo -n " (previously used)"; echo
109 else
110         echo "Running as $(whoami) using real Tracker store"
111 fi
112
113 if [ -n "$PREFIX" ]; then
114         echo "Using Tracker from $PREFIX"
115 fi
116
117 SESSION_FILE="$XDG_RUNTIME_DIR/tracker-sandbox"
118
119 # Slight race condition here if you start two instances simultaneously .. so don't
120 if [ ! -e "$SESSION_FILE" ]; then
121         eval $(dbus-launch --sh-syntax | tee $SESSION_FILE)
122
123         trap "rm \"$SESSION_FILE\"; /bin/kill $DBUS_SESSION_BUS_PID; exit" INT TERM EXIT
124
125         echo "[$DBUS_SESSION_BUS_PID] Launched new session at $DBUS_SESSION_BUS_ADDRESS"
126 else
127         eval $(cat $SESSION_FILE)
128
129         echo "Using existing session at $DBUS_SESSION_BUS_ADDRESS"
130 fi
131
132 sh
133
134 # Cleanup handled by 'trap' handler above.
135
136 # It would be nice if we could ref-count the session instead of pulling the rug
137 # out from under any other tracker-sandbox processes that are still running.
138