fix jrun
[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 disable btrbk.timer
118 fi
119 if systemctl is-active btrbk.service; then
120 $active && ser enable btrbk.timer
121 echo "cron btrbk is already running"
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 reenabled due to failure
130 else
131 ser enable 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 # this will fail is dnsmasq is failed
811 hr; m ser status dnsmasq | cat || :
812 hr; s ss -lpn 'sport = 53'
813 #hr; echo dnsmasq is $(systemctl is-active dnsmasq)
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 }
820 rcat() {
821 resolvcat | less
822 }
823 reresolv() {
824 sudo nscd -i hosts
825 sudo systemctl restart dnsmasq
826 }
827
828 # only run on MAIL_HOST. simpler to keep this on one system.
829 r2eadd() { # usage: name url
830 # initial setup of rss2email:
831 # r2e new r2e@iankelling.org
832 # that initializes files, and sets default email.
833 # symlink to the config doesnt work, so I copied it to /p/c
834 # and then use cli option to specify explicit path.
835 # Only option changed from default config is to set
836 # force-from = True
837 #
838 # or else for a few feeds, the from address is set by the feed, and
839 # if I fail delivery, then I send a bounce message to that from
840 # address, which makes me be a spammer.
841
842 r2e add $1 "$2" $1@r2e.iankelling.org
843 # get up to date and dont send old entries now:
844 r2e run --no-send $1
845 }
846 r2e() { command r2e -d /p/c/rss2email.json -c /p/c/rss2email.cfg "$@"; }
847
848 rspicy() { # usage: HOST DOMAIN
849 # connect to spice vm remote host. use vspicy for local host
850 local port
851 # shellcheck disable=SC2087
852 port=$(ssh $1<<EOF
853 sudo virsh dumpxml $2|grep "<graphics.*type='spice'" | \
854 sed -rn "s/.*port='([0-9]+).*/\1/p"
855 EOF
856 )
857 if [[ $port ]]; then
858 spicy -h $1 -p $port
859 else
860 echo "error: no port found. check that the domain is running."
861 fi
862 }
863
864
865 scssl() {
866 # s gem install scss-lint
867 pushd /a/opt/thoughtbot-guides
868 git pull --stat
869 popd
870 scss-lint -c /a/opt/thoughtbot-guides/style/sass/.scss-lint.yml "$@"
871 }
872
873 skbrc() {
874 sk -e 2120,245 /b/ds/brc /b/ds/brc2
875 }
876
877 skaraoke() {
878 local tmp out
879 out=${2:-${1%.*}.sh}
880 tmp=$(mktemp -d)
881 script -t -c "mpv --no-config --no-resume-playback --no-terminal --no-audio-display '$1'" $tmp/typescript 2>$tmp/timing
882 # todo, the current sleep seems pretty good, but it
883 # would be nice to have an empirical measurement, or
884 # some better wait to sync up.
885 #
886 # note: --loop-file=no prevents it from hanging if you have that
887 # set to inf the mpv config.
888 # --loop=no prevents it from exit code 3 due to stdin if you
889 # had it set to inf in mpv config.
890 #
891 # args go to mpv, for example --volume=80, 50%
892 cat >$out <<EOFOUTER
893 #!/bin/bash
894 trap "trap - TERM && kill 0" INT TERM ERR; set -e
895 ( sleep .2; scriptreplay <( cat <<'EOF'
896 $(cat $tmp/timing)
897 EOF
898 ) <( cat <<'EOF'
899 $(cat $tmp/typescript)
900 EOF
901 ))&
902 base64 -d - <<'EOF'| mpv --loop=no --loop-file=no --no-terminal --no-audio-display "\$@" -
903 $(base64 "$1")
904 EOF
905 kill 0
906 EOFOUTER
907 rm -r $tmp
908 chmod +x $out
909 }
910
911 smeld() { # ssh meld usage host1 host2 file
912 meld <(ssh $1 cat $3) <(ssh $2 cat $3)
913 }
914
915 spd() {
916 PATH=/usr/local/spdhackfix:$PATH command spd "$@"
917 }
918
919 spend() {
920 sudo systemctl suspend
921 }
922
923 # ssh, copy my universal config over if needed.
924
925 # By default .bashrc is sourced for ALL ssh commands. This is wonky.
926 # Normally, this file is not sourced when a script is run, but we can
927 # override that by sourcing ~/.bashrc. I want the same thing for ssh
928 # commands. when a local script runs an ssh command, bashrc should not be
929 # sourced, unless we use a modified command.
930 #
931 # So, in my bashrc, test for conditions of noninteractive ssh and return
932 # if so. And we don't keep the rest of the code in .bashrc, because
933 # even though we return, we parse the whole file which can cause errors
934 # as we develop it.
935 #
936 # To test for an overriding condition: bash builtin vars and env show no
937 # difference in ssh vs local, except shell level which is not
938 # reliable. one option is to use an environment variable. env variables
939 # sent across ssh are strictly limited. We could override an obscure
940 # unused LC_var, like telephone, but I don't want to run into some edge
941 # case where that messes things up. I choose to set SendEnv and
942 # AcceptEnv ssh config vars to allow the environment variable BRC to
943 # propagate across ssh, and for hosts I don't control, I start an inner
944 # shell with it set, which doubles up as a way to have a nondefault
945 # bashrc.
946 sl() {
947 # inspired from https://github.com/Russell91/sshrc
948
949
950 local args info_date info_t type now tmp tmp2 old sshinfo cmd haveinfo dorsync info_sec
951 declare -a args tmpa
952 now=$(date +%s)
953
954 # ssh [-1246Antivivisectionist] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
955 # [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-L address]
956 # [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option]
957 # [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] [user@]hostname
958 # [command]
959
960 while [[ $1 ]]; do
961 case "$1" in
962 -[1246AaCfGgKkMNnqsTtVvXxYy])
963 args+=("$1"); shift
964 ;;
965 -[bcDEeFIiLlmOopQRSWw]*)
966 # -oOption etc is valid
967 if (( ${#1} >= 3 )); then
968 args+=("$1"); shift
969 else
970 args+=("$1" "$2"); shift 2
971 fi
972 ;;
973 *)
974 break
975 ;;
976 esac
977 done
978 remote="$1"; shift
979 if [[ ! $remote ]]; then
980 echo $0: error hostname required >&2
981 return 1
982 fi
983 dorsync=false
984 haveinfo=false
985 tmpa=(/p/sshinfo/???????????"$remote")
986 sshinfo=${tmpa[0]}
987 if [[ -e $sshinfo ]]; then
988 haveinfo=true
989 fi
990 if $haveinfo; then
991 tmp=${sshinfo[0]##*/}
992 tmp2=${tmp::11}
993 type=${tmp2: -1}
994 if [[ $type == b ]]; then
995 info_sec=${tmp::10}
996 for f in /b/ds/sl/.iank/*; do
997 if (( $(stat -L -c%Y $f) > info_sec )); then
998 dorsync=true
999 rm -f $sshinfo
1000 break
1001 fi
1002 done
1003 fi
1004 else
1005 # use this weird yes thing to ensure we know ssh succeeded
1006 if ! tmp=$(command ssh "${args[@]}" "$remote" "if test -e /a/bin/ds/.bashrc -a -L .bashrc; then echo yes; fi"); then
1007 echo failed sl test. doing plain ssh -v
1008 command ssh -v "${args[@]}" "$remote"
1009 fi
1010 if [[ $tmp == yes ]]; then
1011 type=a
1012 else
1013 dorsync=true
1014 type=b
1015 fi
1016 fi
1017 if $dorsync; then
1018 RSYNC_RSH="ssh ${args[*]}" rsync -rptL /b/ds/sl/.iank "$remote":
1019 fi
1020 if $dorsync || ! $haveinfo; then
1021 sshinfo=/p/sshinfo/$now$type"$remote"
1022 touch $sshinfo
1023 chmod 666 $sshinfo
1024 fi
1025 if [[ $type == b ]]; then
1026 if (( ${#@} )); then
1027
1028 # Theres a couple ways to do this. im not sure whats best,
1029 # but relying on bash 4.4+ escape quoting seems most reliable.
1030 command ssh "${args[@]}" "$remote" \
1031 BRC=t bash -c '.\ .iank/.bashrc\;"\"\$@\""' bash ${@@Q}
1032 elif [[ ! -t 0 ]]; then
1033 # This case is when commands are being piped to ssh.
1034 # Normally, no bashrc gets sourced.
1035 # But, since we are doing all this, lets source it because we can.
1036 cat <(echo . .iank/.bashrc) - | command ssh "${args[@]}" "$remote" BRC=t bash
1037 else
1038 command ssh -t "${args[@]}" "$remote" BRC=t INPUTRC=.iank/.inputrc bash --rcfile .iank/.bashrc
1039 fi
1040 else
1041 if [[ -t 0 ]]; then
1042 BRC=t command ssh "${args[@]}" "$remote" ${@@Q}
1043 else
1044 command ssh "${args[@]}" "$remote" BRC=t bash
1045 fi
1046 fi
1047 }
1048 sss() { # ssh solo
1049 sl -oControlMaster=no -oControlPath=/ "$@"
1050 }
1051 # kill off old shared socket then ssh
1052 ssk() {
1053 m ssh -O exit "$@" || [[ $? == 255 ]]
1054 m sl "$@"
1055 }
1056 # plain limited ssh
1057 ssh() {
1058 BRC=t command ssh "$@"
1059 }
1060
1061
1062 # mail related
1063 testmail() {
1064 declare -gi _seq; _seq+=1
1065 echo "test body" | m mail -s "test mail from $HOSTNAME, $_seq" "${@:-root@localhost}"
1066 # for testing to send from an external address, you can do for example
1067 # -fian@iank.bid -aFrom:ian@iank.bid web-6fnbs@mail-tester.com
1068 # note in exim, you can retry a deferred message
1069 # s exim -M MSG_ID
1070 # MSG_ID is in /var/log/exim4/mainlog, looks like 1ccdnD-0001nh-EN
1071 }
1072
1073 # to test sieve, use below command. for fsf mail, see offlineimap-sync script
1074 # make modifications, then copy to live file, use -eW to actually modify mailbox
1075 #
1076 # Another option is to use sieve-test SCRIPT MAIL_FILE. note,
1077 # sieve-test doesnt know about envelopes, Im not sure if sieve-filter does.
1078
1079 # sieve with output filter. arg is mailbox, like INBOX.
1080 # This depends on dovecot conf, notably mail_location in /etc/dovecot/conf.d/10-mail.conf
1081
1082 _dosieve() {
1083 sieve-filter "$@" 2> >(head; tail) >/tmp/testsieve.log && sed -rn '/^Performed actions:/,/^[^ ]/{/^ /p}' /tmp/testsieve.log | sort | uniq -c
1084 }
1085
1086 # always run this first, edit the test files, then run the following
1087 testsieve() {
1088 _dosieve ~/sieve/maintest.sieve ${1:-INBOX} delete
1089 }
1090 runsieve() {
1091 c ~/sieve; cp personal{test,}.sieve; cp lists{test,}.sieve; cp personalend{test,}.sieve
1092 _dosieve ~/sieve/main.sieve -eW ${1:-INBOX} delete
1093 }
1094
1095 # mail related
1096 testexim() {
1097 # testmail above calls sendmail, which is a link to exim/postfix.
1098 # its docs dont say a way of adding an argument
1099 # to sendmail to turn on debug output. We could make a wrapper, but
1100 # that is a pain. Exim debug args are documented here:
1101 # http://www.exim.org/exim-html-current/doc/html/spec_html/ch-the_exim_command_line.html
1102 #
1103 # http://www.exim.org/exim-html-current/doc/html/spec_html/ch-building_and_installing_exim.html
1104 # note, for exim daemon, you can turn on debug options by
1105 # adding -d, etc to COMMONOPTIONS in
1106 # /etc/default/exim4
1107 #
1108 # to specify recipients other than those in to, cc, bcc, you can use the cli args, eg:
1109 # exim -i 'test@zroe.org, t2@zroe.org' <<'EOF'
1110 #
1111 #
1112 exim -d -t <<'EOF'
1113 From: i@dmarctest.b8.nz
1114 To: mailman@dev.fsf.org
1115 Subject: test2
1116 Reply-to: rtest@iankelling.org
1117
1118 This is a test message.
1119 EOF
1120 }
1121
1122 # toggle keyboard
1123 tk() {
1124 # based on
1125 # https://askubuntu.com/questions/160945/is-there-a-way-to-disable-a-laptops-internal-keyboard
1126 id=$(xinput --list --id-only 'AT Translated Set 2 keyboard')
1127 if xinput list | grep -F '∼ AT Translated Set 2 keyboard' &>/dev/null; then
1128 echo enabling keyboard
1129 # find the first slave keyboard number, they are all the same in my output.
1130 # if they werent, worst case we would need to save the slave number somewhere
1131 # when it got disabled.
1132 slave=$(xinput list | sed -n 's/.*slave \+keyboard (\([0-9]*\)).*/\1/p' | head -n1)
1133 xinput reattach $id $slave
1134 else
1135 xinput float $id
1136 fi
1137 }
1138
1139 tm() {
1140 # timer in minutes
1141 # --no-config
1142 (sleep $(calc "$* * 60") && mpv --no-config --volume 50 /a/bin/data/alarm.mp3) > /dev/null 2>&1 &
1143 }
1144
1145 trg() { transmission-remote-gtk&r; }
1146 trc() {
1147 # example, set global upload limit to 100 kilobytes:
1148 # trc -u 100
1149 TR_AUTH=":$(jq -r .profiles[0].password ~/.config/transmission-remote-gtk/config.json)" transmission-remote transmission.lan -ne "$@"
1150 }
1151
1152
1153 tu() {
1154 local s dir
1155 dir="$(dirname "$1")"
1156 if [[ -e $1 && ! -w $1 || ! -w $(dirname "$1") ]]; then
1157 s=s;
1158 fi
1159 # full path for using in some initial setup steps
1160 $s /a/exe/teeu "$@"
1161 }
1162
1163 vpncmd() {
1164 #m s nsenter -t $(pgrep -f "/usr/sbin/openvpn .* --config /etc/openvpn/.*pia.conf") -n -m "$@"
1165 m s nsenter -t $(pgrep -f "/usr/sbin/openvpn .* --config /etc/openvpn/.*client.conf") -n -m "$@"
1166 }
1167 vpnf() {
1168 vpncmd gksudo -u iank "firefox -no-remote -P vpn" &r
1169 }
1170 vpni() {
1171 vpncmd gksudo -u iank "$*"
1172 }
1173 vpnbash() {
1174 vpncmd bash
1175 }
1176
1177
1178 vpn() {
1179 if [[ -e /lib/systemd/system/openvpn-client@.service ]]; then
1180 local vpn_service=openvpn-client
1181 else
1182 local vpn_service=openvpn
1183 fi
1184
1185 [[ $1 ]] || { echo need arg; return 1; }
1186 journalctl --unit=$vpn_service@$1 -f -n0 &
1187 sudo systemctl start $vpn_service@$1
1188 # sometimes the ask-password agent does not work and needs a delay.
1189 sleep .5
1190 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=779240
1191 # noticed around 8-2017 after update from around stretch release
1192 # on debian testing, even though the bug is much older.
1193 sudo systemd-tty-ask-password-agent
1194 }
1195
1196 vpnoff() {
1197 [[ $1 ]] || { echo need arg; return 1; }
1198 if [[ -e /lib/systemd/system/openvpn-client@.service ]]; then
1199 local vpn_service=openvpn-client
1200 else
1201 local vpn_service=openvpn
1202 fi
1203 sudo systemctl stop $vpn_service@$1
1204 }
1205 vpnoffc() { # vpn off client
1206 ser stop openvpn-nn@client
1207 }
1208 vpnc() {
1209 ser start openvpn-nn@client
1210 }
1211
1212
1213 vspicy() { # usage: VIRSH_DOMAIN
1214 # connect to vms made with virt-install
1215 spicy -p $(sudo virsh dumpxml "$1"|grep "<graphics.*type='spice'"|\
1216 sed -r "s/.*port='([0-9]+).*/\1/")
1217 }
1218
1219 wian() {
1220 cat-new-files /m/4e/INBOX/new
1221 }
1222
1223 wtr() { curl wttr.in/boston; }
1224
1225 xevkb() { xev -event keyboard; }
1226
1227 ziva() { e "toot! i love dancing. fart"; }
1228
1229 # * misc stuff
1230
1231 # from curl cheat.sh/:bash_completion
1232 _cheatsh_complete_curl()
1233 {
1234 local cur prev opts
1235 _get_comp_words_by_ref -n : cur
1236
1237 COMPREPLY=()
1238 #cur="${COMP_WORDS[COMP_CWORD]}"
1239 prev="${COMP_WORDS[COMP_CWORD-1]}"
1240 opts="$(curl -s cheat.sh/:list | sed s@^@cheat.sh/@)"
1241
1242 if [[ ${cur} == cheat.sh/* ]] ; then
1243 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
1244 __ltrim_colon_completions "$cur"
1245 return 0
1246 fi
1247 }
1248 complete -F _cheatsh_complete_curl curl
1249
1250
1251
1252
1253 reset-konsole() {
1254 # we also have a file in /a/c/...konsole...
1255 local f=$HOME/.config/konsolerc
1256 setini DefaultProfile profileian.profile "Desktop Entry" $f
1257 setini Favorites profileian.profile "Favorite Profiles" $f
1258 setini ShowMenuBarByDefault false KonsoleWindow $f
1259 setini TabBarPosition Top TabBar $f
1260 }
1261
1262 reset-sakura() {
1263 while -r read k v; do
1264 # shellcheck disable=SC2154
1265 setini $k $v sakura /a/c/subdir_files/.config/sakura/sakura.conf
1266 done <<'EOF'
1267 colorset1_back rgb(33,37,39
1268 less_questions true
1269 audible_bell No
1270 visible_bell No
1271 disable_numbered_tabswitch true
1272 scroll_lines 10000000
1273 scrollbar true
1274 EOF
1275 }
1276
1277 reset-xscreensaver() {
1278 # except for spash, i set these by setting gui options in
1279 # xscreensaver-command -demo
1280 # then finding the corresponding option in .xscreensaver
1281 # spash, i happened to notice in .xscreensaver
1282 #
1283 # dpmsOff, monitor doesnt come back on using old free software supported nvidia card
1284 cat > /home/iank/.xscreensaver <<'EOF'
1285 mode: blank
1286 dpmsEnabled: True
1287 dpmsStandby: 0:02:00
1288 dpmsSuspend: 0:03:00
1289 dpmsOff: 0:00:00
1290 timeout: 0:02:00
1291 lock: True
1292 lockTimeout: 0:03:00
1293 splash: False
1294 EOF
1295
1296 }
1297
1298
1299 # * stuff that makes sense to be at the end
1300 if [[ "$SUDOD" ]]; then
1301 cd "$SUDOD"
1302 unset SUDOD
1303 elif [[ -d /a ]] && [[ $PWD == "$HOME" ]] && [[ $- == *i* ]]; then
1304 cd /a
1305 fi
1306
1307
1308 # best practice
1309 unset IFS
1310
1311
1312 # for mitmproxy to get a newer python.
1313 # commented until i want to use it because it
1314 # noticably slows bash startup
1315 #
1316
1317 mypyenvinit () {
1318 if [[ $EUID == 0 || ! -e ~/.pyenv/bin ]]; then
1319 echo "error: dont be root. make sure pyenv is installed"
1320 return 1
1321 fi
1322 export PATH="$HOME/.pyenv/bin:$PATH"
1323 eval "$(pyenv init -)"
1324 eval "$(pyenv virtualenv-init -)"
1325 }
1326
1327
1328 export GOPATH=$HOME/go
1329 path-add $GOPATH/bin
1330 path-add /usr/local/go/bin
1331
1332 # I have the git repo and a release. either one should work.
1333 # I have both because I was trying to solve an issue that
1334 # turned out to be unrelated.
1335 # ARDUINO_PATH=/a/opt/Arduino/build/linux/work
1336 export ARDUINO_PATH=/a/opt/arduino-1.8.9
1337
1338 # They want to be added to the start, but i think
1339 # that should be avoided unless we really need it.
1340 path-add --end ~/.npm-global
1341
1342 path-add --end $HOME/.cargo/bin
1343
1344 # taken from default changes to bashrc and bash_profile
1345 path-add --end $HOME/.rvm/bin
1346 path-add --end $HOME/.gem/ruby/2.3.0/bin
1347
1348
1349 export BASEFILE_DIR=/a/bin/fai-basefiles
1350
1351 #export ANDROID_HOME=/a/opt/android-home
1352 # https://f-droid.org/en/docs/Installing_the_Server_and_Repo_Tools/
1353 #export USE_SDK_WRAPPER=yes
1354 #PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
1355
1356 # didnt get drush working, if I did, this seems like the
1357 # only good thing to include for it.
1358 # Include Drush completion.
1359 # if [ -f "/home/ian/.drush/drush.complete.sh" ] ; then
1360 # source /home/ian/.drush/drush.complete.sh
1361 # fi
1362
1363
1364 # https://wiki.archlinux.org/index.php/Xinitrc#Autostart_X_at_login
1365 # i added an extra condition as gentoo xorg guide says depending on
1366 # $DISPLAY is fragile.
1367 if [[ ! $DISPLAY && $XDG_VTNR == 1 ]] && shopt -q login_shell && isarch; then
1368 exec startx
1369 fi
1370
1371
1372 # ensure no bad programs appending to this file will have an affect
1373 return 0