-
Notifications
You must be signed in to change notification settings - Fork 0
/
u
executable file
·487 lines (475 loc) · 13.5 KB
/
u
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!/usr/bin/env zsh
# vim: foldmethod=indent
# This is a script to collect shell snippets until I develop them into real
# shell scripts. "u" stands for "utils". I got the idea from hans.
prog=${0##*/}
# Helper functions
function assert () {
"$@"
local ret=$?
if [[ $ret -ne 0 ]]; then
echo "Failed to execute '$@'." >&2
exit 3
fi
}
function start-bluetooth () {
if [[ $(systemctl is-active bluetooth) != active ]]; then
echo Starting bluetooth service ...
sudo systemctl start bluetooth
fi
}
# help and usage functions
function usage () {
echo "$prog [options] subcommand [options] [arguments ...]"
echo Possible subcommands are:
#sed -n '/^case/,/^esac$/{s/^ \(.*\))\( *# *\(.*\)\)\{0,1\}$/\1 \3/p;}' $0
sed -n '
/^case/,/^esac$/{
s/^ \([[:space:]]*(\{0,1\}[^()]*\))\( *# *\(.*\)\)\{0,1\}$/\1 \3/p
}' $ZSH_SCRIPT \
| while read arg help; do
printf '%s\t%s\n' $arg $help
done
}
# completion functions
# First define the completion function. Avoid name colisions.
function _lucs_util_scipt_completion_function () {
local state
# First handle global options. Everything that does not match a global
# option as defined here is handled later. The $state is set to
# "subcommand" in this case.
_arguments \
'(- *)-h[show help]' \
'(-h -x)-x[debug output]' \
':subcommand:->subcommand' \
'*::options:->options' \
&& return 0
case $state in
subcommand)
local -a subcommands_array
subcommands_array=(
'help:show help'
'edit:open the script itself in $EDITOR'
'bk:mount the external backup hd and make a backup'
'phone:some phone related subcommands'
'split-file-at-empty-lines:split a text files into several files'
'backup:foo todo'
'urxvt-font:set the font in this urxvt'
'logs:show different logs'
'setenv:set an environment var in a running process'
'bank:open the bank browser'
'python-profile:create a png from a python profileing run'
'test-vim-startup:mesure and sort nvim startup time'
'cmp-git-branch:run command on this branch and master (-u)'
'true-color-test:test true color support of the terminal'
'print:stage files for printing at the university (or --flush)'
'time-management:control the time management warnings'
'show-term-attributes:show all special font attributes of the terminal'
'updateinfo:show info about upgradable packages with pacman'
'nvim-nightly:'
)
# Use this array to complete the subcommands.
_describe -t subcommands 'util subcommands' subcommands_array \
&& ret=0
;;
options)
# TODO what is this?
curcontext="${curcontext%:*}-${words[1]}:"
case $words[1] in
split-file-at-empty-lines) _arguments '*:: :_files';;
urxvt-font) _arguments '*:: :_x_fonts';; # doesn't work yet
print)
_arguments \
'--flush[flush the print queue now]:lp options:_u_helper_lp_options' \
'*:: :_files';;
phone)
_arguments \
':subcommand:->subcommand' \
'*:: :_files'
subcommands_array=(
put-vcf:'push contacts from a file to the phone'
savecontacts:'backup all contacts from phone to a file'
)
[[ $state = subcommand ]] \
&& _describe -t subcommands 'phone subcommands' subcommands_array \
&& ret=0
;;
logs)
_arguments ':subcommand:->subcommand' '*:: :_files'
subcommands_array=(
{fetchmail,mailin,pop}:'logs for fetchmail'
{smtp,mailout}:'logs for msmtp'
{backup,bk}:'logs for different backup scripts'
)
[[ $state = subcommand ]] \
&& _describe -t subcommands 'logs subcommands' subcommands_array \
&& ret=0
;;
time-management)
_arguments ':subcommand:->subcommand' '*:: :_files'
subcommands_array=(
list:'list the timers'
reset:'reset the timer'
start:'start a timer'
stop:'stop all timers'
pause:'pause all timers'
continue:'continue paused timers'
)
[[ $state = subcommand ]] \
&& _describe -t subcommands 'time management subcommands' subcommands_array \
&& ret=0
;;
esac
;;
*) _message 'Error in completion function.';;
esac
}
#sed -n '/^case/,/^esac$/{s/^ \(.*\))\( *# *\(.*\)\)\{0,1\}$/\1 \3/p;}' $0
# Last define some helper functions
function _u_helper_lp_options () {
local service=lp
_lp $@
}
# SUBCOMMAND FUNCTIONS
# The definition of these functions follows some strict rules for parsing and
# automatic help text extraction:
# 1. the function name has to start with "subcommand-"
# 2. the "function subcommand-NAME ()" has to be on its own line with now
# extra text
# 3. the lines after that can be comments, they form the help text
# 4. the opening "{" has to be on a line on its own.
function subcommand-edit ()
# edit the source of this script
{
# This is all in one line in order to force the shell to read it before
# starting the editor. Just in case some lines shift and such.
$EDITOR $ZSH_SCRIPT; until zsh -n $ZSH_SCRIPT; do $EDITOR -q <(zsh -n $ZSH_SCRIPT); done; exit
}
while getopts hx FLAG; do
case $FLAG in
h) set -- help; break;;
x) set -x;;
esac
done
shift $((OPTIND - 1))
if [[ $# -eq 0 ]]; then
set -- help
fi
case $1 in
help) # show this help message
usage
;;
complete) # print setup code for completion (for eval-ing)
# Print the defined completion function and the setup code.
functions _lucs_util_scipt_completion_function
print compdef _lucs_util_scipt_completion_function $prog
functions _u_helper_lp_options
;;
edit|e) # edit the source of this script
# This is all in one line in order to force the shell to read it before
# starting the editor. Just in case some lines shift and such.
$EDITOR $0; until zsh -n $0; do $EDITOR -q <(zsh -n $0); done; exit
;;
bk) # mount the backup hard disk and make a backup
until
findmnt /media/bak >/dev/null
do
echo Please plug in the external backup disk.
sleep 1
mount LABEL=bak.fs
done
backup.py -v \
snap \
--exclude=/{.cache,.cargo,.cabal,.gem,.gradle,.rustup,.stack,.recoll,tmp,vid,audio,img,comic,TV-'*'.webl.h264.mp4,torrents,.local/share/{nvim/plugins,Zeal},.npm,vcs} \
/media/bak/home
ret=$?
df -h /media/bak
[[ $2 != -n ]] && umount /media/bak
# We assume we are at home and now try to delete old backups from the pi
#systemd-run --host pi
ssh pi systemctl --user start clean-up-backup-snapshots
exit $ret
;;
bk-mini)
set -e
myip=$(curl --silent http://luc42.lima-city.de/ip.php)
piip=$(dig +short luc.now-dns.net)
if [[ $myip = $piip ]]
then domain=pi
else domain=nowip
fi
exec backup.py -v snapshot \
--source etc --source mail --source .config --source uni \
--exclude=/uni/'[^si]*' --exclude='*.m4v' \
${domain}:backup/tp/home
;;
phone) # phone related subcommands (nokia)
shift
case $1 in
savecontacts)
# check bluetooth service
start-bluetooth
echo Connecting to Nokia phone ...
bluetoothctl <<-EOF
power on
connect 00:25:47:10:0D:2D
exit
EOF
# Wait for the connection.
sleep 1
gammu backup $2.vcf <<<ALL
;;
put-vcf)
start-bluetooth
echo Connecting to Nokia phone ...
bluetoothctl <<-EOF
power on
connect 00:25:47:10:0D:2D
exit
EOF
# Wait for the connection.
sleep 1
tmp1=$(mktemp --suffix=.vcf)
tmp2=$(mktemp --suffix=.vcf)
sed '/^ADR[;:]/d' < $2 > $tmp1
iconv -c --to-code=latin1 --output=$tmp2 $tmp1
gammu restore $tmp2 <<<ALL
rm -f $tmp1 $tmp2
;;
*)
echo TODO >&2
exit 2
;;
esac
;;
split-file-at-empty-lines)
shift
empty=''
extension=.vcf
while [[ $# -ge 1 ]]; do
case $1 in
--windows|--crlf|--dos)
empty=$'\r'
shift
;;
-d|--dir|--out|-o|--output)
if [[ $# -eq 1 ]]; then
echo The option $1 needs an argument. >&2
exit 2
fi
output=$2
shift 2
;;
--extension)
if [[ $# -eq 1 ]]; then
echo The option $1 needs an argument. >&2
exit 2
fi
extension=$2
shift 2
;;
--)
shift
break
;;
*)
break
;;
esac
done
if [[ $# -ne 1 ]]; then
echo You need to give exactly one filename. >&2
exit 2
fi
file=$1
if [[ -z $output ]]; then
output=$file.split
fi
# find the number of empty lines (corresponds to the number of files
# created) and then count the bytes in the decimal representation, which
# gives the length to which output filename numbers should be padded with
# zeros
length=$(($(echo $(($(grep --count "^$empty\$" $file) + 1)) | wc --bytes) - 1))
integer -Z $length i=0
mkdir -p $output
rm -f $output/*(N)
while read line; do
if [[ $line = $empty ]]; then
((i++))
rm -f $output/$i$extension
else
echo $line >> $output/$i$extension
fi
done < $file
;;
backup)
backup.py snapshot --source ~/.config/gpg/ --exclude \
$(repeat 14 printf '[0-9]') --exclude latest \
nowip:backup/gpg
backup.py snapshot --source $PASSWORD_STORE_DIR/ --exclude .git \
--exclude .gitattributes \
nowip:backup/pass
;;
urxvt-font)
printf '\33]50;%s\007' ${2:-xft:terminus};;
logs)
shift
case $1 in
bk|backup)
systemctl --no-pager --full status \
backup-pacman-cache.timer \
systemctl --no-pager --full --user status \
echo "==============================================================="
systemctl --no-pager --full status \
backup-pacman-cache.service \
systemctl --no-pager --full --user status \
;;
pop|mailin|fetchmail)
journalctl --quiet --no-pager --lines=$((2*LINES)) \
--user-unit=fetchmail.service
;;
smtp|mailout)
journalctl --quiet --no-pager --lines=$((2*LINES)) \
SYSLOG_IDENTIFIER=msmtp
;;
mail|mailall|allmail)
shift
journalctl --quiet --no-pager --lines=$((2*LINES)) "$@" \
SYSLOG_IDENTIFIER=msmtp + _SYSTEMD_USER_UNIT=fetchmail.service
;;
*) print Choose one of bk, pop or smtp. >&2; exit 2;;
esac
;;
setenv) # <PID> VARNAME=value
shift
# thanks to https://gist.github.com/coderofsalvation/e1376e4d2b29607431df
sudo gdb -q -batch -ex "attach $1" -ex "call putenv(\"$2\")" -ex 'detach'
;;
bank)
shift
browser -p \
https://www.onlinebanking-vr-ll.de/banking-private/entry \
https://banking.umweltbank.de/ $@ &
;;
python-profile)
shift
name=profile
while getopts n: FLAG; do
case $FLAG in
n) name=$OPTARG;;
esac
done
shift $((OPTIND - 1))
PYTHONPATH=. python -m cProfile -o $name.pstats $@
gprof2dot -f pstats $name.pstats > $name.dot
dot -T png -o $name.png $name.dot
feh $name.png
;;
test-vim-startup)
shift
echo " clock self+sourced self: sourced script"
nvim --headless -c 'quitall!' --startuptime \
>(grep ${@:-sourcing} | sort -n --key=3) \
2>/dev/null
echo " clock self+sourced self: sourced script"
;;
cmp-git-branch)
shift
upsteam=master
count=10
while getopts u:c: FLAG; do
case $FLAG in
u) upstream=$OPTARG;;
c) count=$OPTARG;;
esac
done
shift $((OPTIND - 1))
# Save the current HEAD.
head=$(git rev-parse --abbrev-ref HEAD)
time (for i in {0..$count}; do $@ &>/dev/null; done )
git checkout $upstream
time (for i in {0..$count}; do $@ &>/dev/null; done )
git checkout $head
;;
true-color-test) # print some colors to test true color support of the term
awk 'BEGIN{
s="/\\/\\/\\/\\/\\"; s=s s s s s s s s;
for (colnum = 0; colnum<77; colnum++) {
r = 255-(colnum*255/76);
g = (colnum*510/76);
b = (colnum*255/76);
if (g>255) g = 510-g;
printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b;
printf "\033[48;2;%d;%d;%dm", r,g,b;
printf "%s\033[0m", substr(s,colnum+1,1);
}
printf "\n";
}'
;;
print)
shift
if [[ $1 = --flush ]]; then
shift
ssh ifi bin/flush-print-queue.sh $@
else
scp -p $@ ifi:print/
fi
;;
time-management)
shift
function list-loaded-timers () {
systemctl --user list-units --plain 'time-management-warning@*.timer' |\
sed -n '1d;/^$/q;s/ .*//p'
}
case $1 in
list) systemctl --user list-timers 'time-management-warning@*.timer';;
reset) systemctl --user start time-management-warning;;
start)
if [[ -z $2 ]]; then
echo You have to give a time argument. >&2
exit 2
fi
systemctl --user start time-management-warning@$2.timer
;;
stop)
for timer in $(list-loaded-timers); do
systemctl --user stop $timer
done
;;
pause) systemctl --user mask --runtime timer-management-warning;;
continue) systemctl --user unmask --runtime timer-management-warning;;
*) echo argument error >&2; exit 2;;
esac
;;
show-term-attributes)
shift
case $1 in
v2|2)
printf '%s\n' '\e[%dm %3d\e[m'
for i in {0..108}; do
((i%10 == 0)) && echo
printf '\e[%dm %3d\e[m' $i $i
done
echo
;;
v1|1|*)
for i in {0..15}; do
printf 'i=%-3s --[\e[%sm terminal code test string \e[0m]--\n' $i: $i
done
;;
esac
;;
updateinfo)
pacman -Qquet | xargs --no-run-if-empty pacman -Qi | \
sed -E '/(^(Version|Architecture|Licenses|Packager|Build Date|Install Date|Install Reason|Install Script|Validated By)|None$)/d; s/^Name.*/\x1b[31m&\x1b[m/'
;;
nvim-nightly)
wget --output-document ~/.local/bin/nvim-nightly https://github.com/neovim/neovim/releases/download/nightly/nvim.appimage
chmod +x ~/.local/bin/nvim-nightly
;;
*) echo Unknown command >&2; exit 2;;
esac