afm-util: report correctly error status of dbus-send
[src/app-framework-main.git] / scripts / afm-util
1 #!/bin/sh
2
3 if [ "x" = "x${DBUS_SESSION_BUS_ADDRESS}" ]; then
4         DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$UID/bus"
5 fi
6 export DBUS_SESSION_BUS_ADDRESS
7
8 pretty() {
9         sed \
10                 -e '/^method return .*/d' \
11                 -e 's/^Error org.freedesktop.DBus.Error.Failed: "\?\(.*\)"\?$/ERROR: \1/' \
12                 -e 's/^   string "\(.*\)"/\1/' \
13                 -e 's/},/&\n/'
14 }
15
16 send() {
17         dbus-send --session --print-reply \
18                 --dest=org.AGL.afm.user \
19                 /org/AGL/afm/user \
20                 org.AGL.afm.user.$1 \
21                 "string:$2" |
22         pretty
23         return ${PIPESTATUS[0]}
24 }
25
26 case "$1" in
27
28   list|runnables)
29     send runnables true
30     ;;
31
32   add|install)
33     f=$(realpath $2)
34     send install '{"wgt":"'"$f"'","force":true}'
35     ;;
36
37   remove|uninstall)
38     i=$2
39     send uninstall "\"$i\""
40     ;;
41
42   info|detail)
43     i=$2
44     send detail "\"$i\""
45     ;;
46
47   ps|runners)
48     send runners true
49     ;;
50
51   run|start)
52     i=$2
53     send start "\"$i\""
54     ;;
55
56   run-remote|start-remote)
57     i=$2
58     send start '{"id":"'"$i"'","mode":"remote"}'
59     ;;
60
61   once)
62     i=$2
63     send once "\"$i\""
64     ;;
65
66   terminate|kill)
67     i=$2
68     send terminate "$i"
69     ;;
70
71   stop|pause)
72     i=$2
73     send pause "$i"
74     ;;
75
76   resume|continue)
77     i=$2
78     send resume "$i"
79     ;;
80
81   state|status)
82     i=$2
83     send state "$i"
84     ;;
85
86   -h|--help|help)
87     cat << EOC
88 usage: $(basename $0) command [arg]
89
90 The commands are:
91
92   list
93   runnables      list the runnable widgets installed
94
95   add wgt
96   install wgt    install the wgt file
97
98   remove id
99   uninstall id   remove the installed widget of id
100
101   info id
102   detail id      print detail about the installed widget of id
103
104   ps
105   runners        list the running instance
106
107   run id
108   start id       start an instance of the widget of id
109
110   once id        run once an instance of the widget of id
111
112   kill rid
113   terminate rid  terminate the running instance rid
114
115   stop rid
116   pause rid      pause the running instance rid
117
118   resume rid
119   continue rid   continue the previously paused rid
120
121   status rid
122   state rid      get status of the running instance rid
123
124 EOC
125     ;;
126
127   *)
128     echo "unknown command $1" >&2
129     exit 1
130     ;;
131 esac
132
133