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