-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwayd.sh
executable file
·200 lines (184 loc) · 9.16 KB
/
wayd.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
#!/bin/bash
set -euo pipefail
NO_RESPONSE="${NO_RESPONSE:-no response}"
AVG_SLEEP="${AVG_SLEEP:-10800}"
LOGFILE="${LOGFILE:-$HOME/.wayd/log.csv}"
kdialog_args=(
kdialog
--inputbox
'What are you doing now?'
"$NO_RESPONSE"
)
zenity_args=(
zenity
--entry
--title="WAYD?"
--text="What are you doing now?"
--entry-text="$NO_RESPONSE"
)
declare -a ask_args
setup_dialog() {
if command -v zenity &> /dev/null ; then
ask_args=("${zenity_args[@]}")
>&2 echo "Using zenity frontend."
return 0
elif command -v kdialog &> /dev/null ; then
ask_args=("${kdialog_args[@]}")
>&2 echo "Using kdialog frontend."
return 0
else
>&2 echo "no dialog program found!"
exit 1
fi
return 1
}
setup_dialog
ask() {
timeout -k 60s 30s "${ask_args[@]}"
return $?
}
csv() {
local items=("$@")
# quote and escape as needed
# https://datatracker.ietf.org/doc/html/rfc4180
for i in "${!items[@]}"
do
items[$i]="\"${items[i]//\"/\"\"}\""
done
(
IFS=,
echo -n "${items[*]}"$'\r\n'
)
}
rndExpPctFn=(
976 1953 2931 3910 4890 5871 6853 7835 8819 9804 10790 11776 12764 13753 14742
15733 16724 17717 18711 19705 20701 21697 22695 23693 24693 25693 26695 27697
28701 29705 30711 31717 32725 33733 34743 35754 36765 37778 38792 39806 40822
41839 42857 43875 44895 45916 46938 47961 48985 50010 51037 52064 53092 54121
55152 56183 57216 58249 59284 60320 61357 62394 63433 64473 65515 66557 67600
68645 69690 70737 71784 72833 73883 74934 75986 77039 78093 79149 80205 81263
82322 83382 84443 85505 86568 87632 88698 89765 90832 91901 92971 94043 95115
96189 97263 98339 99416 100494 101574 102654 103736 104819 105903 106988 108074
109162 110251 111340 112432 113524 114617 115712 116808 117905 119003 120103
121204 122305 123409 124513 125619 126725 127833 128943 130053 131165 132278
133392 134507 135624 136742 137861 138982 140103 141226 142351 143476 144603
145731 146860 147991 149123 150256 151390 152526 153663 154801 155941 157082
158224 159368 160512 161658 162806 163955 165105 166256 167409 168563 169718
170875 172033 173193 174353 175516 176679 177844 179010 180178 181346 182517
183688 184861 186036 187212 188389 189567 190747 191929 193111 194295 195481
196668 197856 199046 200237 201430 202624 203819 205016 206214 207414 208615
209818 211022 212228 213435 214643 215853 217065 218277 219492 220707 221925
223144 224364 225586 226809 228034 229260 230488 231717 232948 234180 235414
236649 237886 239124 240364 241606 242849 244093 245339 246587 247836 249087
250339 251593 252849 254106 255364 256625 257886 259150 260415 261682 262950
264220 265491 266764 268039 269315 270593 271873 273154 274437 275721 277008
278295 279585 280876 282169 283463 284760 286057 287357 288658 289961 291266
292572 293880 295190 296501 297815 299129 300446 301765 303085 304407 305730
307056 308383 309712 311042 312375 313709 315045 316383 317722 319064 320407
321752 323099 324447 325798 327150 328504 329860 331218 332577 333939 335302
336667 338034 339403 340774 342147 343521 344898 346276 347656 349039 350423
351809 353197 354587 355978 357372 358768 360165 361565 362966 364370 365775
367183 368592 370004 371417 372833 374250 375670 377091 378514 379940 381368
382797 384229 385662 387098 388536 389976 391418 392862 394308 395756 397207
398659 400114 401570 403029 404490 405953 407418 408886 410355 411827 413301
414777 416255 417735 419218 420703 422190 423679 425170 426664 428160 429658
431158 432661 434166 435673 437182 438694 440208 441724 443243 444764 446287
447813 449341 450871 452403 453938 455476 457015 458557 460102 461648 463198
464749 466303 467860 469418 470980 472543 474110 475678 477249 478823 480399
481977 483558 485142 486728 488317 489908 491501 493098 494696 496298 497901
499508 501117 502728 504343 505959 507579 509201 510826 512453 514083 515716
517351 518989 520630 522273 523919 525568 527219 528874 530531 532190 533853
535518 537186 538857 540531 542207 543886 545569 547253 548941 550632 552325
554022 555721 557423 559128 560836 562547 564261 565977 567697 569420 571145
572874 574606 576340 578078 579818 581562 583309 585059 586812 588567 590326
592089 593854 595622 597394 599168 600946 602727 604511 606298 608089 609883
611680 613480 615283 617090 618900 620713 622530 624349 626173 627999 629829
631662 633499 635339 637182 639029 640879 642732 644589 646450 648314 650181
652052 653926 655804 657686 659571 661459 663352 665247 667147 669050 670956
672866 674780 676698 678619 680544 682473 684405 686341 688281 690225 692172
694123 696078 698037 700000 701966 703937 705911 707889 709872 711858 713848
715842 717840 719842 721848 723858 725872 727890 729912 731939 733969 736004
738043 740085 742132 744184 746239 748299 750363 752431 754504 756581 758662
760747 762837 764931 767030 769133 771241 773353 775469 777590 779715 781845
783980 786119 788262 790410 792563 794721 796883 799050 801221 803398 805579
807765 809955 812150 814351 816556 818766 820981 823200 825425 827655 829889
832129 834374 836623 838878 841138 843403 845673 847948 850229 852515 854806
857102 859403 861710 864022 866340 868663 870991 873325 875664 878009 880359
882714 885076 887443 889815 892193 894577 896966 899362 901763 904169 906582
909000 911425 913855 916291 918733 921181 923635 926095 928561 931033 933511
935996 938486 940983 943486 945996 948512 951034 953562 956097 958638 961186
963740 966301 968869 971443 974023 976611 979205 981805 984413 987027 989649
992277 994912 997554 1000203 1002859 1005522 1008192 1010869 1013554 1016246
1018945 1021651 1024365 1027086 1029815 1032551 1035294 1038045 1040804 1043570
1046344 1049126 1051915 1054712 1057517 1060330 1063151 1065980 1068817 1071662
1074515 1077376 1080245 1083123 1086009 1088903 1091806 1094717 1097637 1100565
1103502 1106448 1109402 1112365 1115337 1118317 1121307 1124305 1127313 1130330
1133355 1136390 1139434 1142488 1145551 1148623 1151704 1154796 1157896 1161007
1164127 1167257 1170397 1173546 1176706 1179875 1183055 1186245 1189445 1192655
1195876 1199107 1202348 1205600 1208863 1212136 1215420 1218715 1222021 1225338
1228665 1232004 1235354 1238716 1242088 1245473 1248868 1252275 1255694 1259125
1262567 1266021 1269487 1272966 1276456 1279959 1283474 1287001 1290541 1294093
1297658 1301236 1304827 1308430 1312047 1315677 1319320 1322976 1326646 1330329
1334026 1337737 1341461 1345199 1348952 1352718 1356499 1360294 1364103 1367927
1371766 1375620 1379488 1383372 1387270 1391184 1395114 1399058 1403019 1406995
1410987 1414995 1419019 1423060 1427116 1431190 1435280 1439386 1443510 1447651
1451809 1455984 1460177 1464388 1468616 1472862 1477127 1481409 1485711 1490030
1494369 1498726 1503102 1507498 1511913 1516347 1520802 1525276 1529771 1534285
1538820 1543376 1547953 1552551 1557169 1561810 1566472 1571156 1575862 1580590
1585340 1590114 1594910 1599729 1604572 1609438 1614328 1619242 1624180 1629143
1634131 1639143 1644181 1649244 1654333 1659448 1664590 1669758 1674953 1680174
1685424 1690701 1696006 1701339 1706701 1712092 1717512 1722962 1728441 1733951
1739491 1745062 1750664 1756298 1761964 1767662 1773393 1779156 1784953 1790784
1796649 1802549 1808484 1814454 1820460 1826502 1832581 1838698 1844852 1851044
1857274 1863544 1869853 1876202 1882592 1889023 1895495 1902010 1908567 1915168
1921813 1928502 1935236 1942015 1948841 1955714 1962635 1969603 1976621 1983688
1990805 1997974 2005194 2012467 2019793 2027173 2034608 2042099 2049646 2057251
2064913 2072635 2080418 2088261 2096166 2104134 2112166 2120264 2128427 2136657
2144956 2153324 2161763 2170274 2178858 2187516 2196249 2205060 2213949 2222918
2231968 2241100 2250317 2259619 2269009 2278488 2288057 2297719 2307475 2317327
2327278 2337328 2347480 2357737 2368100 2378571 2389153 2399848 2410659 2421588
2432638 2443812 2455111 2466540 2478101 2489797 2501631 2513607 2525729 2537999
2550421 2563000 2575739 2588642 2601715 2614960 2628383 2641988 2655782 2669768
2683953 2698341 2712940 2727755 2742793 2758061 2773565 2789313 2805314 2821574
2838103 2854910 2872005 2889397 2907096 2925115 2943464 2962156 2981204 3000622
3020425 3040628 3061247 3082300 3103806 3125785 3148258 3171248 3194778 3218876
3243568 3268886 3294862 3321530 3348929 3377100 3406087 3435940 3466712 3498461
3531251 3565152 3600243 3636611 3674351 3713572 3754394 3796954 3841405 3887925
3936716 3988009 4042076 4099235 4159859 4224398 4293391 4367499 4447541 4534553
4629863 4735223 4853006 4986538 5140688 5323010 5546154 5833836 6239301 6932448
)
rndExpEminus6() {
echo "${rndExpPctFn[$(($RANDOM % ${#rndExpPctFn[@]}))]}"
}
rndExp() {
LANG=C printf "%f\\n" "${rndExpPctFn[$(($RANDOM % ${#rndExpPctFn[@]}))]}e-6"
}
rndSleep() {
local r=$(rndExpEminus6)
LANG=C sleep "$(LANG=C printf "%f" "$((AVG_SLEEP * r))e-6")"
}
main() {
mkdir -p "$(dirname "$LOGFILE")"
outfile="$(mktemp)"
errfile="$(mktemp)"
finalize() {
rm -f "$outfile" "$errfile"
}
trap finalize EXIT
>&2 echo "Settings: LOGFILE=\"${LOGFILE}\" AVG_SLEEP=\"${AVG_SLEEP}\" NO_RESPONSE=\"${NO_RESPONSE}\""
>&2 echo "Entering main loop..."
while : ; do
rndSleep
date="$(date -u "+%Y-%m-%dT%H:%M:%S")"
if ask >"$outfile" 2>"$errfile" ; then
out="$(cat "$outfile")"
csv "$date" "$out" "" >> "$LOGFILE"
>&2 echo "$date saved response: $out"
else
err="$(cat "$errfile")"
csv "$date" "$NO_RESPONSE" "$err" >> "$LOGFILE"
>&2 echo "$date saved error: $err"
fi
done
}
main