-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttsqueue-daemon.sh
executable file
·289 lines (238 loc) · 5.88 KB
/
ttsqueue-daemon.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
279
280
281
282
283
284
285
286
287
288
289
#!/bin/bash
# Check -> needed packages installed?
which inotifywait >/dev/null 2>&1;
if [ "$?" -eq 1 ]; then
echo -e ""
echo -e "ERROR: Needed command inotifywait not found. Please install package inotify-tools."
echo -e ""
exit 1;
fi
which md5sum >/dev/null 2>&1;
if [ "$?" -eq 1 ]; then
echo -e ""
echo -e "ERROR: Needed command md5sum not found. Please install package coreutils."
echo -e ""
exit 4;
fi
which cvlc >/dev/null 2>&1;
if [ "$?" -eq 1 ]; then
echo -e ""
echo -e "ERROR: Needed command cvlc not found. Please install package vlc-nox."
echo -e ""
exit 4;
fi
which curl >/dev/null 2>&1;
if [ "$?" -eq 1 ]; then
echo -e ""
echo -e "ERROR: Needed command curl not found. Please install package curl."
echo -e ""
exit 4;
fi
# Define ttsqueue root
TTSROOT=$(dirname $0)
# Do some checks
if [ ! -f $TTSROOT/ttsqueue.conf ]; then
echo -e ""
echo -e "ERROR: Config file ttsqueue.conf in $TTSROOT not found"
echo -e ""
exit 10;
else
# Load configuration
source $TTSROOT/ttsqueue.conf
# Check: Given user
if [ "$TTSUSER" == "root" ]; then
echo -e ""
echo -e "ERROR: TTSUSER can not be root! Please choose another user and try it again"
echo -e ""
exit 11;
else
TTSUSERID=$(cat /etc/passwd | grep $TTSUSER | cut -d ":" -f4)
if [ "$?" -ne 0 ] ; then
echo -e ""
echo -e "ERROR: TTSUSER $TTSUSER not found"
echo -e ""
exit 12;
fi
fi
# Check: Variable defined
if [ ! $TTSDELAY ]; then
echo -e ""
echo -e "ERROR: TTSDELAY not defined in ttsqueue.conf"
echo -e ""
exit 13;
fi
if [ ! $TTSQUEUE ]; then
echo -e ""
echo -e "ERROR: TTSQUEUE not defined in ttsqueue.conf"
echo -e ""
exit 14;
fi
# Check: FilePath exist
if [ ! -f $TTSINTRO ]; then
echo -e ""
echo -e "ERROR: Intro file $TTSINTRO does not exist"
echo -e ""
exit 15;
fi
if [ ! -d $TTSQUEUE ]; then
echo -e ""
echo -e "ERROR: Queue path $TTSQUEUE does not exist"
echo -e ""
exit 16;
fi
# Check: User can write
if [ ! -w $TTSQUEUE ]; then
echo -e ""
echo -e "ERROR: Queue path $TTSQUEUE is not writeable by user $TTSUSER"
echo -e ""
exit 17;
fi
if [ -f $TTSROOT/ttsqueue.log ]; then
if [ ! -w $TTSROOT/ttsqueue.log ]; then
echo -e ""
echo -e "ERROR: Log file $TTSROOT/ttsqueue.log is not writeable by user $TTSUSER"
echo -e ""
exit 18;
fi
fi
if [ -f $TTSROOT/ttsqueue.pid ]; then
if [ ! -w $TTSROOT/ttsqueue.pid ]; then
echo -e ""
echo -e "ERROR: File $TTSROOT/ttsqueue.pid is not writeable by user $TTSUSER"
echo -e ""
exit 19;
fi
fi
fi
# Function for displaying help
function usage {
echo -e ""
echo -e "Usage:"
echo -e " ttsqueue-daemon.sh [start|stop]"
echo -e ""
echo -e "Actions:"
echo -e ""
echo -e " start\t\t\tStart ttsqueue daemon."
echo -e " stopt\t\t\tStop ttsqueue daemon."
echo -e ""
}
function monitoringQueue {
# Start watching
inotifywait -q -m -e close_write "$TTSQUEUE" | while read DIRECTORY EVENTLIST EVENTFILE; do
# Build full qualified path
FILEPATH="$DIRECTORY/$EVENTFILE"
# Special: If file "stop" is created, stop daemon
if [ "$EVENTFILE" == "stop" ] ; then
echo -e "Daemon stopped"
# Remove stop file
rm $FILEPATH >/dev/null 2>&1
# Remove pid
rm $TTSROOT/ttsqueue.pid >/dev/null 2>&1
# Kill this and all child processes
kill -- $(ps -o pgid=$$ | grep -o '[0-9]*') >/dev/null 2>&1
killall inotifywait >/dev/null 2>&1
exit 0
else
# Some output for log file
date
cat $DIRECTORY/$EVENTFILE;
# Generate MD5 Checksum
MD5SUM=$(md5sum $DIRECTORY/$EVENTFILE | cut -d " " -f1);
# If cache file does not exist, create
if [ ! -f $TTSROOT/cache/$MD5SUM.mp3 ]; then
# Encode text string
SPEAKTEXT=$(cat $DIRECTORY/$EVENTFILE | od -An -tx1 | tr ' ' % | xargs printf "%s")
# Build TTS Request
TTSREQUEST=$(echo $TTSURL | sed "s/%SPEAKTEXT%/$SPEAKTEXT/g")
# Fetch file and store in cache
curl $TTSREQUEST -H 'Referer: http://translate.google.com/' -H 'User-Agent: stagefright/1.2 (Linux;Android 5.0)' > $TTSROOT/cache/$MD5SUM.mp3
fi
# Play
if [ -z "$TTSINTRO" ]; then
cvlc $TTSPLAYPARAM "$TTSROOT/cache/$MD5SUM.mp3"
else
cvlc $TTSPLAYPARAM "$TTSINTRO" "$TTSROOT/cache/$MD5SUM.mp3"
fi
echo -e ""
# Now remove file
if [ -w $DIRECTORY/$EVENTFILE ] ; then
rm $FILEPATH >/dev/null 2>&1
# Wait before next can be played
sleep $TTSDELAY
fi
fi
done > $TTSROOT/ttsqueue.log 2>&1 &
# Write pid to file
echo $! > $TTSROOT/ttsqueue.pid
}
# Handling with parameters
case $1 in
# Start daemon
start)
# Check is already running
if [ -f $TTSROOT/ttsqueue.pid ] ; then
PID=$(cat $TTSROOT/ttsqueue.pid | sed "s/\n//g")
ps -ef | grep $PID | grep -v grep >/dev/null
if [ "$?" -eq 0 ] ; then
echo "Daemon is already running"
exit 2;
else
rm $TTSROOT/ttsqueue.pid >/dev/null 2>&1
fi
fi
# Cleanup queue
rm $TTSQUEUE/* >/dev/null 2>&1
nohup sudo -u $TTSUSER $0 daemon >/dev/null 2>&1 &
if [ "$?" -eq 0 ] ; then
# Print status
echo -e "Daemon started"
fi
;;
# Get status
status)
# Check is already running
if [ -f $TTSROOT/ttsqueue.pid ] ; then
PID=$(cat $TTSROOT/ttsqueue.pid | sed "s/\n//g")
ps -ef | grep -v "grep" | grep $PID >/dev/null
if [ "$?" == "0" ] ; then
echo -e "Daemon running"
exit 0
else
echo "Daemon is not running"
exit 2;
fi
else
echo -e "Daemon is not running"
exit 3;
fi
;;
# Stop daemon
stop)
# Check is already running
if [ -f $TTSROOT/ttsqueue.pid ] ; then
PID=$(cat $TTSROOT/ttsqueue.pid | sed "s/\n//g")
ps -ef | grep -v "grep" | grep $PID >/dev/null
if [ "$?" == "0" ] ; then
echo -e "Daemon stopped"
echo "$TTS" > $TTSQUEUE/stop
exit 0
else
echo "Daemon is not running"
rm $TTSROOT/ttsqueue.pid >/dev/null 2>&1
exit 2;
fi
else
echo -e "Daemon is not running"
exit 3;
fi
;;
daemon)
monitoringQueue;
exit 0;
;;
*)
usage;
exit 20;
;;
esac
exit 0