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