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