-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.zsh
More file actions
executable file
·365 lines (310 loc) · 6.57 KB
/
clock.zsh
File metadata and controls
executable file
·365 lines (310 loc) · 6.57 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#! /usr/bin/env zsh
setopt extended_glob
# {{{ Functions
help () {
echo "$(basename $0) : configurable clock."
echo
echo "Dependancies : audio files in ~/audio/bell/clock"
echo
echo "Usage : $(basename $0) [status-file]"
echo
echo "[Status file format]"
echo
echo "variable = value"
echo
echo "Available variables"
echo
echo 'interval : time in minutes between chimes'
echo 'phase : the clock is shifted phase minutes from real time (defaults to zero)'
echo 'ante : number of minutes of the pre-chime ; 0 means no pre-chime'
echo 'post : number of minutes of the post-chime ; 0 means no post-chime'
echo 'chime : complexity of the instrumental chime : 2 means full, 1 simple, 0 for none'
echo 'vocal : complexity of the vocal chime : 2 means full, 1 simple, 0 for none'
echo 'volume : volume of chimes, between 0 and 100'
echo 'pause : whether to pause the clock ; it will only check for waking call every 15 minutes'
echo 'stop : whether to stop the clock'
echo 'weekend : whether to ring in weekend days'
echo
echo "[Signals]"
echo
echo "SIGUSR1 : stop the wait that syncs with minute start"
echo "SIGUSR2 : toggle pause"
echo "HUP, INT, TERM : save status in status file and stop"
exit 0
}
await () {
local delay=$1
# so as not to delay traps interception
sleep $delay &
waitpid=$!
wait $waitpid
}
echoerr () {
print "$@" >&2
}
player () {
local volume=$1
local fichier=$2
mpv-socket.bash add $fichier
mpv-socket.bash volume $volume
echo
}
horodate () {
date +"[=] %A %d %B %Y (o) %H : %M : %S | %:z | "
echo
}
wait-until-next-minute () {
local seconds=`date +%S`
local delay=$(( 60 - seconds ))
[ $seconds -gt 0 ] && {
echo We are $seconds seconds late, we sleep $delay seconds
echo
}
await $delay
}
echo-status-vars () {
echo interval : $interval
echo phase : $phase
echo ante : $ante
echo post : $post
echo chime : $chime
echo vocal : $vocal
echo volume : $volume
echo pause : $pause
echo stop : $stop
echo weekend : $weekend
echo
}
write-status-file () {
local statusfile=$1
local stamp=$2
echo "writing status file"
echo
cat <<- fin >| $statusfile
interval = $interval
phase = $phase
ante = $ante
post = $post
chime = $chime
vocal = $vocal
volume = $volume
pause = $pause
stop = $stop
weekend = $weekend
fin
touch $stamp
}
read-status-file () {
local statusfile=$1
local stamp=$2
[ $statusfile -nt $stamp ] || return 0
echo "reading status file"
echo
touch $stamp
while read ligne
do
ligne=${ligne// }
eval $ligne
done < $statusfile
echo-status-vars
player $volume $rewind
player $volume $tictac
echo
}
rest () {
local switch=$1
(( switch > 0 )) || return 0
echo "clock is paused, sleeping 15 minutes."
echo
await 900
wait-until-next-minute
}
halt () {
local halt=$1
(( halt > 0 )) || return 0
echo "halting wallpaper"
echo
stop=0
write-status-file $statusfile $stamp
break
}
what-time-is-it () {
# format 00 .. 23 & 00 .. 59
hour=$(date +%H)
minute=$(date +%M)
dayofweek=$(date +%w)
}
shift-clock () {
(( minute += phase ))
(( minute < 0 )) && {
(( hour -= 1 ))
(( minute += 60 ))
}
(( minute >= 60 )) && {
(( hour += 1 ))
(( minute -= 60 ))
}
hour=$(printf '%.2d' hour)
minute=$(printf '%.2d' minute)
echo time shifted $phase minutes : $hour:$minute
echo
}
check-interval () {
bell=0
(( modulo = minute % interval ))
(( moduloante = (minute + ante) % interval ))
(( modulopost = (minute - post) % interval ))
(( modulo == 0 )) && bell=1
(( moduloante == 0 )) && bell=1
(( modulopost == 0 )) && bell=1
(( weekend == 0 )) && (( dayofweek == 6 )) && bell=0
(( weekend == 0 )) && (( dayofweek == 0 )) && bell=0
echo minute modulo interval : $modulo
echo minute + ante modulo interval : $moduloante
echo minute - post modulo interval : $modulopost
echo
echo bell : $bell
echo
}
ring-bell () {
(( bell == 1 )) || return 0
(( pause > 0 )) && return 0
echo "ringing at $hour:$minute (shifted $phase minutes)"
echo
(( chime == 2 )) && {
cloche=$audiodir/chime-$hour-$minute.ogg
[[ -f $cloche ]] || cloche=$audiodir/chime-HH-$minute.ogg
[[ -f $cloche ]] || cloche=$audiodir/chime-HH-MM.ogg
echo "player $volume $cloche"
echo
player $volume $cloche
}
(( chime == 1 )) && {
echo "player $volume $simple_chime"
echo
player $volume $simple_chime
}
(( vocal == 2 )) && {
voix=$audiodir/vocal-$hour-$minute.ogg
[[ -f $voix ]] || voix=$audiodir/vocal-HH-$minute.ogg
echo "player $volume $voix"
echo
player $volume $voix
}
(( vocal == 1 )) && {
voix=$audiodir/vocal-HH-$minute.ogg
echo "player $volume $voix"
echo
player $volume $voix
#saytime
}
}
# }}}
# Traps {{{1
signal-stop-wait () {
echo
echo "stop waiting"
echo
[ -z $waitpid ] || kill $waitpid
waitpid=
}
signal-toggle () {
# toggle active / paused
if [ $pause -eq 0 ]
then
pause=1
else
pause=0
fi
write-status-file $statusfile $stamp
signal-stop-wait
}
signal-stop () {
echo "halting clock"
echo
stop=0
write-status-file $statusfile $stamp
signal-stop-wait
exit 128
}
trap signal-stop-wait SIGUSR1
trap signal-toggle SIGUSR2
trap signal-stop HUP INT TERM
# }}}1
# {{{ Already running ?
grep="/bin/grep --color=never"
ps auxww | $=grep -v grep | $=grep -v $$ | $=grep clock.zsh && {
echo "clock already running."
exit 0
}
# }}}
# {{{ Initialization
statusfile=~/racine/run/clock/clock.status
integer interval=15
integer phase=0
integer ante=0
integer post=0
integer chime=2
integer vocal=0
integer volume=100
integer pause=0
integer stop=0
integer intmin intmodulo
audiodir=~/audio/bell/clock
rewind=$audiodir/rewind.ogg
tictac=$audiodir/tictac.ogg
simple_chime=$audiodir/coucou-1.ogg
# }}}
# {{{ Arguments
numarg=$#
aide=0
while true
do
case $1 in
-h)
aide=1
break
;;
?*)
statusfile=$1
shift
;;
*)
break
;;
esac
done
# }}}
# Status file {{{1
stamp=${statusfile/.?*/.stamp}
[[ $statusfile = $stamp ]] && stamp=${stamp}.stamp
touch $statusfile
# }}}1
[ $numarg -eq 0 -o $aide -eq 1 ] && help
echo
echo '========================================================================'
date +" clock starting %A %d %B %Y (o) %H : %M : %S | %:z | "
echo '========================================================================'
echo
echo statusfile : $statusfile
echo stamp : $stamp
echo
read-status-file $statusfile $stamp
trap 1>&2
echo
wait-until-next-minute
# {{{ Loop
while true
do
horodate
read-status-file $statusfile $stamp
echo-status-vars
rest $pause
halt $stop
what-time-is-it
shift-clock
check-interval
ring-bell
wait-until-next-minute
done
# }}}