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