various updates
[distro-setup] / brc
diff --git a/brc b/brc
index d8cfd227b6e7315d62f3405b8ba3c849675a1b79..382b3627ded5e67a98c6c9fa71fbca675151e9b4 100644 (file)
--- a/brc
+++ b/brc
@@ -272,14 +272,16 @@ _khfix_common() {
     local h=${1##*@}
     local x
     ssh-keygen -R $h -f $(readlink -f ~/.ssh/known_hosts)
-    if ! x=$(timeout 0.1 ssh -oBatchMode=yes -v $1 |& sed -rn "s/debug1: Connecting to $h \[([^\]*)].*/\1/p"); then
+    x=$(timeout 1 ssh -oBatchMode=yes -oControlMaster=no -oControlPath=/ -v $1 |& sed -rn "s/debug1: Connecting to $h \[([^\]*)].*/\1/p")
+    if [[ ! $x ]]; then
         echo "khfix: ssh failed"
         return 1
     fi
+    echo "khfix: removing key for $x"
     ssh-keygen -R $x -f $(readlink -f ~/.ssh/known_hosts)
 }
 khfix() { # known hosts fix
-    _khfix_common "$@"
+    _khfix_common "$@" || return 1
     ssh $1 :
 }
 khcopy() {
@@ -378,7 +380,7 @@ cam() {
 }
 
 ccat () { # config cat. see a config without extra lines.
-    grep '^\s*[^[:space:]#]' "$@"
+    grep '^\s*[^;[:space:]#]' "$@"
 }
 
 cdiff() {
@@ -488,7 +490,7 @@ dt() {
     date "+%A, %B %d, %r" "$@"
 }
 
-dus() {
+dus() { # du, sorted, default arg of
     du -sh ${@:-*} | sort -h
 }
 
@@ -512,6 +514,11 @@ envload() { # load environment from a previous: export > file
     done < "$file"
 }
 
+# mail related
+etail() {
+    sudo tail -f /var/log/exim4/mainlog
+}
+
 f() {
     # cd forward
     c +
@@ -534,6 +541,90 @@ faf() { # find all files
 
 fastboot() { /a/opt/androidsdk/platform-tools/fastboot "$@"; }
 
+
+# List of apps to install/update
+# Create from existing manually installed apps by doing
+# fdroidcl search -i, then manually removing
+# automatically installed/preinstalled apps
+
+# firefox updater. commented out, firefox depends on nonfree opengl.
+# de.marmaro.krt.ffupdater
+# # causes replicant to die on install and go into a boot loop
+# me.ccrama.redditslide
+#
+# # my attempt at recovering from boot loop:
+# # in that case, boot to recovery (volume up, home button, power, let go of power after samsun logo)
+# # then
+# mount /dev/block/mmcblk0p12 /data
+# cd /data
+# find -iname '*appname*'
+# rm -rf FOUND_DIRS
+# usually good enough to just rm -rf /data/app/APPNAME
+#
+# currently broken:
+#at.bitfire.davdroid
+fdroid_pkgs=(
+    com.alaskalinuxuser.justnotes
+    com.artifex.mupdfdemo
+    com.fsck.k9
+    com.ichi2.anki
+    com.jmstudios.redmoon
+    com.notecryptpro
+    com.nutomic.syncthingandroid
+    com.termux
+    com.zoffcc.applications.zanavi
+    cz.martykan.forecastie
+    de.danoeh.antennapod
+    im.vector.alpha # riot
+    info.papdt.blackblub
+    me.tripsit.tripmobile
+    net.gaast.giggity
+    net.osmand.plus
+    net.sourceforge.opencamera
+    org.dmfs.tasks # caldav tasks thing
+    org.fdroid.fdroid
+    org.isoron.uhabits
+    org.kde.kdeconnect_tp
+    org.quantumbadger.redreader
+    org.smssecure.smssecure
+    org.fedorahosted.freeotp
+)
+# https://forum.xda-developers.com/android/software-hacking/wip-selinux-capable-superuser-t3216394
+# for maru,
+#me.phh.superuser
+
+fdup() {
+    local -A installed updated
+    local p
+    fdroidcl update
+    for p in $(fdroidcl search -i| grep -o "^\S\+"); do
+        installed[$p]=true
+    done
+    for p in $(fdroidcl search -u| grep -o "^\S\+"); do
+        updated[$p]=false
+    done
+    for p in ${fdroid_pkgs[@]}; do
+        ${installed[$p]:-false} || fdroidcl install $p
+    done
+    for p in ${!installed[@]}; do
+        ${updated[$p]:-true} || fdroidcl upgrade $p
+    done
+}
+
+firefox-default-profile() {
+    key=Default value=1 section=$1
+    file=/p/c/subdir_files/.mozilla/firefox/profiles.ini
+    sed -ri "/^ *$key/d" "$file"
+    sed -ri "/ *\[$section\]/,/^ *\[[^]]+\]/{/^\s*$key[[:space:]=]/d};/ *\[$section\]/a $key=$value" "$file"
+}
+fdhome() { #firefox default home profile
+    firefox-default-profile Profile0
+}
+
+fdwork() {
+    firefox-default-profile Profile4
+}
+
 ff() {
     if type -P firefox &>/dev/null; then
         firefox "$@"
@@ -617,6 +708,29 @@ feh() {
     command feh -FzZ "$@"
 }
 
+# mail related
+frozen() {
+    rm -rf /tmp/frozen
+    s mailq |gr frozen|awk '{print $3}' | while read -r id; do
+        s exim -Mvl $id
+        echo
+        s exim -Mvh $id
+        echo
+        s exim -Mvb $id
+        echo -e '\n\n##############################\n'
+    done | tee -a /tmp/frozen
+}
+frozenrm() {
+    local ids=()
+    while read -r line; do
+        printf '%s\n' "$line"
+        ids+=($(printf '%s\n' "$line" |gr frozen|awk '{print $3}'))
+    done < <(s mailq)
+    echo "sleeping for 2 in case you change your mind"
+    sleep 2
+    s exim -Mrm "${ids[@]}"
+}
+
 funce() {
     # like -e for functions. returns on error.
     # at the end of the function, disable with:
@@ -806,7 +920,15 @@ else
     }
 fi
 
+net-dev-info() {
+    e "lspci -nnk|gr -iA2 net"
+    lspci -nnk|gr -iA2 net
+    hr
+    e "s lshw -C network"
+    hr
+    s lshw -C network
 
+}
 
 istext() {
     grep -Il "" "$@" &>/dev/null
@@ -859,20 +981,56 @@ k() { # history search
     grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history}  | tail -n 80;
 }
 
+ks() { # history search
+    grep -P --binary-files=text "$@" ${HISTFILE:-~/.bash_history}  | uniq;
+}
+
 
 make-targets() {
     # show make targets, via http://stackoverflow.com/questions/3063507/list-goals-targets-in-gnu-make
     make -qp | awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ {split($1,A,/ /);for(i in A)print A[i]}'
 }
 
+mbenable() {
+    mb=$1
+    dst=/m/4e/$1
+    src=/m/md/$1
+    set -x
+    mv -T $src $dst || { set +x; return 1; }
+    ln -s -T $dst $src
+    /a/exe/lnf /p/.mu ~
+    mu index --maildir=/m/4e
+    set +x
+}
+mbdisable() {
+    mb=$1
+    dst=/m/md/$1
+    src=/m/4e/$1
+    set -x
+    if [[ -L $dst ]]; then rm $dst; fi
+    mv -T $src $dst
+    set +x
+}
+
 
 mkc() {
     mkdir "$1"
     c "$1"
 }
 
+mkt() { # mkdir and touch file
+    local path="$1"
+    mkdir -p "$(dirname "$path")"
+    touch "$path"
+}
+
 mkdir() { command mkdir -p "$@"; }
 
+otp() {
+    echo "oathtool --totp -b"
+    oathtool --totp -b "$@"
+}
+
 pithos() {
     cd /
     export PYTHONPATH=/a/opt/Pithosfly
@@ -930,13 +1088,7 @@ pick-trash() {
     done
 }
 
-postconfin() {
-    local MAPFILE
-    mapfile -t
-    local s
-    [[ $EUID == 0 ]] || s=s
-    $s postconf -ev "${MAPFILE[@]}"
-}
+ping8() { ping 8.8.8.8; }
 
 pub() {
     rld /a/h/_site/ li:/var/www/iankelling.org/html
@@ -945,6 +1097,25 @@ pub() {
 pubip() { curl -4s https://icanhazip.com; }
 whatismyip() { pubip; }
 
+pumpa() {
+    # fixes the menu bar in xmonad. this won\'t be needed when xmonad
+    # packages catches up on some changes in future (this is written in
+    # 4/2017)
+    #
+    # geekosaur: so you'll want to upgrade to xmonad 0.13 or else use a
+    # locally modified XMonad.Hooks.ManageDocks that doesn't set the
+    # work area; turns out it's impossible to set correctly if you are
+    # not a fully EWMH compliant desktop environment
+    #
+    # geekosaur: chrome shows one failure mode, qt/kde another, other
+    # gtk apps a third, ... I came up with a setting that works for me
+    # locally but apparently doesn't work for others, so we joined the
+    # other tiling window managers in giving up on setting it at all
+    #
+    xprop -root -remove _NET_WORKAREA
+    command pumpa &r
+}
+
 
 pwgen() {
     # -m = min length
@@ -1008,6 +1179,25 @@ rlu() { # [OPTS] HOST PATH
     s rsync -rlpchviog --relative "${opts[@]}" "$path" "root@$host:/";
 }
 
+# only run on desktop. simpler to keep this on one system.
+r2eadd() { # usage: name url
+    # initial setup of rss2email:
+    # r2e new r2e@iankelling.org
+    # that initializes files, and sets default email.
+    # symlink to the config doesn't work, so I copied it to /p/c
+    # and then use cli option to specify explicit path.
+    # Only option changed from default config is to set
+    # force-from = True
+    #
+    # or else for a few feeds, the from address is set by the feed, and
+    # if I fail delivery, then I send a bounce message to that from
+    # address, which makes me be a spammer.
+
+    r2e add $1 "$2" $1@r2e.iankelling.org
+    # get up to date and don't send old entries now:
+    r2e run --no-send $1
+}
+r2e() { command r2e -d /p/c/rss2email.json -c /p/c/rss2email.cfg "$@"; }
 
 rspicy() { # usage: HOST DOMAIN
     # connect to spice vm remote host. use vspicy for local host
@@ -1023,6 +1213,10 @@ EOF
     fi
 }
 
+rmstrips() {
+    ssh fencepost head -n 300 /gd/gnuorg/EventAndTravelInfo/rms-current-trips.txt
+}
+
 s() {
     # background
     # I use a function because otherwise we can't use in a script,
@@ -1096,6 +1290,40 @@ shellck() {
     shellcheck -e 2086,2046,2068,2006,2119 "$@"
 }
 
+skaraoke() {
+    local tmp out
+    in="$1"
+    out=${2:-${1%.*}.sh}
+    tmp=$(mktemp -d)
+    script -t -c "mpv --no-config --no-resume-playback --no-terminal --no-audio-display '$1'" $tmp/typescript 2>$tmp/timing
+    # todo, the current sleep seems pretty good, but it
+    # would be nice to have an empirical measurement, or
+    # some better wait to sync up.
+    #
+    # note: --loop-file=no prevents it from hanging if you have that
+    # set to inf the mpv config.
+    # --loop=no prevents it from exit code 3 due to stdin if you
+    # had it set to inf in mpv config.
+    #
+    # args go to mpv, for example --volume=80, 50%
+    cat >$out <<EOFOUTER
+#!/bin/bash
+trap "trap - TERM && kill 0" INT TERM ERR; set -e
+( sleep .2; scriptreplay <( cat <<'EOF'
+$(cat $tmp/timing)
+EOF
+) <( cat <<'EOF'
+$(cat $tmp/typescript)
+EOF
+))&
+base64 -d - <<'EOF'| mpv --loop=no --loop-file=no --no-terminal --no-audio-display "\$@" -
+$(base64 "$1")
+EOF
+kill 0
+EOFOUTER
+    rm -r $tmp
+    chmod +x $out
+}
 
 slog() {
     # log with script. timing is $1.t and script is $1.s
@@ -1124,6 +1352,10 @@ splay() { # script replay
     scriptreplay "$1.t" "$1.s"
 }
 
+spend() {
+    s systemctl suspend
+}
+
 sr() {
     # sudo redo. be aware, this command may not work right on strange distros or earlier software
     if [[ $# == 0 ]]; then
@@ -1143,6 +1375,10 @@ srun() {
     ssh $1 /tmp/${2##*/} "${@:2}"
 }
 
+sss() { # ssh solo
+    ssh -oControlMaster=no -oControlPath=/ "$@"
+}
+
 swap() {
     local tmp
     tmp=$(mktemp)
@@ -1162,9 +1398,9 @@ t() {
             fi
         done
         [[ ! ${args[@]} ]] || trash-put "${args[@]}"
-    else
-        rm -rf "$@"
-    fi
+      else
+          rm -rf "$@"
+fi
 }
 
 
@@ -1212,6 +1448,7 @@ te() {
     return $ret
 }
 
+# mail related
 testmail() {
     declare -gi _seq; _seq+=1
     echo "test body" | m mail -s "test mail from $HOSTNAME, $_seq" "${@:-root@localhost}"
@@ -1222,16 +1459,28 @@ testmail() {
     # MSG_ID is in /var/log/exim4/mainlog, looks like 1ccdnD-0001nh-EN
 }
 
-# use -eW to actually modify mailbox
+# to test sieve, use below command. for fsf mail, see fsf-get-mail script.
+# make modifications, then copy to live file, use -eW to actually modify mailbox
+# cp /p/c/subdir_files/sieve/personal{test,}.sieve; testsievelist -eW INBOX
+#
+# Another option is to use sieve-test SCRIPT MAIL_FILE. note,
+# sieve-test doesn't know about envelopes, I'm not sure if sieve-filter does.
+
+# sieve with output filter. arg is mailbox, like INBOX.
+# This depends on dovecot conf, notably mail_location in /etc/dovecot/conf.d/10-mail.conf
+
 testsievelist() {
-    sieve-filter ~/sieve/main.sieve "$@" >/a/tmp/slog 2> >(tail) && sed -rn '/^Performed actions:/{n;n;p}' /a/tmp/slog | sort -u
+    sieve-filter ~/sieve/maintest.sieve "$@" >/tmp/testsieve.log 2> >(tail) && sed -rn '/^Performed actions:/{n;n;p}' /tmp/testsieve.log | sort -u
 }
 
 
+# mail related
+# plain sieve
 testsieve() {
     sieve-filter ~/sieve/main.sieve "$@"
 }
 
+# mail related
 testexim() {
     # testmail above calls sendmail, which is a link to exim/postfix.
     # it's docs don't say a way of adding an argument
@@ -1239,8 +1488,8 @@ testexim() {
     # that is a pain. Exim debug args are documented here:
     # http://www.exim.org/exim-html-current/doc/html/spec_html/ch-the_exim_command_line.html
     #
-# http://www.exim.org/exim-html-current/doc/html/spec_html/ch-building_and_installing_exim.html
-# note, for exim daemon, you can turn on debug options by
+    # http://www.exim.org/exim-html-current/doc/html/spec_html/ch-building_and_installing_exim.html
+    # note, for exim daemon, you can turn on debug options by
     # adding -d, etc to COMMONOPTIONS in
     # /etc/default/exim4
     # for testing external mail, you need the to address as final cmdline arg
@@ -1259,6 +1508,13 @@ tm() {
     (sleep $(calc "$@ * 60") && mpv --no-config --volume 50 /a/bin/data/alarm.mp3) > /dev/null 2>&1 &
 }
 
+trg() { transmission-remote-gtk&r; }
+trc() {
+    # example, set global upload limit to 100 kilobytes:
+    # trc -u 100
+    TR_AUTH=":$(jq -r .profiles[0].password ~/.config/transmission-remote-gtk/config.json)" transmission-remote transmission.lan -ne "$@"
+}
+
 
 tu() {
     local s;
@@ -1270,14 +1526,14 @@ tu() {
 }
 
 tx() { # toggle set -x, and the prompt so it doesn't spam
-    if [[ $- == *x* ]]; then
-        set +x
-        PROMPT_COMMAND=prompt_command
-    else
-        unset PROMPT_COMMAND
-        PS1="\w \$ "
-        set -x
-    fi
+       if [[ $- == *x* ]]; then
+           set +x
+           PROMPT_COMMAND=prompt_command
+       else
+           unset PROMPT_COMMAND
+           PS1="\w \$ "
+           set -x
+       fi
 }
 
 psnetns() {
@@ -1300,29 +1556,13 @@ vpncmd() {
     m s nsenter -t $(pgrep -f "/usr/sbin/openvpn --suppress-timestamps --nobind --config /etc/openvpn/client/client.conf") -n -m "$@"
 }
 vpnf() {
-    vpncmd gksudo -u ian firefox &r
+    vpncmd gksudo -u ian "firefox -no-remote -P firefox-main-profile" &r
 }
 vpnbash() {
     vpncmd bash
 }
 
 
-trg() { transmission-remote-gtk&r; }
-
-# transmission() {
-#     local pid=$(cat /var/lib/transmission-daemon/transmission-daemon.pid)
-#     if [[ $pid && -e /proc/$pid ]]; then
-#         echo "noop. already running."
-#         return
-#     fi
-
-#     local NAME=transmission-daemon
-#     local DAEMON=/usr/bin/$NAME
-#     local duser=debian-transmission
-
-#     [ -e /etc/default/$NAME ] && . /etc/default/$NAME
-#     s ip netns exec vpn sudo -u $duser ionice -c 3 nice -n 19 $DAEMON $OPTIONS
-# }
 
 virshrm() {
     for x in "$@"; do virsh destroy "$x"; virsh undefine "$x"; done
@@ -1348,14 +1588,22 @@ vmunshare() {
     vm-set-listen $1 127.0.0.1
 }
 
+
 vpn() {
-    s systemctl start openvpn@client&
-    journalctl --unit=openvpn@client -f -n0
+    [[ $1 ]] || { echo need arg; return 1; }
+    journalctl --unit=openvpn-client@$1 -f -n0 &
+    s systemctl start openvpn-client@$1
+    # sometimes the ask-password agent does not work and needs a delay.
+    sleep .5
+    # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=779240
+    # noticed around 8-2017 after update from around stretch release
+    # on debian testing, even though the bug is much older.
+    s systemd-tty-ask-password-agent
 }
 
-
 vpnoff() {
-    s systemctl stop openvpn@client
+    [[ $1 ]] || { echo need arg; return 1; }
+    s systemctl stop openvpn-client@$1
 }
 
 
@@ -1373,13 +1621,45 @@ vspicy() { # usage: VIRSH_DOMAIN
 }
 
 
-whatismyip() { curl ipecho.net/plain ; echo; }
+wtr() { curl wttr.in/boston; }
 
+xl() {
+    # this succeeds even if gnome-screensaver isn\'t running.
+    gnome-screensaver-command --exit &>/dev/null
+    if ! pidof xscreensaver; then
+        pushd /
+        xscreensaver &
+        popd
+        # 1 was not long enough
+        sleep 3
+    fi
+    xscreensaver-command -lock
+}
 
 #############################
 ######### misc stuff ########
 #############################
 
+# from curl cheat.sh/:bash_completion
+_cheatsh_complete_curl()
+{
+    local cur prev opts
+    _get_comp_words_by_ref -n : cur
+
+    COMPREPLY=()
+    #cur="${COMP_WORDS[COMP_CWORD]}"
+    prev="${COMP_WORDS[COMP_CWORD-1]}"
+    opts="$(curl -s cheat.sh/:list | sed s@^@cheat.sh/@)"
+
+    if [[ ${cur} == cheat.sh/* ]] ; then
+       COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
+       __ltrim_colon_completions "$cur"
+        return 0
+    fi
+}
+complete -F _cheatsh_complete_curl curl
+
+
 if [[ $- == *i* ]]; then
     # commands to run when bash exits normally
     trap "hl" EXIT
@@ -1576,16 +1856,15 @@ fi
 # based on warning from rvmsudo
 export rvmsudo_secure_path=1
 
-# for other script I wrote
-#export ACME_TINY_PATH=/a/opt/acme-tiny
-export ACME_TINY_WRAPPER_CERT_DIR=/p/c/machine_specific/$HOSTNAME/webservercerts
-
 if [[ -s "/usr/local/rvm/scripts/rvm" ]]; then
     source "/usr/local/rvm/scripts/rvm"
 elif [[ -s $HOME/.rvm/scripts/rvm ]]; then
     source $HOME/.rvm/scripts/rvm
 fi
 
+export GOPATH=$HOME/go
+path_add $GOPATH/bin
+
 
 path_add --end ~/.npm-global