updates, especially for etiona
[distro-setup] / brc2
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4 # this gets sourced. shebang is just for file mode detection
5
6
7 # * settings
8
9 HISTFILE=$HOME/.bh
10
11 source /a/bin/distro-setup/path-add-function
12 path-add /a/exe
13 # add this with absolute paths as needed for better security
14 #path-add --end /path/to/node_modules/.bin
15
16 # pip3 --user things go here:
17 path-add --end ~/.local/bin
18 path-add --ifexists --end /a/work/libremanage
19 path-add --ifexists --end /a/opt/adt-bundle*/tools /a/opt/adt-bundle*/platform-tools
20 path-add --ifexists --end /a/opt/scancode-toolkit-3.0.2
21
22 export WCDHOME=/a
23
24
25 # * include files
26
27 # generated instead of dynamic for the benefit of shellcheck
28 #for x in /a/bin/distro-functions/src/* /a/bin/!(githtml)/*-function?(s); do echo source $x ; done
29 source /a/bin/distro-functions/src/identify-distros
30 source /a/bin/distro-functions/src/package-manager-abstractions
31 source /a/bin/log-quiet/logq-function
32 # for x in /a/bin/bash_unpublished/source-!(.#*); do echo source $x; done
33 source /a/bin/bash_unpublished/source-semi-priv
34 source /a/bin/bash_unpublished/source-state
35
36 source /a/bin/log-quiet/logq-function
37
38
39
40 # * functions
41
42
43
44 # todo, update this
45 complete -F _longopt la lower low rlt rld rl lld ts ll dircp ex fcp fct fpst gr
46
47
48 anki() {
49 if which anki &>/dev/null; then
50 command anki
51 else
52 schroot -c anki -- anki
53 fi
54 }
55
56 acat() {
57 shopt -s nullglob
58 hrcat /m/md/alerts/new/* /m/md/alerts/cur/*
59 shopt -u nullglob
60 }
61 aclear() {
62 shopt -s nullglob
63 files=(/m/md/alerts/new/* /m/md/alerts/cur/*)
64 if (( ${#files[@]} )); then
65 rm -f ${files[@]}
66 fi
67 shopt -u nullglob
68 system-status _
69 }
70
71 ap() {
72 # pushd in case current directory has an ansible.cfg file
73 pushd /a/xans >/dev/null
74 ansible-playbook -v -l ${1:- $(hostname -f)} site.yml
75 popd >/dev/null
76 }
77 aw() {
78 pushd /a/work/ansible-configs >/dev/null
79 time ansible-playbook -v -i inventory adhoc.yml "$@"
80 popd >/dev/null
81 }
82 ad() {
83 pushd /a/bin/distro-setup/a >/dev/null
84 ansible-playbook site.yml
85 popd >/dev/null
86 }
87
88 astudio() {
89 # googling android emulator libGL error: failed to load driver: r600
90 # lead to http://stackoverflow.com/a/36625175/14456
91 export ANDROID_EMULATOR_USE_SYSTEM_LIBS=1
92 /a/opt/android-studio/bin/studio.sh "$@" &r;
93 }
94
95 bindpush() {
96 lipush || return 1
97 for h in li l2; do
98 sl $h <<'EOF' || return 1
99 set -e
100 conflink
101 f=/var/lib/bind/db.b8.nz
102 ser stop bind9
103 sudo rm -fv $f.jnl
104 sudo install -m 644 -o bind -g bind /p/c/machine_specific/linode/bind-initial/db.b8.nz $f
105 ser restart bind9
106 EOF
107 done
108 }
109
110 bbk() { # btrbk wrapper
111
112 local pid
113 c /
114 local active=true
115 systemctl is-active btrbk.timer || active=false
116 if $active; then
117 ser stop btrbk.timer
118 fi
119 if [[ $(systemctl is-active btrbk.service ||:) != inactive ]]; then
120 echo "cron btrbk is already running"
121 if $active; then ser start btrbk.timer; fi
122 return 1
123 fi
124 # run latest
125 install-my-scripts
126 jrun -p btrbk btrbk-run "$@"
127 if $active; then
128 if (( $ret )); then
129 echo bbk: WARNING: btrbk.timer not restarted due to failure
130 else
131 ser start btrbk.timer
132 fi
133 fi
134 return $ret
135 }
136
137 bfg() { java -jar /a/opt/bfg-1.12.14.jar "$@"; }
138
139 bigclock() {
140 xclock -digital -update 1 -face 'arial black-80:bold'
141 }
142
143 inttrap() {
144 pid=$1
145 # just passing on -INT doesnt work.
146 kill -TERM $pid
147 sleep .05
148 if [[ ! -e /proc/$pid ]]; then
149 if [[ $old_int_trap ]]; then
150 $old_int_trap
151 else
152 trap INT
153 fi
154 fi
155 }
156
157
158 jrun() { # journal run. run args, log to journal, tail and grep the journal.
159 # Note, an alternative without systemd would be something like ts.
160 # Note: I tried doing cmd | pee "sudo systemd-cat" cat, but that
161 # had some problems like ctrl-c didnt work or something.
162 local pid pattern sedscript cmd_name ended
163 ret=0
164 case $1 in
165 -p)
166 pattern="$2|"
167 shift 2
168 ;;
169 esac
170 cmd_name=${1##*/}
171 sedscript="/$pattern$cmd_name/p;/^.{16}[^ ]+ $cmd_name\[[0-9]+]: ([^ ]*\/)?$cmd_name: exiting with status [0-9]+\$/q"
172 # We use >() so that $! is the pid of journalctl, otherwise its the sed pid and then
173 # if we kill that, it takes journalctl about 10 seconds to catch up, and we get
174 # an annoying message about job finishing then.
175 journalctl -qn2 -f &> >(sed -nr "$sedscript") &
176 # We kill this in prompt-command for the case that we ctrl-c the
177 # systemd-cat. i dont know any way to trap ctrl-c and still run the
178 # normal action for it. There might be a way, unsure.
179 jr_pid=$!
180 systemd-cat -t "$cmd_name" "$@" || ret=$?
181 if (( $ret )); then
182 echo "jrun: ERROR: $* returned $ret"
183 fi
184 # This justs lets the journal output its last line
185 # before the prompt comes up.
186 sleep .5
187 kill $jr_pid &>/dev/null ||:
188 unset jr_pid
189 fg &>/dev/null ||:
190 }
191
192 sm() {
193 c /
194 # run latest
195 install-my-scripts
196 jrun -p btrbk switch-mail-host "$@"
197 return $ret
198 }
199
200 lipush() {
201 # note, i had --delete-excluded, but that deletes all files in --exclude-from on
202 # the remote site, which doesn't make sense, so not sure why i had it.
203 local p a
204 p=(/a/bin /a/exe /a/h /a/c /p/c/machine_specific/linode{,.hosts} /a/opt/{emacs-debianstable,mu})
205 a="-ahviSAXPH --specials --devices --delete --relative --exclude-from=/p/c/li-rsync-excludes"
206 ret=0
207 m rsync "$@" $a ${p[@]} /p/c/machine_specific/l2 root@l2.b8.nz:/ || ret=$?
208 m rsync "$@" $a ${p[@]} /p/c/machine_specific/li root@li.b8.nz:/ || ret=$?
209 m rsync "$@" -ahviSAXPH root@iankelling.org:/a/h/proposed-comments/ /a/h/proposed-comments || ret=$?
210 return $ret
211 }
212 lipushnoe() { # noe = noemacs. for running faster.
213 rsync "$@" --delete-excluded -ahviSAXPH --specials --devices --delete --relative \
214 --exclude-from=/p/c/li-rsync-excludes /a/bin /a/exe /a/h /a/c /p/c/machine_specific/li root@li:/
215 }
216
217 #### begin bitcoin related things
218 btc() {
219 local f=/etc/bitcoin/bitcoin.conf
220 # importprivkey will timeout if using the default of 15 mins.
221 # upped it to 1 hour.
222 bitcoin-cli -rpcclienttimeout=60000 -$(s grep rpcuser= $f) -$(s grep rpcpassword= $f) "$@"
223 }
224 btcusd() { # $1 btc in usd
225 local price
226 price="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
227 printf "$%s\n" "$price"
228 if [[ $1 ]]; then
229 printf "$%.2f\n" "$(echo "scale=4; $price * $1"| bc -l)"
230 fi
231 }
232 usdbtc() { # $1 usd in btc
233 local price
234 price="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
235 printf "$%s\n" "$price"
236 if [[ $1 ]]; then
237 # 100 mil satoshi / btc. 8 digits after the 1.
238 printf "%.8f btc\n" "$(echo "scale=10; $1 / $price "| bc -l)"
239 fi
240 }
241 satoshi() { # $1 satoshi in usd
242 local price
243 price="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
244 price=$(echo "scale=10; $price * 0.00000001"| bc -l)
245 printf "$%f\n" "$price"
246 if [[ $1 ]]; then
247 printf "$%.2f\n" "$(echo "scale=10; $price * $1"| bc -l)"
248 fi
249 }
250 #### end bitcoin related things
251
252
253
254 cbfstool () { /a/opt/coreboot/build/cbfstool "$@"; }
255
256
257 cgpl()
258 {
259 if (($#)); then
260 cp /a/bin/data/COPYING "$@"
261 else
262 cp /a/bin/data/COPYING .
263 fi
264 }
265
266 capache()
267 {
268 if (($#)); then
269 cp /a/bin/data/LICENSE "$@"
270 else
271 cp /a/bin/data/LICENSE .
272 fi
273 }
274
275 chrome() {
276 if type -p chromium &>/dev/null; then
277 cmd=chromium
278 else
279 cd
280 cmd="schroot -c stretch chromium"
281 CHROMIUM_FLAGS='--enable-remote-extensions' $cmd &r
282 fi
283 }
284
285
286 # do all tee.
287 # pipe to this, or just type like a shell
288 # todo: test this
289 dat() {
290 tee >(ssh frodo.b8.nz) >(ssh x2) >(ssh tp.b8.nz) >(ssh kw) >(ssh tp.b8.nz)
291 }
292 da() { # do all
293 local host
294 for host in x2 kw tp.b8.nz x3.b8.nz frodo.b8.nz; do
295 ssh $host "$@"
296 done
297 }
298
299
300 debian_pick_mirror () {
301 # netselect-apt finds a fast mirror.
302 # but we need to replace the mirrors ourselves,
303 # because it doesnt do that. best it can do is
304 # output a basic sources file
305 # here we get the server it found, get the main server we use
306 # then substitute all instances of one for the other in the sources file
307 # and backup original to /etc/apt/sources.list-original.
308 # this is idempotent. the only way to identify debian sources is to
309 # note the original server, so we put it in a comment so we can
310 # identify it later.
311 local file
312 file=$(mktemp -d)/f # safe way to get file name without creating one
313 sudo netselect-apt -o "$file" || return 1
314 url=$(grep ^\\w $file | head -n1 | awk '{print $2}')
315 sudo cp -f /etc/apt/sources.list /etc/apt/sources.list-original
316 sudo sed -ri "/http.us.debian.org/ s@( *[^ #]+ +)[^ ]+([^#]+).*@\1$url\2# http.us.debian.org@" /etc/apt/sources.list
317 sudo apt-get update
318 }
319 digme() {
320 digdiff @ns{1,2}.iankelling.org "$@"
321 }
322
323
324 dup() {
325 local ran_d
326 ran_d=false
327 system-status _
328 case $PS1 in
329 *DISTRO-BEGIN!*|*DISTRO!*)
330 pushd /
331 /b/ds/distro-begin || return $?
332 popd
333 ran_d=true
334 ;;&
335 *DISTRO-END!*|*DISTRO!*)
336 pushd /
337 /b/ds/distro-end || return $?
338 popd
339 ran_d=true
340 ;;&
341 *CONFLINK*)
342 if ! $ran_d; then
343 conflink
344 fi
345 ;;
346 esac
347 system-status _
348 }
349
350 envload() { # load environment from a previous: export > file
351 local file=${1:-$HOME/.${USER}_env}
352 eval "$(export | sed 's/^declare -x/export -n/')"
353 while IFS= read -r line; do
354 # declare -x makes variables local to a function
355 eval ${line/#declare -x/export}
356 done < "$file"
357 }
358
359 failfunc() { asdf a b c; }
360 failfunc2() { failfunc d e f; }
361
362 # one that comes with distros is too old for newer devices
363 fastboot() {
364 /a/opt/android-platform-tools/fastboot "$@";
365 }
366
367 kdecd() { /usr/lib/x86_64-linux-gnu/libexec/kdeconnectd; }
368
369 # List of apps to install/update
370 # Create from existing manually installed apps by doing
371 # fdroidcl update
372 # fdroidcl search -i, then manually removing
373 # automatically installed/preinstalled apps
374
375 #
376 # # my attempt at recovering from boot loop:
377 # # in that case, boot to recovery (volume up, home button, power, let go of power after samsun logo)
378 # # then
379 # mount /dev/block/mmcblk0p12 /data
380 # cd /data
381 # find -iname '*appname*'
382 # rm -rf FOUND_DIRS
383 # usually good enough to just rm -rf /data/app/APPNAME
384 #
385 # currently broken:
386 # # causes replicant to crash
387 # org.quantumbadger.redreader
388 # org.kde.kdeconnect_tp
389
390 # not broke, but wont work without gps
391 #com.zoffcc.applications.zanavi
392 # not broke, but not using atm
393 #com.nutomic.syncthingandroid
394 # # doesn\'t work on replicant
395 #net.sourceforge.opencamera
396 #
397 fdroid_pkgs=(
398 de.marmaro.krt.ffupdater
399 me.ccrama.redditslide
400 org.fedorahosted.freeotp
401 at.bitfire.davdroid
402 com.alaskalinuxuser.justnotes
403 com.artifex.mupdf.viewer.app
404 com.danielkim.soundrecorder
405 com.fsck.k9
406 com.ghostsq.commander
407 com.ichi2.anki
408 com.jmstudios.redmoon
409 com.jmstudios.chibe
410 org.kde.kdeconnect_tp
411 com.notecryptpro
412 com.termux
413 cz.martykan.forecastie
414 de.danoeh.antennapod
415 de.blinkt.openvpn
416 de.marmaro.krt.ffupdater
417 eu.siacs.conversations
418 free.rm.skytube.oss
419 im.vector.alpha # riot
420 info.papdt.blackblub
421 me.tripsit.tripmobile
422 net.gaast.giggity
423 net.minetest.minetest
424 net.osmand.plus
425 org.isoron.uhabits
426 org.linphone
427 org.gnu.icecat
428 org.smssecure.smssecure
429 org.yaaic
430 sh.ftp.rocketninelabs.meditationassistant.opensource
431 )
432 # https://forum.xda-developers.com/android/software-hacking/wip-selinux-capable-superuser-t3216394
433 # for maru,
434 #me.phh.superuser
435
436 fdup() {
437 local -A installed updated
438 local p
439 # tried putting this in go buildscript cronjob,
440 # but it failed with undefined: os.UserCacheDir. I expect its due to
441 # an environment variable missing, but its easier just to stick it here.
442 m go get -u mvdan.cc/fdroidcl || return 1
443 m fdroidcl update
444 if fdroidcl search -u | grep ^org.fdroid.fdroid; then
445 fdroidcl install org.fdroid.fdroid
446 sleep 5
447 m fdroidcl update
448 fi
449 for p in $(fdroidcl search -i| grep -o "^\S\+"); do
450 installed[$p]=true
451 done
452 for p in $(fdroidcl search -u| grep -o "^\S\+"); do
453 updated[$p]=false
454 done
455 for p in ${fdroid_pkgs[@]}; do
456 if ! ${installed[$p]:-false}; then
457 m fdroidcl install $p
458 # sleeps are just me being paranoid since replicant has a history of crashing when certain apps are installed
459 sleep 5
460 fi
461 done
462 for p in ${!installed[@]}; do
463 if ! ${updated[$p]:-true}; then
464 m fdroidcl install $p
465 sleep 5
466 fi
467 done
468 }
469
470 firefox-default-profile() {
471 key=Default value=1 section=$1
472 file=/p/c/subdir_files/.mozilla/firefox/profiles.ini
473 sed -ri "/^ *$key/d" "$file"
474 sed -ri "/ *\[$section\]/,/^ *\[[^]]+\]/{/^\s*$key[[:space:]=]/d};/ *\[$section\]/a $key=$value" "$file"
475 }
476 fdhome() { #firefox default home profile
477 firefox-default-profile Profile0
478 }
479
480 fdwork() {
481 firefox-default-profile Profile4
482 }
483
484 ff() {
485 if type -P firefox &>/dev/null; then
486 firefox "$@"
487 else
488 iceweasel "$@"
489 fi
490 }
491
492
493
494 fn() {
495 firefox -P alt "$@" >/dev/null 2>&1
496 }
497
498
499 fsdiff () {
500 local missing=false
501 local dname="${PWD##*/}"
502 local m="/a/tmp/$dname-missing"
503 local d="/a/tmp/$dname-diff"
504 [[ -e $d ]] && rm "$d"
505 [[ -e $m ]] && rm "$m"
506 local msize=0
507 local fsfile
508 while read -r line; do
509 fsfile="$1${line#.}"
510 if [[ -e "$fsfile" ]]; then
511 md5diff "$line" "$fsfile" && tee -a "/a/tmp/$dname-diff" <<< "$fsfile $line"
512 else
513 missing=true
514 echo "$line" >> "$m"
515 msize=$((msize + 1))
516 fi
517 done < <(find . -type f )
518 if $missing; then
519 echo "$m"
520 (( msize <= 100 )) && cat $m
521 fi
522 }
523 fsdiff-test() {
524 # expected output, with different tmp dirs
525 # /tmp/tmp.HDPbwMqdC9/c/d ./c/d
526 # /a/tmp/tmp.qLDkYxBYPM-missing
527 # ./b
528 cd $(mktemp -d)
529 echo ok > a
530 echo nok > b
531 mkdir c
532 echo ok > c/d
533 local x
534 x=$(mktemp -d)
535 mkdir $x/c
536 echo different > $x/c/d
537 echo ok > $x/a
538 fsdiff $x
539 }
540 rename-test() {
541 # test whether missing files were renamed, generally for use with fsdiff
542 # $1 = fsdiff output file, $2 = directory to compare to. pwd = fsdiff dir
543 # echos non-renamed files
544 local x y found
545 unset sums
546 for x in "$2"/*; do
547 { sums+=( "$(md5sum < "$x")" ) ; } 2>/dev/null
548 done
549 while read -r line; do
550 { missing_sum=$(md5sum < "$line") ; } 2>/dev/null
551 renamed=false
552 for x in "${sums[@]}"; do
553 if [[ $missing_sum == "$x" ]]; then
554 renamed=true
555 break
556 fi
557 done
558 $renamed || echo "$line"
559 done < "$1"
560 return 0
561 }
562
563 feh() {
564 # F = fullscren, z = random, Z = auto zoom
565 command feh -FzZ "$@"
566 }
567
568
569
570 fw() {
571 firefox -P default "$@" >/dev/null 2>&1
572 }
573
574 gitian() {
575 git config user.email ian@iankelling.org
576 }
577
578 # at least in flidas, things rely on gpg being gpg1
579 gpg() {
580 command gpg2 "$@"
581 }
582
583 gse() {
584 local email=ian@iankelling.org
585 if readlink ~/.mu | grep fsf &>/dev/null; then
586 email=iank@fsf.org
587 fi
588 git send-email --notes "--envelope-sender=<$email>" \
589 --suppress-cc=self "$@"
590 }
591
592 hstatus() {
593 # do git status on published repos.
594 c /a/bin/githtml
595 for x in *; do
596 cd $(readlink -f $x)/..
597 status=$(i status -s) || pwd
598 if [[ $status ]]; then
599 hr
600 echo $x
601 printf "%s\n" "$status"
602 fi
603 cd /a/bin/githtml
604 done
605 }
606
607 idea() {
608 /a/opt/idea-IC-163.7743.44/bin/idea.sh "$@" &r
609 }
610
611 ilog() {
612 chan=${1:-#fsfsys}
613 # use * instead of -r since that does sorted order
614 ssh root@iankelling.org "cd /var/lib/znc/moddata/log/iank/freenode/$chan && hr && for x in *; do echo \$x; cat \$x; hr; done" | less +G
615 }
616
617 o() {
618 if type gvfs-open &> /dev/null ; then
619 gvfs-open "$@"
620 else
621 xdg-open "$@"
622 fi
623 # another alternative is run-mailcap
624 }
625
626 jfilter() {
627 grep -Evi -e "^(\S+\s+){4}(sudo|sshd|cron)\[\S*:" \
628 -e "^(\S+\s+){4}systemd\[\S*: (starting|started) (btrfsmaintstop|dynamicipupdate|spamd dns bug fix cronjob|rss2email)\.*$"
629 }
630 jtail() {
631 journalctl -n 10000 -f "$@" | jfilter
632 }
633 jr() { journalctl "$@" | jfilter | less ; }
634 jrf() { journalctl -n 200 -f "$@" | jfilter; }
635
636
637 kff() { # keyboardio firmware flash
638 pushd /a/bin/distro-setup/Arduino/Model01-Firmware
639 yes $'\n' | make flash
640 popd
641 }
642
643
644
645 lom() {
646 local l base
647 if [[ $1 == /* ]]; then
648 base=${1##*/}
649 if mountpoint /mnt/$base; then
650 return 0
651 fi
652 l=$(sudo losetup -f)
653 sudo losetup $l $1
654 if ! sudo cryptsetup luksOpen $l $base; then
655 sudo losetup -d $l
656 return 1
657 fi
658 sudo mkdir -p /mnt/$base
659 sudo mount /dev/mapper/$base /mnt/$base
660 sudo chown $USER:$USER /mnt/$base
661 else
662 base=$1
663 sudo umount /mnt/$base
664 l=$(sudo cryptsetup status /dev/mapper/$base|sed -rn 's/^\s*device:\s*(.*)/\1/p')
665 sudo cryptsetup luksClose /dev/mapper/$base || return 1
666 sudo losetup -d $l
667 fi
668 }
669
670
671 mbenable() {
672 local mb=$1
673 dst=/m/4e/$mb
674 src=/m/md/$mb
675 set -x
676 [[ -e $src ]] || { set +x; return 1; }
677 mv -T $src $dst || { set +x; return 1; }
678 ln -s -T $dst $src
679 /a/exe/lnf /m/.mu ~
680 mu index --maildir=/m/4e
681 set +x
682 }
683 mbdisable() {
684 local mb=$1
685 dst=/m/md/$mb
686 src=/m/4e/$mb
687 set -x
688 [[ -e $src ]] || { set +x; return 1; }
689 if [[ -L $dst ]]; then rm $dst; fi
690 mv -T $src $dst
691 set +x
692 }
693
694
695 mdt() {
696 markdown "$1" >/tmp/mdtest.html
697 firefox /tmp/mdtest.html
698 }
699
700 mo() { xset dpms force off; } # monitor off
701
702 myirc() {
703 chan=${1:-fsf-office}
704 # use * instead of -r since that does sorted order
705 ssh root@iankelling.org "cd /var/lib/znc/moddata/log/iank/freenode/#$chan; grep '\<iank.*' *"
706 }
707
708 net-dev-info() {
709 e "lspci -nnk|gr -iA2 net"
710 lspci -nnk|gr -iA2 net
711 hr
712 e "s lshw -C network"
713 hr
714 sudo lshw -C network
715 }
716
717 nk() {
718 ser stop NetworkManager
719 ser stop dnsmasq
720 sudo resolvconf -d NetworkManager
721 ser start dnsmasq
722 sudo ifup br0
723 }
724 ngo() {
725 sudo ifdown br0
726 ser start NetworkManager
727 sleep 4
728 sudo nmtui-connect
729 }
730
731 otp() {
732 oathtool --totp -b "$@" | xclip -selection clipboard
733 }
734
735
736 pakaraoke() {
737 # from http://askubuntu.com/questions/456021/remove-vocals-from-mp3-and-get-only-instrumentals
738 pactl load-module module-ladspa-sink sink_name=Karaoke master=alsa_output.usb-Audioengine_Audioengine_D1-00.analog-stereo plugin=karaoke_1409 label=karaoke control=-30
739 }
740
741 pfind() { #find *$1* in $PATH
742 [[ $# != 1 ]] && { echo requires 1 argument; return 1; }
743 local pathArray
744 IFS=: pathArray=($PATH); unset IFS
745 find "${pathArray[@]}" -iname "*$1*"
746 }
747
748 pick-trash() {
749 # trash-restore lists everything that has been trashed at or below CWD
750 # This picks out files just in CWD, not subdirectories,
751 # which also match grep $1, usually use $1 for a time string
752 # which you get from running restore-trash once first
753 local name x ask
754 local nth=1
755 # last condition is to not ask again for ones we skipped
756 while name="$( echo | restore-trash | gr "$PWD/[^/]\+$" | gr "$1" )" \
757 && [[ $name ]] && (( $(wc -l <<<"$name") >= nth )); do
758 name="$(echo "$name" | head -n $nth | tail -n 1 )"
759 read -r -p "$name [Y/n] " ask
760 if [[ ! $ask || $ask == [Yy] ]]; then
761 x=$( echo "$name" | gr -o "^\s*[0-9]*" )
762 echo $x | restore-trash > /dev/null
763 elif [[ $ask == [Nn] ]]; then
764 nth=$((nth+1))
765 else
766 return
767 fi
768 done
769 }
770
771
772 pub() {
773 rld /a/h/_site/ li:/var/www/iankelling.org/html
774 }
775
776
777 pumpa() {
778 # fixes the menu bar in xmonad. this won\'t be needed when xmonad
779 # packages catches up on some changes in future (this is written in
780 # 4/2017)
781 #
782 # geekosaur: so youll want to upgrade to xmonad 0.13 or else use a
783 # locally modified XMonad.Hooks.ManageDocks that doesnt set the
784 # work area; turns out it\'s impossible to set correctly if you are
785 # not a fully EWMH compliant desktop environment
786 #
787 # geekosaur: chrome shows one failure mode, qt/kde another, other
788 # gtk apps a third, ... I came up with a setting that works for me
789 # locally but apparently doesnt work for others, so we joined the
790 # other tiling window managers in giving up on setting it at all
791 #
792 xprop -root -remove _NET_WORKAREA
793 command pumpa &r
794 }
795
796 # reviewboard, used at my old job
797 #rbpipe() { rbt post -o --diff-filename=- "$@"; }
798 #rbp() { rbt post -o "$@"; }
799
800 rebr() {
801 sudo ifdown br0
802 sudo ifup br0
803 }
804
805 resolvcat() {
806 local f
807 m s nscd -i hosts
808 f=/etc/resolv.conf
809 echo $f:; ccat $f
810 hr; s ss -lpn 'sport = 53'
811 if systemctl is-enabled dnsmasq &>/dev/null || [[ $(systemctl is-active dnsmasq ||:) != inactive ]]; then
812 # this will fail is dnsmasq is failed
813 hr; m ser status dnsmasq | cat || :
814 f=/var/run/dnsmasq/resolv.conf
815 hr; echo $f:; ccat $f
816 hr; m grr '^ *(servers-file|server) *=|^ *no-resolv *$' /etc/dnsmasq.conf /etc/dnsmasq.d
817 f=/etc/dnsmasq-servers.conf
818 hr; echo $f:; ccat $f
819 fi
820 if systemctl is-enabled systemd-resolved &>/dev/null || [[ $(systemctl is-active systemd-resolved ||:) != inactive ]]; then
821 hr; m ser status systemd-resolved | cat || :
822 hr; m systemd-resolve --status
823 fi
824
825 }
826 rcat() {
827 resolvcat | less
828 }
829 reresolv() {
830 sudo nscd -i hosts
831 #sudo systemctl restart dnsmasq
832 }
833
834 # only run on MAIL_HOST. simpler to keep this on one system.
835 r2eadd() { # usage: name url
836 # initial setup of rss2email:
837 # r2e new r2e@iankelling.org
838 # that initializes files, and sets default email.
839 # symlink to the config doesnt work, so I copied it to /p/c
840 # and then use cli option to specify explicit path.
841 # Only option changed from default config is to set
842 # force-from = True
843 #
844 # or else for a few feeds, the from address is set by the feed, and
845 # if I fail delivery, then I send a bounce message to that from
846 # address, which makes me be a spammer.
847
848 r2e add $1 "$2" $1@r2e.iankelling.org
849 # get up to date and dont send old entries now:
850 r2e run --no-send $1
851 }
852 r2e() { command r2e -d /p/c/rss2email.json -c /p/c/rss2email.cfg "$@"; }
853
854 rspicy() { # usage: HOST DOMAIN
855 # connect to spice vm remote host. use vspicy for local host
856 local port
857 # shellcheck disable=SC2087
858 port=$(ssh $1<<EOF
859 sudo virsh dumpxml $2|grep "<graphics.*type='spice'" | \
860 sed -rn "s/.*port='([0-9]+).*/\1/p"
861 EOF
862 )
863 if [[ $port ]]; then
864 spicy -h $1 -p $port
865 else
866 echo "error: no port found. check that the domain is running."
867 fi
868 }
869
870
871 scssl() {
872 # s gem install scss-lint
873 pushd /a/opt/thoughtbot-guides
874 git pull --stat
875 popd
876 scss-lint -c /a/opt/thoughtbot-guides/style/sass/.scss-lint.yml "$@"
877 }
878
879 skbrc() {
880 sk -e 2120,245 /b/ds/brc /b/ds/brc2
881 }
882
883 skaraoke() {
884 local tmp out
885 out=${2:-${1%.*}.sh}
886 tmp=$(mktemp -d)
887 script -t -c "mpv --no-config --no-resume-playback --no-terminal --no-audio-display '$1'" $tmp/typescript 2>$tmp/timing
888 # todo, the current sleep seems pretty good, but it
889 # would be nice to have an empirical measurement, or
890 # some better wait to sync up.
891 #
892 # note: --loop-file=no prevents it from hanging if you have that
893 # set to inf the mpv config.
894 # --loop=no prevents it from exit code 3 due to stdin if you
895 # had it set to inf in mpv config.
896 #
897 # args go to mpv, for example --volume=80, 50%
898 cat >$out <<EOFOUTER
899 #!/bin/bash
900 trap "trap - TERM && kill 0" INT TERM ERR; set -e
901 ( sleep .2; scriptreplay <( cat <<'EOF'
902 $(cat $tmp/timing)
903 EOF
904 ) <( cat <<'EOF'
905 $(cat $tmp/typescript)
906 EOF
907 ))&
908 base64 -d - <<'EOF'| mpv --loop=no --loop-file=no --no-terminal --no-audio-display "\$@" -
909 $(base64 "$1")
910 EOF
911 kill 0
912 EOFOUTER
913 rm -r $tmp
914 chmod +x $out
915 }
916
917 smeld() { # ssh meld usage host1 host2 file
918 meld <(ssh $1 cat $3) <(ssh $2 cat $3)
919 }
920
921 spd() {
922 PATH=/usr/local/spdhackfix:$PATH command spd "$@"
923 }
924
925 spend() {
926 sudo systemctl suspend
927 }
928
929 # ssh, copy my universal config over if needed.
930
931 # By default .bashrc is sourced for ALL ssh commands. This is wonky.
932 # Normally, this file is not sourced when a script is run, but we can
933 # override that by sourcing ~/.bashrc. I want the same thing for ssh
934 # commands. when a local script runs an ssh command, bashrc should not be
935 # sourced, unless we use a modified command.
936 #
937 # So, in my bashrc, test for conditions of noninteractive ssh and return
938 # if so. And we don't keep the rest of the code in .bashrc, because
939 # even though we return, we parse the whole file which can cause errors
940 # as we develop it.
941 #
942 # To test for an overriding condition: bash builtin vars and env show no
943 # difference in ssh vs local, except shell level which is not
944 # reliable. one option is to use an environment variable. env variables
945 # sent across ssh are strictly limited. We could override an obscure
946 # unused LC_var, like telephone, but I don't want to run into some edge
947 # case where that messes things up. I choose to set SendEnv and
948 # AcceptEnv ssh config vars to allow the environment variable BRC to
949 # propagate across ssh, and for hosts I don't control, I start an inner
950 # shell with it set, which doubles up as a way to have a nondefault
951 # bashrc.
952 sl() {
953 # inspired from https://github.com/Russell91/sshrc
954
955
956 local args info_date info_t type now tmp tmp2 old sshinfo cmd haveinfo dorsync info_sec
957 declare -a args tmpa
958 now=$(date +%s)
959
960 # ssh [-1246Antivivisectionist] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
961 # [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-L address]
962 # [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option]
963 # [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] [user@]hostname
964 # [command]
965
966 while [[ $1 ]]; do
967 case "$1" in
968 -[1246AaCfGgKkMNnqsTtVvXxYy])
969 args+=("$1"); shift
970 ;;
971 -[bcDEeFIiLlmOopQRSWw]*)
972 # -oOption etc is valid
973 if (( ${#1} >= 3 )); then
974 args+=("$1"); shift
975 else
976 args+=("$1" "$2"); shift 2
977 fi
978 ;;
979 *)
980 break
981 ;;
982 esac
983 done
984 remote="$1"; shift
985 if [[ ! $remote ]]; then
986 echo $0: error hostname required >&2
987 return 1
988 fi
989 dorsync=false
990 haveinfo=false
991 tmpa=(/p/sshinfo/???????????"$remote")
992 sshinfo=${tmpa[0]}
993 if [[ -e $sshinfo ]]; then
994 haveinfo=true
995 fi
996 if $haveinfo; then
997 tmp=${sshinfo[0]##*/}
998 tmp2=${tmp::11}
999 type=${tmp2: -1}
1000 if [[ $type == b ]]; then
1001 info_sec=${tmp::10}
1002 for f in /b/ds/sl/.iank/*; do
1003 if (( $(stat -L -c%Y $f) > info_sec )); then
1004 dorsync=true
1005 rm -f $sshinfo
1006 break
1007 fi
1008 done
1009 fi
1010 else
1011 # use this weird yes thing to ensure we know ssh succeeded
1012 if ! tmp=$(command ssh "${args[@]}" "$remote" "if test -e /a/bin/ds/.bashrc -a -L .bashrc; then echo yes; fi"); then
1013 echo failed sl test. doing plain ssh -v
1014 command ssh -v "${args[@]}" "$remote"
1015 fi
1016 if [[ $tmp == yes ]]; then
1017 type=a
1018 else
1019 dorsync=true
1020 type=b
1021 fi
1022 fi
1023 if $dorsync; then
1024 RSYNC_RSH="ssh ${args[*]}" rsync -rptL /b/ds/sl/.iank "$remote":
1025 fi
1026 if $dorsync || ! $haveinfo; then
1027 sshinfo=/p/sshinfo/$now$type"$remote"
1028 touch $sshinfo
1029 chmod 666 $sshinfo
1030 fi
1031 if [[ $type == b ]]; then
1032 if (( ${#@} )); then
1033
1034 # Theres a couple ways to do this. im not sure whats best,
1035 # but relying on bash 4.4+ escape quoting seems most reliable.
1036 command ssh "${args[@]}" "$remote" \
1037 BRC=t bash -c '.\ .iank/.bashrc\;"\"\$@\""' bash ${@@Q}
1038 elif [[ ! -t 0 ]]; then
1039 # This case is when commands are being piped to ssh.
1040 # Normally, no bashrc gets sourced.
1041 # But, since we are doing all this, lets source it because we can.
1042 cat <(echo . .iank/.bashrc) - | command ssh "${args[@]}" "$remote" BRC=t bash
1043 else
1044 command ssh -t "${args[@]}" "$remote" BRC=t INPUTRC=.iank/.inputrc bash --rcfile .iank/.bashrc
1045 fi
1046 else
1047 if [[ -t 0 ]]; then
1048 BRC=t command ssh "${args[@]}" "$remote" ${@@Q}
1049 else
1050 command ssh "${args[@]}" "$remote" BRC=t bash
1051 fi
1052 fi
1053 }
1054 sss() { # ssh solo
1055 sl -oControlMaster=no -oControlPath=/ "$@"
1056 }
1057 # kill off old shared socket then ssh
1058 ssk() {
1059 m ssh -O exit "$@" || [[ $? == 255 ]]
1060 m sl "$@"
1061 }
1062 # plain limited ssh
1063 ssh() {
1064 BRC=t command ssh "$@"
1065 }
1066
1067
1068 # mail related
1069 testmail() {
1070 declare -gi _seq; _seq+=1
1071 echo "test body" | m mail -s "test mail from $HOSTNAME, $_seq" "${@:-root@localhost}"
1072 # for testing to send from an external address, you can do for example
1073 # -fian@iank.bid -aFrom:ian@iank.bid web-6fnbs@mail-tester.com
1074 # note in exim, you can retry a deferred message
1075 # s exim -M MSG_ID
1076 # MSG_ID is in /var/log/exim4/mainlog, looks like 1ccdnD-0001nh-EN
1077 }
1078
1079 # to test sieve, use below command. for fsf mail, see offlineimap-sync script
1080 # make modifications, then copy to live file, use -eW to actually modify mailbox
1081 #
1082 # Another option is to use sieve-test SCRIPT MAIL_FILE. note,
1083 # sieve-test doesnt know about envelopes, Im not sure if sieve-filter does.
1084
1085 # sieve with output filter. arg is mailbox, like INBOX.
1086 # This depends on dovecot conf, notably mail_location in /etc/dovecot/conf.d/10-mail.conf
1087
1088 _dosieve() {
1089 sieve-filter "$@" 2> >(head; tail) >/tmp/testsieve.log && sed -rn '/^Performed actions:/,/^[^ ]/{/^ /p}' /tmp/testsieve.log | sort | uniq -c
1090 }
1091
1092 # always run this first, edit the test files, then run the following
1093 testsieve() {
1094 _dosieve ~/sieve/maintest.sieve ${1:-INBOX} delete
1095 }
1096 runsieve() {
1097 c ~/sieve; cp personal{test,}.sieve; cp lists{test,}.sieve; cp personalend{test,}.sieve
1098 _dosieve ~/sieve/main.sieve -eW ${1:-INBOX} delete
1099 }
1100
1101 # mail related
1102 testexim() {
1103 # testmail above calls sendmail, which is a link to exim/postfix.
1104 # its docs dont say a way of adding an argument
1105 # to sendmail to turn on debug output. We could make a wrapper, but
1106 # that is a pain. Exim debug args are documented here:
1107 # http://www.exim.org/exim-html-current/doc/html/spec_html/ch-the_exim_command_line.html
1108 #
1109 # http://www.exim.org/exim-html-current/doc/html/spec_html/ch-building_and_installing_exim.html
1110 # note, for exim daemon, you can turn on debug options by
1111 # adding -d, etc to COMMONOPTIONS in
1112 # /etc/default/exim4
1113 #
1114 # to specify recipients other than those in to, cc, bcc, you can use the cli args, eg:
1115 # exim -i 'test@zroe.org, t2@zroe.org' <<'EOF'
1116 #
1117 #
1118 exim -d -t <<'EOF'
1119 From: i@dmarctest.b8.nz
1120 To: mailman@dev.fsf.org
1121 Subject: test2
1122 Reply-to: rtest@iankelling.org
1123
1124 This is a test message.
1125 EOF
1126 }
1127
1128 # toggle keyboard
1129 tk() {
1130 # based on
1131 # https://askubuntu.com/questions/160945/is-there-a-way-to-disable-a-laptops-internal-keyboard
1132 id=$(xinput --list --id-only 'AT Translated Set 2 keyboard')
1133 if xinput list | grep -F '∼ AT Translated Set 2 keyboard' &>/dev/null; then
1134 echo enabling keyboard
1135 # find the first slave keyboard number, they are all the same in my output.
1136 # if they werent, worst case we would need to save the slave number somewhere
1137 # when it got disabled.
1138 slave=$(xinput list | sed -n 's/.*slave \+keyboard (\([0-9]*\)).*/\1/p' | head -n1)
1139 xinput reattach $id $slave
1140 else
1141 xinput float $id
1142 fi
1143 }
1144
1145 tm() {
1146 # timer in minutes
1147 # --no-config
1148 (sleep $(calc "$* * 60") && mpv --no-config --volume 50 /a/bin/data/alarm.mp3) > /dev/null 2>&1 &
1149 }
1150
1151 trg() { transmission-remote-gtk&r; }
1152 trc() {
1153 # example, set global upload limit to 100 kilobytes:
1154 # trc -u 100
1155 TR_AUTH=":$(jq -r .profiles[0].password ~/.config/transmission-remote-gtk/config.json)" transmission-remote transmission.lan -ne "$@"
1156 }
1157
1158
1159 tu() {
1160 local s dir
1161 dir="$(dirname "$1")"
1162 if [[ -e $1 && ! -w $1 || ! -w $(dirname "$1") ]]; then
1163 s=s;
1164 fi
1165 # full path for using in some initial setup steps
1166 $s /a/exe/teeu "$@"
1167 }
1168
1169 vpncmd() {
1170 #m s nsenter -t $(pgrep -f "/usr/sbin/openvpn .* --config /etc/openvpn/.*pia.conf") -n -m "$@"
1171 m s nsenter -t $(pgrep -f "/usr/sbin/openvpn .* --config /etc/openvpn/.*client.conf") -n -m "$@"
1172 }
1173 vpnf() {
1174 vpncmd gksudo -u iank "firefox -no-remote -P vpn" &r
1175 }
1176 vpni() {
1177 vpncmd gksudo -u iank "$*"
1178 }
1179 vpnbash() {
1180 vpncmd bash
1181 }
1182
1183
1184 vpn() {
1185 if [[ -e /lib/systemd/system/openvpn-client@.service ]]; then
1186 local vpn_service=openvpn-client
1187 else
1188 local vpn_service=openvpn
1189 fi
1190
1191 [[ $1 ]] || { echo need arg; return 1; }
1192 journalctl --unit=$vpn_service@$1 -f -n0 &
1193 sudo systemctl start $vpn_service@$1
1194 # sometimes the ask-password agent does not work and needs a delay.
1195 sleep .5
1196 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=779240
1197 # noticed around 8-2017 after update from around stretch release
1198 # on debian testing, even though the bug is much older.
1199 sudo systemd-tty-ask-password-agent
1200 }
1201
1202 vpnoff() {
1203 [[ $1 ]] || { echo need arg; return 1; }
1204 if [[ -e /lib/systemd/system/openvpn-client@.service ]]; then
1205 local vpn_service=openvpn-client
1206 else
1207 local vpn_service=openvpn
1208 fi
1209 sudo systemctl stop $vpn_service@$1
1210 }
1211 vpnoffc() { # vpn off client
1212 ser stop openvpn-nn@client
1213 }
1214 vpnc() {
1215 ser start openvpn-nn@client
1216 }
1217
1218
1219 vspicy() { # usage: VIRSH_DOMAIN
1220 # connect to vms made with virt-install
1221 spicy -p $(sudo virsh dumpxml "$1"|grep "<graphics.*type='spice'"|\
1222 sed -r "s/.*port='([0-9]+).*/\1/")
1223 }
1224
1225 wian() {
1226 cat-new-files /m/4e/INBOX/new
1227 }
1228
1229 wtr() { curl wttr.in/boston; }
1230
1231 xevkb() { xev -event keyboard; }
1232
1233 ziva() { e "toot! i love dancing. fart"; }
1234
1235 # * misc stuff
1236
1237 # from curl cheat.sh/:bash_completion
1238 _cheatsh_complete_curl()
1239 {
1240 local cur prev opts
1241 _get_comp_words_by_ref -n : cur
1242
1243 COMPREPLY=()
1244 #cur="${COMP_WORDS[COMP_CWORD]}"
1245 prev="${COMP_WORDS[COMP_CWORD-1]}"
1246 opts="$(curl -s cheat.sh/:list | sed s@^@cheat.sh/@)"
1247
1248 if [[ ${cur} == cheat.sh/* ]] ; then
1249 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
1250 __ltrim_colon_completions "$cur"
1251 return 0
1252 fi
1253 }
1254 complete -F _cheatsh_complete_curl curl
1255
1256
1257
1258
1259 reset-konsole() {
1260 # we also have a file in /a/c/...konsole...
1261 local f=$HOME/.config/konsolerc
1262 setini DefaultProfile profileian.profile "Desktop Entry" $f
1263 setini Favorites profileian.profile "Favorite Profiles" $f
1264 setini ShowMenuBarByDefault false KonsoleWindow $f
1265 setini TabBarPosition Top TabBar $f
1266 }
1267
1268 reset-sakura() {
1269 while -r read k v; do
1270 # shellcheck disable=SC2154
1271 setini $k $v sakura /a/c/subdir_files/.config/sakura/sakura.conf
1272 done <<'EOF'
1273 colorset1_back rgb(33,37,39
1274 less_questions true
1275 audible_bell No
1276 visible_bell No
1277 disable_numbered_tabswitch true
1278 scroll_lines 10000000
1279 scrollbar true
1280 EOF
1281 }
1282
1283 reset-xscreensaver() {
1284 # except for spash, i set these by setting gui options in
1285 # xscreensaver-command -demo
1286 # then finding the corresponding option in .xscreensaver
1287 # spash, i happened to notice in .xscreensaver
1288 #
1289 # dpmsOff, monitor doesnt come back on using old free software supported nvidia card
1290 cat > /home/iank/.xscreensaver <<'EOF'
1291 mode: blank
1292 dpmsEnabled: True
1293 dpmsStandby: 0:02:00
1294 dpmsSuspend: 0:03:00
1295 dpmsOff: 0:00:00
1296 timeout: 0:02:00
1297 lock: True
1298 lockTimeout: 0:03:00
1299 splash: False
1300 EOF
1301
1302 }
1303
1304
1305 # * stuff that makes sense to be at the end
1306 if [[ "$SUDOD" ]]; then
1307 cd "$SUDOD"
1308 unset SUDOD
1309 elif [[ -d /a ]] && [[ $PWD == "$HOME" ]] && [[ $- == *i* ]]; then
1310 cd /a
1311 fi
1312
1313
1314 # best practice
1315 unset IFS
1316
1317
1318 # for mitmproxy to get a newer python.
1319 # commented until i want to use it because it
1320 # noticably slows bash startup
1321 #
1322
1323 mypyenvinit () {
1324 if [[ $EUID == 0 || ! -e ~/.pyenv/bin ]]; then
1325 echo "error: dont be root. make sure pyenv is installed"
1326 return 1
1327 fi
1328 export PATH="$HOME/.pyenv/bin:$PATH"
1329 eval "$(pyenv init -)"
1330 eval "$(pyenv virtualenv-init -)"
1331 }
1332
1333
1334 export GOPATH=$HOME/go
1335 path-add $GOPATH/bin
1336 path-add /usr/local/go/bin
1337
1338 # I have the git repo and a release. either one should work.
1339 # I have both because I was trying to solve an issue that
1340 # turned out to be unrelated.
1341 # ARDUINO_PATH=/a/opt/Arduino/build/linux/work
1342 export ARDUINO_PATH=/a/opt/arduino-1.8.9
1343
1344 # They want to be added to the start, but i think
1345 # that should be avoided unless we really need it.
1346 path-add --end ~/.npm-global
1347
1348 path-add --end $HOME/.cargo/bin
1349
1350 # taken from default changes to bashrc and bash_profile
1351 path-add --end $HOME/.rvm/bin
1352 path-add --end $HOME/.gem/ruby/2.3.0/bin
1353
1354
1355 export BASEFILE_DIR=/a/bin/fai-basefiles
1356
1357 #export ANDROID_HOME=/a/opt/android-home
1358 # https://f-droid.org/en/docs/Installing_the_Server_and_Repo_Tools/
1359 #export USE_SDK_WRAPPER=yes
1360 #PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
1361
1362 # didnt get drush working, if I did, this seems like the
1363 # only good thing to include for it.
1364 # Include Drush completion.
1365 # if [ -f "/home/ian/.drush/drush.complete.sh" ] ; then
1366 # source /home/ian/.drush/drush.complete.sh
1367 # fi
1368
1369
1370 # https://wiki.archlinux.org/index.php/Xinitrc#Autostart_X_at_login
1371 # i added an extra condition as gentoo xorg guide says depending on
1372 # $DISPLAY is fragile.
1373 if [[ ! $DISPLAY && $XDG_VTNR == 1 ]] && shopt -q login_shell && isarch; then
1374 exec startx
1375 fi
1376
1377
1378 # ensure no bad programs appending to this file will have an affect
1379 return 0