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