forked from BlackBoxe/kopimi-altar-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kopimi.sh
executable file
·278 lines (242 loc) · 5.75 KB
/
kopimi.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/sh
set +e
K_ME=${0##*/}
K_MY_DIR=${0%/*}
K_MY_PID=$$
K_VERSION="0.1"
k_usage() {
cat << _END_OF_USAGE_
Usage: $K_ME OPTIONS
KopimiBox master daemon
Options:
-C,--cfg-file FILE use the specified configuration file
-l,--log-file FILE log activity to the specified file
-L,--lib-dir DIR use the specified lib directory
(for helper functions & scripts)
-p,--pid-file FILE log pid to the specified file
-R,--run-dir DIR use the specified run directory
(for temporary files storage)
-v,--verbose be verbose (use multiple time to increase
verbosity level)
-V,--version display program version and exit
-h,--help display program usage and exit
_END_OF_USAGE_
}
k_version() {
cat << _END_OF_VERSION_
$K_ME v$K_VERSION, Copyright (C) 2014 BlackBoxe <[email protected]>
_END_OF_VERSION_
}
K_IS_ALIVE=0
K_TIME_START=0
K_LOG_LEVEL=0
k_error() {
echo "$K_ME: $@"
exit 1
}
k_get_options() {
while [ -n "$1" ]; do
case $1 in
-C|--config-file)
shift
K_OPT_CONFIG_FILE=$1
;;
-l|--log-file)
shift
K_OPT_LOG_FILE=$1
;;
-L|--lib-dir)
shift
K_OPT_LIB_DIR=$1
;;
-p|--pid-file)
shift
K_OPT_PID_FILE=$1
;;
-R|--run-dir)
shift
K_OPT_RUN_DIR=$1
;;
-v|--verbose)
K_OPT_LOG_LEVEL=$(($K_OPT_LOG_LEVEL + 1))
;;
-vv)
K_OPT_LOG_LEVEL=$(($K_OPT_LOG_LEVEL + 2))
;;
-vvv)
K_OPT_LOG_LEVEL=$(($K_OPT_LOG_LEVEL + 3))
;;
-h|--help)
k_usage
exit 0
;;
-V|--version)
k_version
exit 0
;;
*)
k_error "unknown option '$1'"
;;
esac
shift
done
}
k_now() {
date +%s
}
k_log() {
local level
local time_now
local time_str
local t
local h
local m
local s
level=$1
shift
[ $level -le $K_LOG_LEVEL ] || return
time_now=$(k_now)
t=$(($time_now - $K_TIME_START))
h=$(($t / 3600))
t=$(($t % 3600))
m=$(($t / 60))
s=$(($t % 60))
time_str=$(printf "%.2d:%.2d:%.2d" $h $m $s)
echo "$K_ME [$time_str][$level]: $@" >>$K_LOG_FILE
}
k_rel2abs() {
local p
local r
local d
local f
local wd
r=""
wd=$(pwd)
for p in $*; do
d=${p%/*}
f=${p##*/}
cd $d >/dev/null 2>&1 \
|| h_error "can't use directory '$d' ('$p')"
d=$(pwd)
cd $wd
r="${r}${r:+ }${d}/${f}"
done
echo "$r"
}
k_startup() {
K_IS_ALIVE=1
K_TIME_START=$(k_now)
RANDOM=$K_TIME_START
if [ -n "$K_OPT_CONFIG_FILE" ]; then
K_CONFIG_FILE=$K_OPT_CONFIG_FILE
elif [ -f $K_MY_DIR/kopimi.conf ]; then
K_CONFIG_FILE=$K_MY_DIR/kopimi.conf
else
k_error "can't find any config file, use a '--config-file' option"
fi
[ -r $K_CONFIG_FILE ] \
&& . $K_CONFIG_FILE \
|| K_error "can't read config file '$K_CONFIG_FILE'"
if [ -n "$K_OPT_LOG_LEVEL" ]; then
K_LOG_LEVEL=$K_OPT_LOG_LEVEL
fi
if [ -n "$K_OPT_LOG_FILE" ]; then
K_LOG_FILE=$K_OPT_LOG_FILE
elif [ -z "$K_LOG_FILE" ]; then
K_LOG_FILE=$K_MY_DIR/kopimi.log
fi
touch $K_LOG_FILE >/dev/null 2>&1 \
|| k_error "can't create log file '$K_LOG_FILE'"
K_LOG_FILE=$(k_rel2abs $K_LOG_FILE)
if [ -n "$K_OPT_PID_FILE" ]; then
K_PID_FILE=$K_OPT_PID_FILE
elif [ -z "$K_PID_FILE" ]; then
K_PID_FILE=$K_MY_DIR/kopimi.pid
fi
touch $K_PID_FILE >/dev/null 2>&1 \
|| k_error "can't create pid file '$K_PID_FILE'"
K_PID_FILE=$(k_rel2abs $K_PID_FILE)
[ -n "$K_LIB_DIR" ] \
|| k_error "can't use library directory, 'K_LIB_DIR' not set"
K_LIB_DIR=$(k_rel2abs $K_LIB_DIR)
[ -n "$K_RUN_DIR" ] \
|| k_error "can't use run directory, 'K_RUN_DIR' not set"
mkdir -p $K_RUN_DIR >/dev/null 2>&1 \
|| k_error "can't create run directory '$K_RUN_DIR'"
K_RUN_DIR=$(k_rel2abs $K_RUN_DIR)
[ -p "$K_CTL_FIFO" ] \
|| mkfifo $K_CTL_FIFO \
|| k_log 1 "ERROR: creating fifo '$K_CTL_FIFO'"
k_log 0 "starting"
k_log 1 "using config file: $K_CONFIG_FILE"
k_log 1 "using lib directory: $K_LIB_DIR"
k_log 1 "using run directory: $K_RUN_DIR"
for M in $K_LIB_DIR/[0-9][0-9]-*.sh; do
k_log 1 "loading module: $M"
. $M
done
echo "$K_MY_PID" > $K_PID_FILE
k_hook_call_handlers on_app_starting
k_hook_call_handlers on_app_started
trap k_quit INT TERM
k_log 0 "started"
}
k_cleanup() {
k_log 0 "ending"
k_hook_call_handlers on_app_ending
k_hook_call_handlers on_app_ended
rm $K_PID_FILE || \
k_log 1 "ERROR: removing pid-file '$K_PID_FILE'"
rm $K_CTL_FIFO || \
k_log 1 "ERROR: removing fifo '$K_CTL_FIFO'"
k_log 0 "ended"
}
k_loop() {
local timeout
while [ $K_IS_ALIVE -gt 0 ]; do
timeout=$(($RANDOM % ($K_IDLE_TIMEOUT_MAX - $K_IDLE_TIMEOUT_MIN) + $K_IDLE_TIMEOUT_MIN))
k_log 3 "waiting for event for $timeout seconds"
if read -t $timeout PROTO ACTION TYPE DEVICE STATE <> $K_CTL_FIFO ; then
k_log 3 "received event! Protocol: $PROTO, Action: $ACTION"
if [ $PROTO = "KOPIMI/0.1" ]; then
if [ $ACTION = "NOTIFY" ]; then
if [ $TYPE = "USB-STORAGE" ]; then
if [ $STATE = "ADDED" ]; then
k_log 2 "USB device '$DEVICE' plugged"
k_hook_call_handlers on_media_plugged "$DEVICE"
elif [ $STATE = "REMOVED" ]; then
k_log 2 "USB device '$DEVICE' removed"
k_hook_call_handlers on_media_removed "$DEVICE"
else
k_log 1 "ERROR: unknown USB device state '$STATE'"
fi
elif [ $TYPE = "BUTTON" ]; then
if [ $STATE = "PRESSED" ]; then
k_log 2 "button '$DEVICE' pressed"
k_hook_call_handlers on_button_pressed "$DEVICE"
else
k_log 1 "ERROR: unknown button state '$STATE'"
fi
else
k_log 1 "ERROR: unknown device type '$TYPE'"
fi
elif [ $ACTION = "QUIT" ]; then
K_IS_ALIVE=0
else
k_log 1 "ERROR: unknown event '$ACTION'"
fi
else
k_log 1 "ERROR: unknown protocol '$PROTO'"
fi
else
k_hook_call_handlers on_idle
fi
done
}
k_quit() {
K_IS_ALIVE=0
}
k_get_options $@
k_startup
k_loop
k_cleanup