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