various bug fixes
[distro-setup] / brc
1 #!/bin/bash
2 # this gets sourced. shebang is just for file mode detection
3
4 # note, to catch errors in functions but not outside, do:
5 # set -E -o pipefail
6 # trap return ERR
7 # trap 'trap ERR' RETURN
8
9
10 # * settings
11
12 CDPATH=.
13
14 set -o pipefail
15
16 # remove all aliases. aliases provided by the system tend to get in the way,
17 # for example, error happens if I try to define a function the same name as an alias
18 unalias -a
19
20 # remove gnome keyring warning messages
21 # there is probably a more proper way, but I didnt find any easily on google
22 # now using xfce+xmonad instead of vanilla xmonad, so disabling this
23 #unset GNOME_KEYRING_CONTROL
24
25 # use extra globing features.
26 shopt -s extglob
27 # include .files when globbing, but ignore files name . and ..
28 # setting this also sets dotglob.
29 # Note, this doesnt work in bash 4.4 anymore, for paths with
30 # more than 1 directory, like a/b/.foo, since * is fixed to not match /
31 export GLOBIGNORE=*/.:*/..
32
33 # broken with bash_completion package. Saw a bug for this once. dont anymore.
34 # still broken in wheezy
35 # still buggered in latest stable from the web, version 2.1
36 # perhaps its fixed in newer git version, which fails to make for me
37 # this note is from 6-2014.
38 # Also, enabling this before sourcing .bashrc makes PATH be empty.
39 #shopt -s nullglob
40
41 # make tab on an empty line do nothing
42 shopt -s no_empty_cmd_completion
43
44 # advanced completion
45 # http://bash-completion.alioth.debian.org/
46 # might be sourced by the system already, but ive noticed it not being sourced before
47 if ! type _init_completion &> /dev/null && [[ -r "/usr/share/bash-completion/bash_completion" ]]; then
48 . /usr/share/bash-completion/bash_completion
49 fi
50
51
52 # fix spelling errors for cd, only in interactive shell
53 shopt -s cdspell
54 # append history instead of overwritting it
55 shopt -s histappend
56 # for compatibility, per gentoo/debian bashrc
57 shopt -s checkwinsize
58 # attempt to save multiline single commands as single history entries.
59 shopt -s cmdhist
60 # enable **
61 shopt -s globstar
62
63
64 # inside emcas fixes
65 if [[ $RLC_INSIDE_EMACS ]]; then
66 # EMACS is used by bash on startup, but we dont need it anymore.
67 # plus I hit a bug in a makefile which inherited it
68 unset EMACS
69 export RLC_INSIDE_EMACS
70 export PAGER=cat
71 export MANPAGER=cat
72 # scp completion does not work, but this doesnt fix it. todo, figure this out
73 complete -r scp &> /dev/null
74 # todo, remote file completion fails, figure out how to turn it off
75 export NODE_DISABLE_COLORS=1
76 # This gets rid of ugly terminal escape chars in node repl
77 # sometime, Id like to have completion working in emacs shell for node
78 # the offending chars can be found in lib/readline.js,
79 # things that do like:
80 # stream.write('\x1b[' + (x + 1) + 'G');
81 # We can remove them and keep readline, for example by doing this
82 # to start a repl:
83 #!/usr/bin/env nodejs
84 # var readline = require('readline');
85 # readline.cursorTo = function(a,b,c) {};
86 # readline.clearScreenDown = function(a) {};
87 # const repl = require('repl');
88 # var replServer = repl.start('');
89 #
90 # no prompt, or else readline complete seems to be confused, based
91 # on our column being different? node probably needs to send
92 # different kind of escape sequence that is not ugly. Anyways,
93 # completion doesnt work yet even with the ugly prompt, so whatever
94 #
95 export NODE_NO_READLINE=1
96
97 fi
98
99
100 if [[ $- == *i* ]]; then
101 # for readline-complete.el
102 if [[ $RLC_INSIDE_EMACS ]]; then
103 # all for readline-complete.el
104 stty echo
105 bind 'set horizontal-scroll-mode on'
106 bind 'set print-completions-horizontally on'
107 bind '"\C-i": self-insert'
108 else
109
110 # todo: not sure this works in sakura
111 #stty werase undef
112 #bind "\C-w": kill-region
113 # sakura == xterm-256color
114 # konsole == xterm
115 if [[ $TERM == "xterm" ]]; then
116 # control + arrow keys. for other terminals, see http://unix.stackexchange.com/questions/10806/how-to-change-previous-next-word-shortcut-in-bash
117 bind '"\e[1;5C": shell-forward-word' 2>/dev/null
118 bind '"\e[1;5D": shell-backward-word' 2>/dev/null
119 else
120 # make ctrl-backspace work. for konsole, i fixed it through
121 # /home/iank/.local/share/konsole/default.keytab
122 stty werase '^h'
123 bind '"\eOc": shell-forward-word'
124 bind '"\eOd": shell-backward-word'
125 fi
126 # i cant remember why i did this, probably to free up some keys to bind
127 # to other things in bash.
128 # other than C-c and C-z, the rest defined by stty -a are, at least in
129 # gnome-terminal, overridden by bash, or disabled by the system
130 stty lnext undef stop undef start undef
131 fi
132
133 fi
134
135
136 # history number. History expansion is good.
137 PS4='$LINENO+ '
138 # history file size limit, set to unlimited.
139 # this needs to be different from the default because
140 # default HISTFILESIZE is 500 and could clobber our history
141 HISTFILESIZE=
142 # max commands 1 session can append/read from history
143 HISTSIZE=100000
144 # my own history size limit based on lines
145 HISTFILELINES=1000000
146 HISTFILE=$HOME/.bh
147 # the time format display when doing the history command
148 # also, setting this makes the history file record time
149 # of each command as seconds from the epoch
150 HISTTIMEFORMAT="%Y-%m-%d %I:%M %p "
151 # consecutive duplicate lines dont go in history
152 HISTCONTROL=ignoredups
153 # works in addition to HISTCONTROL to do more flexible things
154 # it could also do the same things as HISTCONTROL and thus replace it,
155 # but meh. dunno why, but just " *" does glob expansion, so use [ ] to avoid it.
156 HISTIGNORE='pass *:[ ]*:lom '
157
158 export BC_LINE_LENGTH=0
159
160
161 # note, if I use a machine I dont want files readable by all users, set
162 # umask 077 # If fewer than 4 digits are entered, leading zeros are assumed
163
164 C_DEFAULT_DIR=/a
165
166 # i for insensitive. the rest from
167 # https://superuser.com/questions/366930/how-do-i-get-the-git-pager-to-clean-up-screen-output-after-exit
168 # and reading the man
169 export LESS=RXi
170
171 # * include files
172 for _x in /a/bin/distro-functions/src/* /a/bin/!(githtml)/*-function?(s); do
173 source "$_x"
174 done
175 unset _x
176 # so I can share my bashrc
177 for x in /a/bin/bash_unpublished/source-!(.#*); do source $x; done
178 source $(dirname $(readlink -f $BASH_SOURCE))/path_add-function
179 source /a/bin/log-quiet/logq-function
180 if [[ -e /a/bin/errhandle/err ]]; then
181 source /a/bin/errhandle/err
182 err-allow
183 fi
184 path_add /a/exe
185 # pip3 --user things go here:
186 path_add --end ~/.local/bin
187 path_add --ifexists --end /a/work/libremanage
188 path_add --ifexists --end /a/opt/adt-bundle*/tools /a/opt/adt-bundle*/platform-tools
189 path_add --ifexists --end /a/opt/scancode-toolkit-2.9.2
190 export WCDHOME=/a
191 # based on readme.debian. dunno if this will break on other distros.
192 _x=/usr/share/wcd/wcd-include.sh
193 if [[ -e $_x ]]; then source $_x; fi
194
195
196 # * aliases
197
198 # very few aliases, functions are always preferred.
199
200 # ancient stuff.
201 if [[ $OS == Windows_NT ]]; then
202 alias ffs='cygstart "/c/Program Files (x86)/Mozilla Firefox/firefox.exe" -P scratch'
203 export DISPLAY=nt
204 alias j='command cygpath'
205 alias t='command cygstart'
206 alias cygstart='echo be quick, use the alias "t" instead :\)'
207 alias cygpath='echo be quick, use the alias "j" instead :\)'
208 fi
209
210
211
212 # keep this in mind? good for safety.
213 # alias cp='cp -i'
214 # alias mv='mv -i'
215
216
217 # remove any default aliases for these
218 unalias ls ll grep &>/dev/null ||:
219
220
221
222 # * functions
223
224
225 ..() { c ..; }
226 ...() { c ../..; }
227 ....() { c ../../..; }
228 .....() { c ../../../..; }
229 ......() { c ../../../../..; }
230
231
232 # file cut copy and paste, like the text buffers :)
233 # I havnt tested these.
234 _fbufferinit() { # internal use by
235 ! [[ $my_f_tempdir ]] && my_f_tempdir=$(mktemp -d)
236 rm -rf "$my_f_tempdir"/*
237 }
238 fcp() { # file cp
239 _fbufferinit
240 cp "$@" "$my_f_tempdir"/
241 }
242 fct() { # file cut
243 _fbufferinit
244 mv "$@" "$my_f_tempdir"/
245 }
246 fpst() { # file paste
247 [[ $2 ]] && { echo too many arguments; return 1; }
248 target=${1:-.}
249 cp "$my_f_tempdir"/* "$target"
250 }
251
252
253 # todo, update this
254 complete -F _longopt la lower low rlt rld rl lld ts ll dircp ex fcp fct fpst gr
255
256
257 _cdiff-prep() {
258 # join options which are continued to multiples lines onto one line
259 local first=true
260 grep -vE '^([ \t]*#|^[ \t]*$)' "$1" | while IFS= read -r line; do
261 # remove leading spaces/tabs. assumes extglob
262 if [[ $line == "[ ]*" ]]; then
263 line="${line##+( )}"
264 fi
265 if $first; then
266 pastline="$line"
267 first=false
268 elif [[ $line == *=* ]]; then
269 echo "$pastline" >> "$2"
270 pastline="$line"
271 else
272 pastline="$pastline $line"
273 fi
274 done
275 echo "$pastline" >> "$2"
276 }
277
278 _khfix_common() {
279 local host=${1##*@}
280 local ip port
281 read -r ip port < <(timeout 1 ssh -oBatchMode=yes -oControlMaster=no -oControlPath=/ -v $1 |& sed -rn "s/debug1: Connecting to $host \[([^\]*)] port ([0-9]+).*/\1 \2/p")
282 if [[ ! $ip ]]; then
283 echo "khfix: ssh failed"
284 return 1
285 fi
286 if [[ $port != 22 ]]; then
287 ip_entry="[$ip]:$port"
288 host_entry="[$host]:$port"
289 else
290 ip_entry=$ip
291 host_entry=$host
292 fi
293 ssh-keygen -R "$host_entry" -f $(readlink -f ~/.ssh/known_hosts)
294 echo "khfix: removing key for $ip_entry"
295 ssh-keygen -R "$ip_entry" -f $(readlink -f ~/.ssh/known_hosts)
296 }
297 khfix() { # known hosts fix
298 _khfix_common "$@" || return 1
299 ssh $1 :
300 }
301 khcopy() {
302 _khfix_common "$@"
303 ssh-copy-id $1
304 }
305
306 a() {
307 local x=$(readlink -nf "$@")
308 # yes, its kinda dumb that xclip/xsel cant do this in one invocation
309 echo -n "$x" | xclip -selection clipboard
310 echo -n "$x" | xclip
311 }
312
313 ack() { ack-grep "$@"; }
314
315 anki() {
316 if which anki &>/dev/null; then
317 command anki
318 else
319 schroot -c anki -- anki
320 fi
321 }
322
323 astudio() {
324 # googling android emulator libGL error: failed to load driver: r600
325 # lead to http://stackoverflow.com/a/36625175/14456
326 export ANDROID_EMULATOR_USE_SYSTEM_LIBS=1
327 /a/opt/android-studio/bin/studio.sh "$@" &r;
328 }
329
330 b() {
331 # backwards
332 c -
333 }
334
335 bkrun() {
336 # use -p from interactive shell
337 btrbk-run -p "$@"
338 }
339
340 bfg() { java -jar /a/opt/bfg-1.12.14.jar "$@"; }
341
342 bigclock() {
343 xclock -digital -update 1 -face 'arial black-80:bold'
344 }
345
346 bpush() {
347 case $HOSTNAME in
348 tp) target=x2 ;;
349 x2) target=tp ;;
350 esac
351 btrbk-run -t $target && switch-mail-host $HOSTNAME $target
352 }
353 bpull() {
354 case $HOSTNAME in
355 tp) source=x2 ;;
356 x2) source=tp ;;
357 esac
358 btrbk-run -s $source && switch-mail-host $source $HOSTNAME
359 }
360
361 btc() {
362 local f=/etc/bitcoin/bitcoin.conf
363 # importprivkey will timeout if using the default of 15 mins.
364 # upped it to 1 hour.
365 bitcoin-cli -rpcclienttimeout=60000 -$(s grep rpcuser= $f) -$(s grep rpcpassword= $f) "$@"
366 }
367
368 btcusd() { # $1 btc in usd
369 local price
370 price="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
371 printf "$%s\n" "$price"
372 if [[ $1 ]]; then
373 printf "$%.2f\n" "$(echo "scale=4; $price * $1"| bc -l)"
374 fi
375 }
376 usdbtc() { # $1 usd in btc
377 local price
378 price="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
379 printf "$%s\n" "$price"
380 if [[ $1 ]]; then
381 # 100 mil satoshi / btc. 8 digits after the 1.
382 printf "%.8f btc\n" "$(echo "scale=10; $1 / $price "| bc -l)"
383 fi
384 }
385 satoshi() { # $1 satoshi in usd
386 local price
387 price="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
388 price=$(echo "scale=10; $price * 0.00000001"| bc -l)
389 printf "$%f\n" "$price"
390 if [[ $1 ]]; then
391 printf "$%.2f\n" "$(echo "scale=10; $price * $1"| bc -l)"
392 fi
393 }
394
395
396 # c. better cd
397 if [[ $RLC_INSIDE_EMACS ]]; then
398 c() { wcd -c -z 50 -o "$@"; }
399 else
400 # lets see what the fancy terminal does from time to time
401 c() { wcd -c -z 50 "$@"; }
402 fi
403
404 caa() { git commit --amend --no-edit -a; }
405
406 caf() {
407 find -L $1 -type f -not \( -name .svn -prune -o -name .git -prune \
408 -o -name .hg -prune -o -name .editor-backups -prune \
409 -o -name .undo-tree-history -prune \) \
410 -exec bash -lc 'hr; echo "$1"; hr; cat "$1"' _ {} \; 2>/dev/null
411
412 }
413
414 calc() { echo "scale=3; $*" | bc -l; }
415 # no having to type quotes, but also no command history:
416 clc() {
417 local x
418 read -r x
419 echo "scale=3; $x" | bc -l
420 }
421
422 cam() {
423 git commit -am "$*"
424 }
425
426 cbfstool () { /a/opt/coreboot/build/cbfstool "$@"; }
427
428 ccat () { # config cat. see a config without extra lines.
429 grep '^\s*[^;[:space:]#]' "$@"
430 }
431
432 cdiff() {
433 # diff config files,
434 # setup for format of postfix, eg:
435 # option = stuff[,]
436 # [more stuff]
437 local pastline
438 local unified="$(mktemp)"
439 local f1="$(mktemp)"
440 local f2="$(mktemp)"
441 _cdiff-prep "$1" "$f1"
442 _cdiff-prep "$2" "$f2"
443 cat "$f1" "$f2" | grep -Po '^[^=]+=' | sort | uniq > "$unified"
444 while IFS= read -r line; do
445 # the default bright red / blue doesnt work in emacs shell
446 dwdiff -cblue,red -A best -d " ," <(grep "^$line" "$f1" || echo ) <(grep "^$line" "$f2" || echo ) | colordiff
447 done < "$unified"
448 }
449
450 cgpl()
451 {
452 if (($#)); then
453 cp /a/bin/data/COPYING "$@"
454 else
455 cp /a/bin/data/COPYING .
456 fi
457 }
458
459 capache()
460 {
461 if (($#)); then
462 cp /a/bin/data/LICENSE "$@"
463 else
464 cp /a/bin/data/LICENSE .
465 fi
466 }
467
468 cat-new-files() {
469 local start=$SECONDS
470 local dir="$1"
471 inotifywait -m "$dir" -e create -e moved_to |
472 while read filedir _ file; do
473 cat "$filedir$file"
474 hr
475 calc $((SECONDS - start)) / 60
476 sleep 5
477 done
478
479 }
480
481 chown() {
482 # makes it so chown -R symlink affects the symlink and its target.
483 if [[ $1 == -R ]]; then
484 shift
485 command chown -h "$@"
486 command chown -R "$@"
487 else
488 command chown "$@"
489 fi
490 }
491
492 cim() {
493 git commit -m "$*"
494 }
495
496 cl() {
497 # choose recent directory. cl = cd list
498 c =
499 }
500
501 chrome() {
502 if type -p chromium &>/dev/null; then
503 cmd=chromium
504 else
505 cd
506 cmd="schroot -c stretch chromium"
507 CHROMIUM_FLAGS='--enable-remote-extensions' $cmd &r
508 fi
509 }
510
511 d() { builtin bg; }
512 complete -A stopped -P '"%' -S '"' d
513
514 dat() { # do all tee, for more complex scripts
515 tee >(ssh frodo bash -l) >(bash -l) >(ssh x2 bash -l) >(ssh tp bash -l)
516 }
517 da() { # do all
518 local host
519 "$@"
520 for host in x2 tp kd; do
521 ssh $host "$@"
522 done
523 }
524
525 dc() {
526 diff --strip-trailing-cr -w "$@" # diff content
527 }
528
529 debian_pick_mirror () {
530 # netselect-apt finds a fast mirror.
531 # but we need to replace the mirrors ourselves,
532 # because it doesnt do that. best it can do is
533 # output a basic sources file
534 # here we get the server it found, get the main server we use
535 # then substitute all instances of one for the other in the sources file
536 # and backup original to /etc/apt/sources.list-original.
537 # this is idempotent. the only way to identify debian sources is to
538 # note the original server, so we put it in a comment so we can
539 # identify it later.
540 local file=$(mktemp -d)/f # safe way to get file name without creating one
541 sudo netselect-apt -o "$file" || return 1
542 url=$(grep ^\\w $file | head -n1 | awk '{print $2}')
543 sudo cp -f /etc/apt/sources.list /etc/apt/sources.list-original
544 sudo sed -ri "/http.us.debian.org/ s@( *[^ #]+ +)[^ ]+([^#]+).*@\1$url\2# http.us.debian.org@" /etc/apt/sources.list
545 sudo apt-get update
546 }
547
548 despace() {
549 local x y
550 for x in "$@"; do
551 y="${x// /_}"
552 safe_rename "$x" "$y"
553 done
554 }
555
556 dt() {
557 date "+%A, %B %d, %r" "$@"
558 }
559
560 dus() { # du, sorted, default arg of
561 du -sh ${@:-*} | sort -h
562 }
563
564
565
566 e() { echo "$@"; }
567
568
569 ediff() {
570 [[ ${#@} == 2 ]] || { echo "error: ediff requires 2 arguments"; return 1; }
571 emacs --eval "(ediff-files \"$1\" \"$2\")"
572 }
573
574
575 envload() { # load environment from a previous: export > file
576 local file=${1:-$HOME/.${USER}_env}
577 eval "$(export | sed 's/^declare -x/export -n/')"
578 while IFS= read -r line; do
579 # declare -x makes variables local to a function
580 eval ${line/#declare -x/export}
581 done < "$file"
582 }
583
584 # mail related
585 etail() {
586 sudo tail -f /var/log/exim4/mainlog
587 }
588
589 f() {
590 # cd forward
591 c +
592 }
593
594 fa() {
595 # find array. make an array of file names found by find into $x
596 # argument: find arguments
597 # return: find results in an array $x
598 while read -rd ''; do
599 x+=("$REPLY");
600 done < <(find "$@" -print0);
601 }
602
603 faf() { # find all files
604 find -L $1 -not \( -name .svn -prune -o -name .git -prune \
605 -o -name .hg -prune -o -name .editor-backups -prune \
606 -o -name .undo-tree-history -prune \) 2>/dev/null
607 }
608
609 # one that comes with distros is too old for newer devices
610 fastboot() {
611 /a/opt/android-platform-tools/fastboot "$@";
612 }
613
614 kdecd() { /usr/lib/x86_64-linux-gnu/libexec/kdeconnectd; }
615
616 # List of apps to install/update
617 # Create from existing manually installed apps by doing
618 # fdroidcl update
619 # fdroidcl search -i, then manually removing
620 # automatically installed/preinstalled apps
621
622 #
623 # # my attempt at recovering from boot loop:
624 # # in that case, boot to recovery (volume up, home button, power, let go of power after samsun logo)
625 # # then
626 # mount /dev/block/mmcblk0p12 /data
627 # cd /data
628 # find -iname '*appname*'
629 # rm -rf FOUND_DIRS
630 # usually good enough to just rm -rf /data/app/APPNAME
631 #
632 # currently broken:
633 # # causes replicant to crash
634 # org.quantumbadger.redreader
635 # org.kde.kdeconnect_tp
636
637 # not broke, but wont work without gps
638 #com.zoffcc.applications.zanavi
639 # not broke, but not using atm
640 #com.nutomic.syncthingandroid
641 # # doesn\'t work on replicant
642 #net.sourceforge.opencamera
643 #
644 fdroid_pkgs=(
645 de.marmaro.krt.ffupdater
646 me.ccrama.redditslide
647 org.fedorahosted.freeotp
648 at.bitfire.davdroid
649 com.alaskalinuxuser.justnotes
650 com.artifex.mupdf.viewer.app
651 com.danielkim.soundrecorder
652 com.fsck.k9
653 com.ghostsq.commander
654 com.ichi2.anki
655 com.jmstudios.redmoon
656 com.jmstudios.chibe
657 org.kde.kdeconnect_tp
658 com.notecryptpro
659 com.termux
660 cz.martykan.forecastie
661 de.danoeh.antennapod
662 de.blinkt.openvpn
663 de.marmaro.krt.ffupdater
664 eu.siacs.conversations
665 free.rm.skytube.oss
666 im.vector.alpha # riot
667 info.papdt.blackblub
668 me.tripsit.tripmobile
669 net.gaast.giggity
670 net.minetest.minetest
671 net.osmand.plus
672 org.isoron.uhabits
673 org.linphone
674 org.gnu.icecat
675 org.smssecure.smssecure
676 org.yaaic
677 sh.ftp.rocketninelabs.meditationassistant.opensource
678 )
679 # https://forum.xda-developers.com/android/software-hacking/wip-selinux-capable-superuser-t3216394
680 # for maru,
681 #me.phh.superuser
682
683 fdup() {
684 local -A installed updated
685 local p
686 fdroidcl update
687 if fdroidcl search -u | grep ^org.fdroid.fdroid; then
688 fdroidcl upgrade org.fdroid.fdroid
689 sleep 5
690 fdroidcl update
691 fi
692 for p in $(fdroidcl search -i| grep -o "^\S\+"); do
693 installed[$p]=true
694 done
695 for p in $(fdroidcl search -u| grep -o "^\S\+"); do
696 updated[$p]=false
697 done
698 for p in ${fdroid_pkgs[@]}; do
699 if ! ${installed[$p]:-false}; then
700 fdroidcl install $p
701 # sleeps are just me being paranoid since replicant has a history of crashing when certain apps are installed
702 sleep 5
703 fi
704 done
705 for p in ${!installed[@]}; do
706 if ! ${updated[$p]:-true}; then
707 fdroidcl upgrade $p
708 sleep 5
709 fi
710 done
711 }
712
713 firefox-default-profile() {
714 key=Default value=1 section=$1
715 file=/p/c/subdir_files/.mozilla/firefox/profiles.ini
716 sed -ri "/^ *$key/d" "$file"
717 sed -ri "/ *\[$section\]/,/^ *\[[^]]+\]/{/^\s*$key[[:space:]=]/d};/ *\[$section\]/a $key=$value" "$file"
718 }
719 fdhome() { #firefox default home profile
720 firefox-default-profile Profile0
721 }
722
723 fdwork() {
724 firefox-default-profile Profile4
725 }
726
727 ff() {
728 if type -P firefox &>/dev/null; then
729 firefox "$@"
730 else
731 iceweasel "$@"
732 fi
733 }
734
735
736
737 fn() {
738 firefox -P alt "$@" >/dev/null 2>&1
739 }
740
741
742 fsdiff () {
743 local missing=false
744 local dname="${PWD##*/}"
745 local m="/a/tmp/$dname-missing"
746 local d="/a/tmp/$dname-diff"
747 [[ -e $d ]] && rm "$d"
748 [[ -e $m ]] && rm "$m"
749 local msize=0
750 local fsfile
751 while read -r line; do
752 fsfile="$1${line#.}"
753 if [[ -e "$fsfile" ]]; then
754 md5diff "$line" "$fsfile" && tee -a "/a/tmp/$dname-diff" <<< "$fsfile $line"
755 else
756 missing=true
757 echo "$line" >> "$m"
758 msize=$((msize + 1))
759 fi
760 done < <(find -type f )
761 if $missing; then
762 echo "$m"
763 (( msize <= 100 )) && cat $m
764 fi
765 }
766 fsdiff-test() {
767 # expected output, with different tmp dirs
768 # /tmp/tmp.HDPbwMqdC9/c/d ./c/d
769 # /a/tmp/tmp.qLDkYxBYPM-missing
770 # ./b
771 cd $(mktemp -d)
772 echo ok > a
773 echo nok > b
774 mkdir c
775 echo ok > c/d
776 local x=$(mktemp -d)
777 mkdir $x/c
778 echo different > $x/c/d
779 echo ok > $x/a
780 fsdiff $x
781 }
782 rename-test() {
783 # test whether missing files were renamed, generally for use with fsdiff
784 # $1 = fsdiff output file, $2 = directory to compare to. pwd = fsdiff dir
785 # echos non-renamed files
786 local x y found
787 unset sums
788 for x in "$2"/*; do
789 { sums+=( "$(md5sum < "$x")" ) ; } 2>/dev/null
790 done
791 while read -r line; do
792 { missing_sum=$(md5sum < "$line") ; } 2>/dev/null
793 renamed=false
794 for x in "${sums[@]}"; do
795 if [[ $missing_sum == "$x" ]]; then
796 renamed=true
797 break
798 fi
799 done
800 $renamed || echo "$line"
801 done < "$1"
802 return 0
803 }
804
805 feh() {
806 # F = fullscren, z = random, Z = auto zoom
807 command feh -FzZ "$@"
808 }
809
810 # mail related
811 frozen() {
812 rm -rf /tmp/frozen
813 s mailq |gr frozen|awk '{print $3}' | while read -r id; do
814 s exim -Mvl $id
815 echo
816 s exim -Mvh $id
817 echo
818 s exim -Mvb $id
819 echo -e '\n\n##############################\n'
820 done | tee -a /tmp/frozen
821 }
822 frozenrm() {
823 local ids=()
824 while read -r line; do
825 printf '%s\n' "$line"
826 ids+=($(printf '%s\n' "$line" |gr frozen|awk '{print $3}'))
827 done < <(s mailq)
828 echo "sleeping for 2 in case you change your mind"
829 sleep 2
830 s exim -Mrm "${ids[@]}"
831 }
832
833 funce() {
834 # like -e for functions. returns on error.
835 # at the end of the function, disable with:
836 # trap ERR
837 trap 'echo "${BASH_COMMAND:+BASH_COMMAND=\"$BASH_COMMAND\" }
838 ${FUNCNAME:+FUNCNAME=\"$FUNCNAME\" }${LINENO:+LINENO=\"$LINENO\" }\$?=$?"
839 trap ERR
840 return' ERR
841 }
842
843
844 fw() {
845 firefox -P default "$@" >/dev/null 2>&1
846 }
847
848 getdir () {
849 local help="Usage: getdir [--help] PATH
850 Output the directory of PATH, or just PATH if it is a directory."
851 if [[ $1 == --help ]]; then
852 echo "$help"
853 return 0
854 fi
855 if [[ $# -ne 1 ]]; then
856 echo "getdir error: expected 1 argument, got $#"
857 return 1
858 fi
859 if [[ -d $1 ]]; then
860 echo "$1"
861 else
862 local dir="$(dirname "$1")"
863 if [[ -d $dir ]]; then
864 echo "$dir"
865 else
866 echo "getdir error: directory does not exist"
867 return 1
868 fi
869 fi
870 }
871
872 git_empty_branch() { # start an empty git branch. carefull, it deletes untracked files.
873 [[ $# == 1 ]] || { echo 'need a branch name!'; return 1;}
874 local gitroot
875 gitroot || return 1 # function to set gitroot
876 builtin cd "$gitroot"
877 git symbolic-ref HEAD refs/heads/$1
878 rm .git/index
879 git clean -fdx
880 }
881
882 gitroot() {
883 local help="Usage: gitroot [--help]
884 Print the full path to the root of the current git repo
885
886 Handles being within a .git directory, unlike git rev-parse --show-toplevel,
887 and works in older versions of git which did not have that."
888 if [[ $1 == --help ]]; then
889 echo "$help"
890 return
891 fi
892 local p=$(git rev-parse --git-dir) || { echo "error: not in a git repo" ; return 1; }
893 [[ $p != /* ]] && p=$PWD
894 echo "${p%%/.git}"
895 }
896
897 gitian() {
898 git config user.email ian@iankelling.org
899 }
900
901 gh() {
902 # i got an error, gh not found when doing a pull request, it seems like it wants itself in it\'s path.
903 local _oldpath="$PATH"
904 PATH="$PATH:~/node_modules/.bin"
905 command gh "$@"
906 PATH="$_oldpath"
907 }
908
909 gmacs() {
910 # quit will prompt if the program crashes.
911 gdb -ex=r -ex=quit --args emacs "$@"; r;
912 }
913
914 gdkill() {
915 # kill the emacs daemon
916 pk1 emacs --daemon
917 }
918
919 # at least in flidas, things rely on gpg being gpg1
920 gpg() {
921 command gpg2 "$@"
922 }
923
924 gse() {
925 git send-email --notes '--envelope-sender=<ian@iankelling.org>' \
926 --suppress-cc=self "$@"
927 }
928
929 gr() {
930 grep -iIP --color=auto "$@"
931 }
932
933 grr() { # grep recursive
934 if [[ ${#@} == 1 ]]; then
935 grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@" .
936 else
937 grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@"
938 fi
939 }
940
941 hstatus() {
942 # do git status on published repos
943 cd /a/bin/githtml
944 do_hr=false
945 for x in *; do
946 cd `readlink -f $x`/..
947 status=$(i status -s) || pwd
948 if [[ $status ]]; then
949 hr
950 echo $x
951 printf "%s\n" "$status"
952 fi
953 cd /a/bin/githtml
954 done
955 }
956
957 hl() { # history limit. Write extra history to archive file.
958 # todo: this is not working or not used currently
959 local max_lines linecount tempfile prune_lines x
960 local harchive="${HISTFILE}_archive"
961 for x in "$HISTFILE" "$harchive"; do
962 [[ -e $x ]] || { touch "$x" && echo "notice from hl(): creating $x"; }
963 if [[ ! $x || ! -e $x || ! -w $x || $(stat -c "%u" "$x") != $EUID ]]; then
964 echo "error in hl: history file \$x:$x no good"
965 return 1
966 fi
967 done
968 history -a # save history
969 max_lines=$HISTFILELINES
970 [[ $max_lines =~ ^[0-9]+$ ]] || { echo "error in hl: failed to get max line count"; return 1; }
971 linecount=$(wc -l < $HISTFILE) # pipe so it doesnt output a filename
972 [[ $linecount =~ ^[0-9]+$ ]] || { echo "error in hl: wc failed"; return 1; }
973 if (($linecount > $max_lines)); then
974 prune_lines=$(($linecount - $max_lines))
975 head -n $prune_lines "$HISTFILE" >> "$harchive" \
976 && sed --follow-symlinks -ie "1,${prune_lines}d" $HISTFILE
977 fi
978 }
979
980 hr() { # horizontal row. used to break up output
981 printf "$(tput setaf 5)â–ˆ$(tput sgr0)%.0s" $(seq ${COLUMNS:-60})
982 echo
983 }
984
985 hrcat() { local f; for f; do [[ -f $f ]] || continue; hr; echo "$f"; cat "$f"; done }
986
987 # get latest hub and run it
988 # main command to use:
989 # hub pull-request
990 # on first use, you input username/pass and it gets an oath token so you dont have to repeat
991 # it\'s at ~/.config/hub
992 hub() {
993 local up uptar updir p
994 p=/github/hub/releases/
995 up=https://github.com/$(curl -s https://github.com$p| grep -o $p'download/[^/]*/hub-linux-amd64[^"]*' | head -n1)
996 uptar=${up##*/}
997 updir=${uptar%.tgz}
998 if [[ ! -e /a/opt/$updir ]]; then
999 rm -rf /a/opt/hub-linux-amd64*
1000 wget -P /a/opt $up
1001 tar -C /a/opt -zxf /a/opt/$uptar
1002 rm -f /a/opt/$uptar
1003 s /a/opt/$updir/install
1004 fi
1005
1006 # save token across computers
1007 if [[ ! -L ~/.config/hub ]]; then
1008 if [[ -e ~/.config/hub ]]; then
1009 mv ~/.config/hub /p/c/subdir_files/.config/
1010 fi
1011 if [[ -e /p/c/subdir_files/.config/hub ]]; then
1012 conflink
1013 fi
1014 fi
1015 command hub "$@"
1016 }
1017
1018 i() { git "$@"; }
1019 # modified from ~/local/bin/git-completion.bash
1020 # other completion commands are mostly taken from bash_completion package
1021 complete -o bashdefault -o default -o nospace -F _git i 2>/dev/null \
1022 || complete -o default -o nospace -F _git i
1023
1024 if ! type service &>/dev/null; then
1025 service() {
1026 echo actually running: systemctl $2 $1
1027 systemctl $2 $1
1028 }
1029 fi
1030
1031 ic() {
1032 # fast commit all
1033 git commit -am "$*"
1034 }
1035
1036
1037 idea() {
1038 /a/opt/idea-IC-163.7743.44/bin/idea.sh "$@" &r
1039 }
1040
1041 ifn() {
1042 # insensitive find
1043 find -L . -not \( -name .svn -prune -o -name .git -prune \
1044 -o -name .hg -prune -o -name .editor-backups -prune \
1045 -o -name .undo-tree-history -prune \) -iname "*$**" 2>/dev/null
1046 }
1047
1048
1049 if [[ $OS == Windows_NT ]]; then
1050 # cygstart wrapper
1051 cs() {
1052 cygstart "$@" &
1053 }
1054 xp() {
1055 explorer.exe .
1056 }
1057 # launch
1058 o() {
1059 local x=(*$1*)
1060 (( ${#x[#]} > 1 )) && { echo "warning ${#x[#]} matches found"; sleep 1; }
1061 cygstart *$1* &
1062 }
1063 else
1064 o() {
1065 if type gvfs-open &> /dev/null ; then
1066 gvfs-open "$@"
1067 else
1068 xdg-open "$@"
1069 fi
1070 # another alternative is run-mailcap
1071 }
1072 fi
1073
1074 ipdrop() {
1075 s iptables -A INPUT -s $1 -j DROP
1076 }
1077
1078 net-dev-info() {
1079 e "lspci -nnk|gr -iA2 net"
1080 lspci -nnk|gr -iA2 net
1081 hr
1082 e "s lshw -C network"
1083 hr
1084 s lshw -C network
1085
1086 }
1087
1088 istext() {
1089 grep -Il "" "$@" &>/dev/null
1090 }
1091
1092 jtail() {
1093 journalctl -n 10000 -f "$@" | grep -Evi "^(\S+\s+){4}(sudo|sshd|cron)"
1094 }
1095
1096 kff() { # keyboardio firmware flash
1097 pushd /a/bin/distro-setup/Arduino/Model01-Firmware
1098 yes $'\n' | make flash
1099 popd
1100 }
1101
1102 l() {
1103 if [[ $PWD == /[iap] ]]; then
1104 command ls -A --color=auto -I lost+found "$@"
1105 else
1106 command ls -A --color=auto "$@"
1107 fi
1108 }
1109
1110
1111 lcn() { locate -i "*$**"; }
1112
1113 lg() { LC_COLLATE=C.UTF-8 ll --group-directories-first; }
1114
1115 lt() { ll -tr "$@"; }
1116
1117 lld() { ll -d "$@"; }
1118
1119 lom() {
1120 local l base
1121 if [[ $1 == /* ]]; then
1122 l=$(sudo losetup -f)
1123 sudo losetup $l $1
1124 base=${1##*/}
1125 if ! sudo cryptsetup luksOpen $l $base; then
1126 sudo losetup -d $l
1127 return 1
1128 fi
1129 sudo mkdir -p /mnt/$base
1130 sudo mount /dev/mapper/$base /mnt/$base
1131 sudo chown $USER:$USER /mnt/$base
1132 else
1133 base=$1
1134 sudo umount /mnt/$base
1135 l=$(sudo cryptsetup status /dev/mapper/$base|sed -rn 's/^\s*device:\s*(.*)/\1/p')
1136 sudo cryptsetup luksClose /dev/mapper/$base
1137 sudo losetup -d $l
1138 fi
1139 }
1140
1141 low() { # make filenames lowercase, remove bad chars
1142 local f new
1143 for f in "$@"; do
1144 new="${f,,}" # downcase
1145 new="${new//[^[:alnum:]._-]/_}" # sub bad chars
1146 new="${new#"${new%%[[:alnum:]]*}"}" # remove leading/trailing non-alnum
1147 new="${new%"${new##*[[:alnum:]]}"}"
1148 # remove bad underscores, like __ and _._
1149 new=$(echo $new | sed -r 's/__+/_/g;s/_+([.-])|([.-])_+/\1/g')
1150 safe_rename "$f" "$new" || return 1
1151 done
1152 return 0
1153 }
1154
1155 lower() { # make first letter of filenames lowercase.
1156 local x
1157 for x in "$@"; do
1158 if [[ ${x::1} == [A-Z] ]]; then
1159 y=$(tr "[A-Z]" "[a-z]" <<<"${x::1}")"${x:1}"
1160 safe_rename "$x" "$y" || return 1
1161 fi
1162 done
1163 }
1164
1165
1166 k() { # history search
1167 grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history} | tail -n 80;
1168 }
1169
1170 ks() { # history search
1171 grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history} | uniq;
1172 }
1173
1174
1175 make-targets() {
1176 # show make targets, via http://stackoverflow.com/questions/3063507/list-goals-targets-in-gnu-make
1177 make -qp | awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ {split($1,A,/ /);for(i in A)print A[i]}'
1178 }
1179
1180 mbenable() {
1181 mb=$1
1182 dst=/m/4e/$1
1183 src=/m/md/$1
1184 set -x
1185 [[ -e $src ]] || { set +x; return 1; }
1186 mv -T $src $dst || { set +x; return 1; }
1187 ln -s -T $dst $src
1188 /a/exe/lnf /p/.mu ~
1189 mu index --maildir=/m/4e
1190 set +x
1191 }
1192 mbdisable() {
1193 mb=$1
1194 dst=/m/md/$1
1195 src=/m/4e/$1
1196 set -x
1197 [[ -e $src ]] || { set +x; return 1; }
1198 if [[ -L $dst ]]; then rm $dst; fi
1199 mv -T $src $dst
1200 set +x
1201 }
1202
1203
1204 mdt() {
1205 markdown "$1" >/tmp/mdtest.html
1206 firefox /tmp/mdtest.html
1207 }
1208
1209
1210 mkc() {
1211 mkdir "$1"
1212 c "$1"
1213 }
1214
1215 mkct() {
1216 mkc `mktemp -d`
1217 }
1218
1219 mkt() { # mkdir and touch file
1220 local path="$1"
1221 mkdir -p "$(dirname "$path")"
1222 touch "$path"
1223 }
1224
1225 mkdir() { command mkdir -p "$@"; }
1226
1227 mo() { xset dpms force off; } # monitor off
1228
1229
1230 nopanic() {
1231 sudo tee -a /var/log/exim4/paniclog-archive </var/log/exim4/paniclog; sudo truncate -s0 /var/log/exim4/paniclog
1232 }
1233
1234
1235 otp() {
1236 oathtool --totp -b "$@" | xclip -selection clipboard
1237 }
1238
1239 p8() { ping 8.8.8.8; }
1240
1241 pakaraoke() {
1242 # from http://askubuntu.com/questions/456021/remove-vocals-from-mp3-and-get-only-instrumentals
1243 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
1244 }
1245
1246
1247 pfind() { #find *$1* in $PATH
1248 [[ $# != 1 ]] && { echo requires 1 argument; return 1; }
1249 local pathArray
1250 IFS=: pathArray=($PATH); unset IFS
1251 find "${pathArray[@]}" -iname "*$1*"
1252 }
1253
1254 pk1() {
1255 local pid
1256 pid=($(pgrep -f "$*"))
1257 case ${#pid[@]} in
1258 1)
1259 ps -F $pid
1260 m kill $pid
1261 ;;
1262 0) echo "no pid found" ;;
1263 *)
1264 ps -F ${pid[@]}
1265 ;;
1266 esac
1267 }
1268
1269 pick-trash() {
1270 # trash-restore lists everything that has been trashed at or below CWD
1271 # This picks out files just in CWD, not subdirectories,
1272 # which also match grep $1, usually use $1 for a time string
1273 # which you get from running restore-trash once first
1274 local name x ask
1275 local nth=1
1276 # last condition is to not ask again for ones we skipped
1277 while name="$( echo | restore-trash | gr "$PWD/[^/]\+$" | gr "$1" )" \
1278 && [[ $name ]] && (( $(wc -l <<<"$name") >= nth )); do
1279 name="$(echo "$name" | head -n $nth | tail -n 1 )"
1280 read -p "$name [Y/n] " ask
1281 if [[ ! $ask || $ask == [Yy] ]]; then
1282 x=$( echo "$name" | gr -o "^\s*[0-9]*" )
1283 echo $x | restore-trash > /dev/null
1284 elif [[ $ask == [Nn] ]]; then
1285 nth=$((nth+1))
1286 else
1287 return
1288 fi
1289 done
1290 }
1291
1292
1293 pub() {
1294 rld /a/h/_site/ li:/var/www/iankelling.org/html
1295 }
1296
1297 pubip() { curl -4s https://icanhazip.com; }
1298 pubip6() { curl -6s https://icanhazip.com; }
1299 whatismyip() { pubip; }
1300
1301 pumpa() {
1302 # fixes the menu bar in xmonad. this won\'t be needed when xmonad
1303 # packages catches up on some changes in future (this is written in
1304 # 4/2017)
1305 #
1306 # geekosaur: so youll want to upgrade to xmonad 0.13 or else use a
1307 # locally modified XMonad.Hooks.ManageDocks that doesnt set the
1308 # work area; turns out it\'s impossible to set correctly if you are
1309 # not a fully EWMH compliant desktop environment
1310 #
1311 # geekosaur: chrome shows one failure mode, qt/kde another, other
1312 # gtk apps a third, ... I came up with a setting that works for me
1313 # locally but apparently doesnt work for others, so we joined the
1314 # other tiling window managers in giving up on setting it at all
1315 #
1316 xprop -root -remove _NET_WORKAREA
1317 command pumpa &r
1318 }
1319
1320
1321 pwgen() {
1322 # -m = min length
1323 # -x = max length
1324 # -t = print pronunciation
1325 apg -m 14 -x 17 -t
1326 for (( i=0; i<10; i++ )); do
1327 shuf -n3 /usr/share/hunspell/en_US.dic | sed 's,/.*,,' | paste -sd . -
1328
1329 done
1330 }
1331
1332 pwlong() {
1333 # -M CLN = use Caps, Lowercase, Numbers
1334 # -n 1 = 1 password
1335 # -a 1 = use random instead of pronounceable algorithm
1336 apg -m 50 -x 70 -n 1 -a 1 -M CLN
1337 }
1338
1339
1340 q() { # start / launch a program in the backround and redir output to null
1341 "$@" &> /dev/null &
1342 }
1343
1344 r() {
1345 exit "$@" 2>/dev/null
1346 }
1347
1348 rbpipe() { rbt post -o --diff-filename=- "$@"; }
1349 rbp() { rbt post -o "$@"; }
1350
1351 rl() {
1352 # rsync, root is required to keep permissions right.
1353 # rsync --archive --human-readable --verbose --itemize-changes --checksum \(-ahvic\) \
1354 # --no-times --delete
1355 # basically, make an exact copy, use checksums instead of file times to be more accurate
1356 rsync -ahvic --delete "$@"
1357 }
1358 rld() {
1359 # like rlu, but dont delete files on the target end which
1360 # do not exist on the original end.
1361 rsync -ahvic "$@"
1362 }
1363 complete -F _rsync -o nospace rld rl rlt
1364
1365 rlt() {
1366 # rl without preserving modification time.
1367 rsync -ahvic --delete --no-t "$@"
1368 }
1369
1370 rlu() { # [OPTS] HOST PATH
1371 # eg. rlu -opts frodo /testpath
1372 # relative paths will expanded with readlink -f.
1373 # useful for selectively sending dirs which have been synced with unison,
1374 # where the path is the same on both hosts.
1375 opts=("${@:1:$#-2}") # 1 to last -2
1376 path="${@:$#}" # last
1377 host="${@:$#-1:1}" # last -1
1378 if [[ $path == .* ]]; then
1379 path=$(readlink -f $path)
1380 fi
1381 # rync here uses checksum instead of time so we dont mess with
1382 # unison relying on time as much. g is for group, same reason
1383 # to keep up with unison.
1384 s rsync -rlpchviog --relative "${opts[@]}" "$path" "root@$host:/";
1385 }
1386
1387 # only run on MAIL_HOST. simpler to keep this on one system.
1388 r2eadd() { # usage: name url
1389 # initial setup of rss2email:
1390 # r2e new r2e@iankelling.org
1391 # that initializes files, and sets default email.
1392 # symlink to the config doesnt work, so I copied it to /p/c
1393 # and then use cli option to specify explicit path.
1394 # Only option changed from default config is to set
1395 # force-from = True
1396 #
1397 # or else for a few feeds, the from address is set by the feed, and
1398 # if I fail delivery, then I send a bounce message to that from
1399 # address, which makes me be a spammer.
1400
1401 r2e add $1 "$2" $1@r2e.iankelling.org
1402 # get up to date and dont send old entries now:
1403 r2e run --no-send $1
1404 }
1405 r2e() { command r2e -d /p/c/rss2email.json -c /p/c/rss2email.cfg "$@"; }
1406
1407 rspicy() { # usage: HOST DOMAIN
1408 # connect to spice vm remote host. use vspicy for local host
1409 local port=$(ssh $1<<EOF
1410 sudo virsh dumpxml $2|grep "<graphics.*type='spice'" | \
1411 sed -rn "s/.*port='([0-9]+).*/\1/p"
1412 EOF
1413 )
1414 if [[ $port ]]; then
1415 spicy -h $1 -p $port
1416 else
1417 echo "error: no port found. check that the domain is running."
1418 fi
1419 }
1420
1421 rmstrips() {
1422 ssh fencepost head -n 300 /gd/gnuorg/EventAndTravelInfo/rms-current-trips.txt | less
1423 }
1424
1425 s() {
1426 # background
1427 # I use a function because otherwise we cant use in a script,
1428 # cant assign to variable.
1429 #
1430 # note: gksudo is recommended for X apps because it does not set the
1431 # home directory to the same, and thus apps writing to ~ fuck things up
1432 # with root owned files.
1433 #
1434 if [[ $EUID != 0 || $1 == -* ]]; then
1435 SUDOD="$PWD" sudo -i "$@"
1436 else
1437 "$@"
1438 fi
1439 }
1440
1441 safe_rename() { # warn and dont rename if file exists.
1442 # mv -n exists, but it\'s silent
1443 if [[ $# != 2 ]]; then
1444 echo safe_rename error: $# args, need 2 >2
1445 return 1
1446 fi
1447 if [[ $1 != $2 ]]; then # yes, we want to silently ignore this
1448 if [[ -e $2 || -L $2 ]]; then
1449 echo "Cannot rename $1 to $2 as it already exists."
1450 else
1451 mv -vi "$1" "$2"
1452 fi
1453 fi
1454 }
1455
1456
1457 sb() { # sudo bash -c
1458 # use sb instead of s is for sudo redirections,
1459 # eg. sb 'echo "ok fine" > /etc/file'
1460 local SUDOD="$PWD"
1461 sudo -i bash -c "$@"
1462 }
1463 complete -F _root_command s sb
1464
1465 scssl() {
1466 # s gem install scss-lint
1467 pushd /a/opt/thoughtbot-guides
1468 git pull --stat
1469 popd
1470 scss-lint -c /a/opt/thoughtbot-guides/style/sass/.scss-lint.yml "$@"
1471 }
1472
1473 ser() {
1474 local s; [[ $EUID != 0 ]] && s=sudo
1475 if type -p systemctl &>/dev/null; then
1476 $s systemctl $1 $2
1477 else
1478 $s service $2 $1
1479 fi
1480 }
1481 # like restart, but do nothing if its not already started
1482 srestart() {
1483 local service=$1
1484 if [[ $(s systemctl --no-pager show -p ActiveState $service ) == ActiveState=active ]]; then
1485 systemctl restart $service
1486 fi
1487 }
1488 serstopnm() {
1489 ser stop NetworkManager
1490 ser stop dnsmasq
1491 s resolvconf -d NetworkManager
1492 ser start dnsmasq
1493 }
1494
1495 setini() { # set a value in a .ini style file
1496 key="$1" value="$2" section="$3" file="$4"
1497 if [[ -s $file ]]; then
1498 sed -ri -f - "$file" <<EOF
1499 # remove existing keys
1500 / *\[$section\]/,/^ *\[[^]]+\]/{/^\s*$key[[:space:]=]/d}
1501 # add key
1502 /^\s*\[$section\]/a $key=$value
1503 # from section to eof, do nothing
1504 /^\s*\[$section\]/,\$b
1505 # on the last line, if we haven't found section yet, add section and key
1506 \$a [$section]\\
1507 $key=$value
1508 EOF
1509 else
1510 cat >"$file" <<EOF
1511 [$section]
1512 $key=$value
1513 EOF
1514 fi
1515 }
1516
1517 sgo() { # service go
1518 service=$1
1519 ser restart $service || return 1
1520 if type -p systemctl &>/dev/null; then
1521 ser enable $service
1522 fi
1523 }
1524
1525
1526 shellck() {
1527 # 2086 = unquoted $var
1528 # 2046 = unquoted $(cmd)
1529 # i had -x as an arg, but debian testing(stretch) doesn\'t support it
1530 shellcheck -e 2086,2046,2068,2006,2119 "$@"
1531 }
1532
1533 skaraoke() {
1534 local tmp out
1535 in="$1"
1536 out=${2:-${1%.*}.sh}
1537 tmp=$(mktemp -d)
1538 script -t -c "mpv --no-config --no-resume-playback --no-terminal --no-audio-display '$1'" $tmp/typescript 2>$tmp/timing
1539 # todo, the current sleep seems pretty good, but it
1540 # would be nice to have an empirical measurement, or
1541 # some better wait to sync up.
1542 #
1543 # note: --loop-file=no prevents it from hanging if you have that
1544 # set to inf the mpv config.
1545 # --loop=no prevents it from exit code 3 due to stdin if you
1546 # had it set to inf in mpv config.
1547 #
1548 # args go to mpv, for example --volume=80, 50%
1549 cat >$out <<EOFOUTER
1550 #!/bin/bash
1551 trap "trap - TERM && kill 0" INT TERM ERR; set -e
1552 ( sleep .2; scriptreplay <( cat <<'EOF'
1553 $(cat $tmp/timing)
1554 EOF
1555 ) <( cat <<'EOF'
1556 $(cat $tmp/typescript)
1557 EOF
1558 ))&
1559 base64 -d - <<'EOF'| mpv --loop=no --loop-file=no --no-terminal --no-audio-display "\$@" -
1560 $(base64 "$1")
1561 EOF
1562 kill 0
1563 EOFOUTER
1564 rm -r $tmp
1565 chmod +x $out
1566 }
1567
1568 slog() {
1569 # log with script. timing is $1.t and script is $1.s
1570 # -l to save to ~/typescripts/
1571 # -t to add a timestamp to the filenames
1572 local logdir do_stamp arg_base
1573 (( $# >= 1 )) || { echo "arguments wrong"; return 1; }
1574 logdir="/a/dt/"
1575 do_stamp=false
1576 while getopts "lt" option
1577 do
1578 case $option in
1579 l ) arg_base=$logdir ;;
1580 t ) do_stamp=true ;;
1581 esac
1582 done
1583 shift $(($OPTIND - 1))
1584 arg_base+=$1
1585 [[ -e $logdir ]] || mkdir -p $logdir
1586 $do_stamp && arg_base+=$(date +%F.%T%z)
1587 script -t $arg_base.s 2> $arg_base.t
1588 }
1589 splay() { # script replay
1590 #logRoot="$HOME/typescripts/"
1591 #scriptreplay "$logRoot$1.t" "$logRoot$1.s"
1592 scriptreplay "$1.t" "$1.s"
1593 }
1594
1595 smeld() { # usage host1 host2 file
1596 meld <(ssh $1 cat $3) <(ssh $2 cat $3)
1597 }
1598
1599 spd() {
1600 PATH=/usr/local/spdhackfix:$PATH command spd "$@"
1601 }
1602
1603 spend() {
1604 s systemctl suspend
1605 }
1606
1607 sr() {
1608 # sudo redo. be aware, this command may not work right on strange distros or earlier software
1609 if [[ $# == 0 ]]; then
1610 sudo -E bash -c -l "$(history -p '!!')"
1611 else
1612 echo this command redos last history item. no argument is accepted
1613 fi
1614 }
1615
1616 srm () {
1617 # with -ll, less secure but faster.
1618 command srm -ll "$@"
1619 }
1620
1621 srun() {
1622 scp $2 $1:/tmp
1623 ssh $1 /tmp/${2##*/} "${@:2}"
1624 }
1625
1626 sss() { # ssh solo
1627 ssh -oControlMaster=no -oControlPath=/ "$@"
1628 }
1629 ssk() {
1630 local -a opts=()
1631 while [[ $1 == -* ]]; do
1632 opts+=("$1")
1633 shift
1634 done
1635 m pkill -f "^ssh: /tmp/ssh_mux_${USER}_${1#*@}_22_"
1636 m ssh "${opts[@]}" "$@"
1637 }
1638
1639 swap() {
1640 local tmp
1641 tmp=$(mktemp)
1642 mv $1 $tmp
1643 mv $2 $1
1644 mv $tmp $2
1645 }
1646
1647 t() {
1648 local x
1649 local -a args
1650 if type -t trash-put >/dev/null; then
1651 # skip args that dont exist, or else trash-put will have an error
1652 for x in "$@"; do
1653 if [[ -e $x || -L $x ]]; then
1654 args+=("$x")
1655 fi
1656 done
1657 [[ ! ${args[@]} ]] || trash-put "${args[@]}"
1658 else
1659 rm -rf "$@"
1660 fi
1661 }
1662
1663
1664 tclock() {
1665 clear
1666 date +%l:%_M
1667 len=60
1668 # this goes to full width
1669 #len=${1:-$((COLUMNS -7))}
1670 x=1
1671 while true; do
1672 if (( x == len )); then
1673 end=true
1674 d="$(date +%l:%_M) "
1675 else
1676 end=false
1677 d=$(date +%l:%M:%_S)
1678 fi
1679 echo -en "\r"
1680 echo -n "$d"
1681 for ((i=0; i<x; i++)); do
1682 if (( i % 6 )); then
1683 echo -n _
1684 else
1685 echo -n .
1686 fi
1687 done
1688 if $end; then
1689 echo
1690 x=1
1691 else
1692 x=$((x+1))
1693 fi
1694 sleep 5
1695 done
1696 }
1697
1698
1699 te() {
1700 # test existence / exists
1701 local ret=0
1702 for x in "$@"; do
1703 [[ -e "$x" || -L "$x" ]] || ret=1
1704 done
1705 return $ret
1706 }
1707
1708 # mail related
1709 testmail() {
1710 declare -gi _seq; _seq+=1
1711 echo "test body" | m mail -s "test mail from $HOSTNAME, $_seq" "${@:-root@localhost}"
1712 # for testing to send from an external address, you can do for example
1713 # -fian@iank.bid -aFrom:ian@iank.bid web-6fnbs@mail-tester.com
1714 # note in exim, you can retry a deferred message
1715 # s exim -M MSG_ID
1716 # MSG_ID is in /var/log/exim4/mainlog, looks like 1ccdnD-0001nh-EN
1717 }
1718
1719 # to test sieve, use below command. for fsf mail, see fsf-get-mail script.
1720 # make modifications, then copy to live file, use -eW to actually modify mailbox
1721 # cp /p/c/subdir_files/sieve/personal{test,}.sieve; testsievelist -eW INBOX
1722 #
1723 # Another option is to use sieve-test SCRIPT MAIL_FILE. note,
1724 # sieve-test doesnt know about envelopes, Im not sure if sieve-filter does.
1725
1726 # sieve with output filter. arg is mailbox, like INBOX.
1727 # This depends on dovecot conf, notably mail_location in /etc/dovecot/conf.d/10-mail.conf
1728
1729 testsievelist() {
1730 sieve-filter ~/sieve/maintest.sieve "$@" >/tmp/testsieve.log 2> >(tail) && sed -rn '/^Performed actions:/{n;n;p}' /tmp/testsieve.log | sort -u
1731 }
1732
1733
1734 # mail related
1735 # plain sieve
1736 testsieve() {
1737 sieve-filter ~/sieve/main.sieve "$@"
1738 }
1739
1740 # mail related
1741 testexim() {
1742 # testmail above calls sendmail, which is a link to exim/postfix.
1743 # its docs dont say a way of adding an argument
1744 # to sendmail to turn on debug output. We could make a wrapper, but
1745 # that is a pain. Exim debug args are documented here:
1746 # http://www.exim.org/exim-html-current/doc/html/spec_html/ch-the_exim_command_line.html
1747 #
1748 # http://www.exim.org/exim-html-current/doc/html/spec_html/ch-building_and_installing_exim.html
1749 # note, for exim daemon, you can turn on debug options by
1750 # adding -d, etc to COMMONOPTIONS in
1751 # /etc/default/exim4
1752 exim -d -t <<'EOF'
1753 From: root@gnu.org
1754 To: ian@iankelling.org
1755 Subject: Testing Exim
1756
1757 This is a test message.
1758 EOF
1759 }
1760
1761 # toggle keyboard
1762 tk() {
1763 # based on
1764 # https://askubuntu.com/questions/160945/is-there-a-way-to-disable-a-laptops-internal-keyboard
1765 id=$(xinput --list --id-only 'AT Translated Set 2 keyboard')
1766 if xinput list | grep -F '∼ AT Translated Set 2 keyboard' &>/dev/null; then
1767 echo enabling keyboard
1768 # find the first slave keyboard number, they are all the same in my output.
1769 # if they werent, worst case we would need to save the slave number somewhere
1770 # when it got disabled.
1771 slave=$(xinput list | sed -n 's/.*slave \+keyboard (\([0-9]*\)).*/\1/p' | head -n1)
1772 xinput reattach $id $slave
1773 else
1774 xinput float $id
1775 fi
1776 }
1777
1778 tm() {
1779 # timer in minutes
1780 # --no-config
1781 (sleep $(calc "$@ * 60") && mpv --no-config --volume 50 /a/bin/data/alarm.mp3) > /dev/null 2>&1 &
1782 }
1783
1784 trg() { transmission-remote-gtk&r; }
1785 trc() {
1786 # example, set global upload limit to 100 kilobytes:
1787 # trc -u 100
1788 TR_AUTH=":$(jq -r .profiles[0].password ~/.config/transmission-remote-gtk/config.json)" transmission-remote transmission.lan -ne "$@"
1789 }
1790
1791
1792 tu() {
1793 local s;
1794 local dir="$(dirname "$1")"
1795 if [[ -e $1 && ! -w $1 || ! -w $(dirname "$1") ]]; then
1796 s=s;
1797 fi
1798 $s teeu "$@"
1799 }
1800
1801 tx() { # toggle set -x, and the prompt so it doesnt spam
1802 if [[ $- == *x* ]]; then
1803 set +x
1804 PROMPT_COMMAND=prompt-command
1805 # disabled due to issue on stretch, running ll we get error. something
1806 # about the DEBUG trap is broken
1807 # if [[ $TERM == *(screen*|xterm*|rxvt*) ]]; then
1808 # trap 'settitle "$BASH_COMMAND"' DEBUG
1809 # fi
1810 else
1811 # normally, i would just execute these commands in the function.
1812 # however, DEBUG is not inherited, so we need to run it outside a function.
1813 # And we want to run set -x afterwards to avoid spam, so we cram everything
1814 # in here, and then it will run after this function is done.
1815 #PROMPT_COMMAND='trap DEBUG; unset PROMPT_COMMAND; PS1="\w \$ "; set -x'
1816
1817 unset PROMPT_COMMAND
1818 PS1="\w \$ "
1819 set -x
1820 fi
1821 }
1822
1823 psnetns() {
1824 # show all processes in the network namespace $1.
1825 # blank entries appear to be subprocesses/threads
1826 local x netns
1827 netns=$1
1828 ps -w | head -n 1
1829 s find -L /proc/[1-9]*/task/*/ns/net -samefile /run/netns/$netns | cut -d/ -f5 | \
1830 while read l; do
1831 x=$(ps -w --no-headers -p $l);
1832 if [[ $x ]]; then echo "$x"; else echo $l; fi;
1833 done
1834 }
1835
1836 m() { printf "%s\n" "$*"; "$@"; }
1837
1838
1839 vpncmd() {
1840 #m s nsenter -t $(pgrep -f "/usr/sbin/openvpn .* --config /etc/openvpn/.*pia.conf") -n -m "$@"
1841 m s nsenter -t $(pgrep -f "/usr/sbin/openvpn .* --config /etc/openvpn/.*client.conf") -n -m "$@"
1842 }
1843 vpnf() {
1844 vpncmd gksudo -u iank "firefox -no-remote -P vpn" &r
1845 }
1846 vpni() {
1847 vpncmd gksudo -u iank "$*"
1848 }
1849 vpnbash() {
1850 vpncmd bash
1851 }
1852
1853
1854
1855 virshrm() {
1856 for x in "$@"; do virsh destroy "$x"; virsh undefine "$x"; done
1857 }
1858
1859 vm-set-listen(){
1860 local t=$(mktemp)
1861 local vm=$1
1862 local ip=$2
1863 s virsh dumpxml $vm | sed -r "s/(<listen.*address=')([^']+)/\1$ip/" | \
1864 sed -r "s/listen='[^']+/listen='$ip/"> $t
1865 s virsh undefine $vm
1866 s virsh define $t
1867 }
1868
1869
1870 vmshare() {
1871 vm-set-listen $1 0.0.0.0
1872 }
1873
1874
1875 vmunshare() {
1876 vm-set-listen $1 127.0.0.1
1877 }
1878
1879
1880 vpn() {
1881 if [[ -e /lib/systemd/system/openvpn-client@.service ]]; then
1882 local vpn_service=openvpn-client
1883 else
1884 local vpn_service=openvpn
1885 fi
1886
1887 [[ $1 ]] || { echo need arg; return 1; }
1888 journalctl --unit=$vpn_service@$1 -f -n0 &
1889 s systemctl start $vpn_service@$1
1890 # sometimes the ask-password agent does not work and needs a delay.
1891 sleep .5
1892 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=779240
1893 # noticed around 8-2017 after update from around stretch release
1894 # on debian testing, even though the bug is much older.
1895 s systemd-tty-ask-password-agent
1896 }
1897
1898 vpnoff() {
1899 [[ $1 ]] || { echo need arg; return 1; }
1900 if [[ -e /lib/systemd/system/openvpn-client@.service ]]; then
1901 local vpn_service=openvpn-client
1902 else
1903 local vpn_service=openvpn
1904 fi
1905 s systemctl stop $vpn_service@$1
1906 }
1907
1908
1909 vrm() {
1910 virsh destroy $1
1911 virsh undefine $1
1912 }
1913
1914
1915
1916 vspicy() { # usage: VIRSH_DOMAIN
1917 # connect to vms made with virt-install
1918 spicy -p $(sudo virsh dumpxml "$1"|grep "<graphics.*type='spice'"|\
1919 sed -r "s/.*port='([0-9]+).*/\1/")
1920 }
1921
1922 wian() {
1923 cat-new-files /m/4e/INBOX/new
1924 }
1925
1926 wtr() { curl wttr.in/boston; }
1927
1928 xl() {
1929 if pgrep gnome-screensav &>/dev/null; then
1930 # this command actually starts gnome-screensaver if it isn\'t running.
1931 # lololol, what crap
1932 gnome-screensaver-command --exit &>/dev/null
1933 fi
1934 mate-screensaver-command --exit &>/dev/null
1935 if ! pidof xscreensaver; then
1936 pushd /
1937 xscreensaver &
1938 popd
1939 # 1 was not long enough
1940 sleep 3
1941 fi
1942 xscreensaver-command -activate
1943 }
1944
1945 # * misc stuff
1946
1947 # from curl cheat.sh/:bash_completion
1948 _cheatsh_complete_curl()
1949 {
1950 local cur prev opts
1951 _get_comp_words_by_ref -n : cur
1952
1953 COMPREPLY=()
1954 #cur="${COMP_WORDS[COMP_CWORD]}"
1955 prev="${COMP_WORDS[COMP_CWORD-1]}"
1956 opts="$(curl -s cheat.sh/:list | sed s@^@cheat.sh/@)"
1957
1958 if [[ ${cur} == cheat.sh/* ]] ; then
1959 COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
1960 __ltrim_colon_completions "$cur"
1961 return 0
1962 fi
1963 }
1964 complete -F _cheatsh_complete_curl curl
1965
1966
1967 if [[ $- == *i* ]]; then
1968 # commands to run when bash exits normally
1969 trap "hl" EXIT
1970 fi
1971
1972
1973 # temporary variables to test colorization
1974 # some copied from gentoo /etc/bash/bashrc,
1975 use_color=false
1976 # dircolors --print-database uses its own built-in database
1977 # instead of using /etc/DIR_COLORS. Try to use the external file
1978 # first to take advantage of user additions.
1979 safe_term=${TERM//[^[:alnum:]]/?} # sanitize TERM
1980 match_lhs=""
1981 [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
1982 [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
1983 [[ -z ${match_lhs} ]] \
1984 && type -P dircolors >/dev/null \
1985 && match_lhs=$(dircolors --print-database)
1986 # test if our $TERM is in the TERM values in dircolor
1987 [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true
1988
1989
1990 if ${use_color} && [[ $- == *i* ]]; then
1991
1992 if [[ $XTERM_VERSION == Cygwin* ]]; then
1993 get_term_color() {
1994 for x in "$@"; do
1995 case $x in
1996 underl) echo -n $'\E[4m' ;;
1997 bold) echo -n $'\E[1m' ;;
1998 red) echo -n $'\E[31m' ;;
1999 green) echo -n $'\E[32m' ;;
2000 blue) echo -n $'\E[34m' ;;
2001 cyan) echo -n $'\E[36m' ;;
2002 yellow) echo -n $'\E[33m' ;;
2003 purple) echo -n $'\E[35m' ;;
2004 nocolor) echo -n $'\E(B\E[m' ;;
2005 esac
2006 done
2007 }
2008
2009 else
2010 get_term_color() {
2011 for x in "$@"; do
2012 case $x in
2013 underl) echo -n $(tput smul) ;;
2014 bold) echo -n $(tput bold) ;;
2015 red) echo -n $(tput setaf 1) ;;
2016 green) echo -n $(tput setaf 2) ;;
2017 blue) echo -n $(tput setaf 4) ;;
2018 cyan) echo -n $(tput setaf 6) ;;
2019 yellow) echo -n $(tput setaf 3) ;;
2020 purple) echo -n $(tput setaf 5) ;;
2021 nocolor) echo -n $(tput sgr0) ;; # no font attributes
2022 esac
2023 done
2024 }
2025 fi
2026 else
2027 get_term_color() {
2028 :
2029 }
2030 fi
2031 # Try to keep environment pollution down, EPA loves us.
2032 unset safe_term match_lhs use_color
2033
2034
2035
2036 # * prompt
2037
2038
2039 if [[ $- == *i* ]]; then
2040 # git branch/status prompt function
2041 if [[ $OS != Windows_NT ]]; then
2042 GIT_PS1_SHOWDIRTYSTATE=true
2043 fi
2044 # arch source lopip show -fcation
2045 [[ -r /usr/share/git/git-prompt.sh ]] && source /usr/share/git/git-prompt.sh
2046 # fedora/debian source
2047 [[ -r /usr/share/git-core/contrib/completion/git-prompt.sh ]] && source /usr/share/git-core/contrib/completion/git-prompt.sh
2048
2049 # in case we didnt source git-prompt.sh
2050 if ! declare -f __git_ps1 > /dev/null; then
2051 __git_ps1() {
2052 :
2053 }
2054 fi
2055
2056 # this needs to come before next ps1 stuff
2057 # this stuff needs bash 4, feb 2009,
2058 # old enough to no longer condition on $BASH_VERSION anymore
2059 shopt -s autocd
2060 shopt -s dirspell
2061 PS1='\w'
2062 if [[ $- == *i* ]] && [[ ! $RLC_INSIDE_EMACS ]]; then
2063 PROMPT_DIRTRIM=2
2064 bind -m vi-command B:shell-backward-word
2065 bind -m vi-command W:shell-forward-word
2066 fi
2067
2068 if [[ $SSH_CLIENT ]]; then
2069 PS1="\h $PS1"
2070 fi
2071
2072
2073
2074
2075 prompt-command() {
2076 local return=$? # this MUST COME FIRST
2077 local psc pst ps_char ps_color stale_subvol
2078 unset IFS
2079 history -a # save history
2080
2081
2082
2083 case $return in
2084 0) ps_color="$(get_term_color blue)"
2085 ps_char='\$'
2086 ;;
2087 1) ps_color="$(get_term_color green)"
2088 ps_char="$return \\$"
2089 ;;
2090 *) ps_color="$(get_term_color yellow)"
2091 ps_char="$return \\$"
2092 ;;
2093 esac
2094 if [[ ! -O . ]]; then # not owner
2095 if [[ -w . ]]; then # writable
2096 ps_color="$(get_term_color bold red)"
2097 else
2098 ps_color="$(get_term_color bold green)"
2099 fi
2100 fi
2101 # I would set nullglob, but bash has had bugs where that
2102 # doesnt work if not in top level.
2103 if [[ -e /nocow/btrfs-stale ]] && ((`command ls -AUq /nocow/btrfs-stale|wc -l`)); then
2104 ps_char="! $ps_char"
2105 fi
2106 PS1="${PS1%"${PS1#*[wW]}"} \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
2107 # emacs completion doesnt like the git prompt atm, so disabling it.
2108 #PS1="${PS1%"${PS1#*[wW]}"}$(__git_ps1 ' (%s)') \[$ps_color\]$ps_char\[$(get_term_color nocolor)\] "
2109 }
2110 PROMPT_COMMAND=prompt-command
2111
2112 settitle () {
2113 if [[ $TERM == screen* ]]; then
2114 local title_escape="\033]..2;"
2115 else
2116 local title_escape="\033]0;"
2117 fi
2118 if [[ $* != prompt-command ]]; then
2119 echo -ne "$title_escape$USER@$HOSTNAME ${PWD/#$HOME/~} "
2120 printf "%s" "$*"
2121 echo -ne "\007"
2122 fi
2123 }
2124
2125 # for titlebar.
2126 # condition from the screen man page i think.
2127 # note: duplicated in tx()
2128 # disabled. see note in tx
2129 # if [[ $TERM == *(screen*|xterm*|rxvt*) ]]; then
2130 # trap 'settitle "$BASH_COMMAND"' DEBUG
2131 # else
2132 # trap DEBUG
2133 # fi
2134
2135 fi
2136
2137 reset-konsole() {
2138 # we also have a file in /a/c/...konsole...
2139 local f=$HOME/.config/konsolerc
2140 setini DefaultProfile profileian.profile "Desktop Entry" $f
2141 setini Favorites profileian.profile "Favorite Profiles" $f
2142 setini ShowMenuBarByDefault false KonsoleWindow $f
2143 setini TabBarPosition Top TabBar $f
2144 }
2145
2146 reset-sakura() {
2147 while read k v; do
2148 setini $k $v sakura /a/c/subdir_files/.config/sakura/sakura.conf
2149 done <<'EOF'
2150 colorset1_back rgb(33,37,39
2151 less_questions true
2152 audible_bell No
2153 visible_bell No
2154 disable_numbered_tabswitch true
2155 scroll_lines 10000000
2156 scrollbar true
2157 EOF
2158 }
2159
2160 reset-xscreensaver() {
2161 # except for spash, i set these by setting gui options in
2162 # xscreensaver-command -demo
2163 # then finding the corresponding option in .xscreensaver
2164 # spash, i happened to notice in .xscreensaver
2165 cat > /home/iank/.xscreensaver <<'EOF'
2166 mode: blank
2167 dpmsEnabled: True
2168 dpmsStandby: 0:02:00
2169 dpmsSuspend: 0:02:00
2170 dpmsOff: 0:03:00
2171 timeout: 0:02:00
2172 lock: True
2173 lockTimeout: 0:03:00
2174 splash: False
2175 EOF
2176
2177 }
2178
2179
2180 # * stuff that makes sense to be at the end
2181 if [[ "$SUDOD" ]]; then
2182 cd "$SUDOD"
2183 unset SUDOD
2184 elif [[ -d /a ]] && [[ $PWD == $HOME ]] && [[ $- == *i* ]]; then
2185 cd /a
2186 fi
2187
2188
2189 # best practice
2190 unset IFS
2191
2192
2193 # if someone exported $SOE, catch errors
2194 if [[ $SOE ]]; then
2195 errcatch
2196 fi
2197
2198 # for mitmproxy to get a newer python.
2199 # commented until i want to use it because it
2200 # noticably slows bash startup
2201 #
2202 #if [[ $EUID != 0 && -e ~/.pyenv/bin ]]; then
2203 # export PATH="~/.pyenv/bin:$PATH"
2204 # eval "$(pyenv init -)"
2205 # eval "$(pyenv virtualenv-init -)"
2206 #fi
2207
2208
2209
2210 export GOPATH=$HOME/go
2211 path_add $GOPATH/bin
2212
2213 export ARDUINO_PATH=/a/opt/Arduino/build/linux/work
2214
2215 path_add --end ~/.npm-global
2216
2217 # taken from default changes to bashrc and bash_profile
2218 path_add --end $HOME/.rvm/bin
2219 [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
2220
2221 export BASEFILE_DIR=/a/bin/fai-basefiles
2222
2223 # didnt get drush working, if I did, this seems like the
2224 # only good thing to include for it.
2225 # Include Drush completion.
2226 # if [ -f "/home/ian/.drush/drush.complete.sh" ] ; then
2227 # source /home/ian/.drush/drush.complete.sh
2228 # fi
2229
2230
2231 # https://wiki.archlinux.org/index.php/Xinitrc#Autostart_X_at_login
2232 # i added an extra condition as gentoo xorg guide says depending on
2233 # $DISPLAY is fragile.
2234 if [[ ! $DISPLAY && $XDG_VTNR == 1 ]] && shopt -q login_shell && isarch; then
2235 exec startx
2236 fi
2237
2238
2239 # ensure no bad programs appending to this file will have an affect
2240 return 0