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