lots of fixes and improvements
[distro-setup] / brc
diff --git a/brc b/brc
index b163a093094e0689b9c3477b6683f48d79cddbe4..ca9a78682a4d3ee5dd53c98abb2c246894be0949 100644 (file)
--- a/brc
+++ b/brc
@@ -272,7 +272,7 @@ _khfix_common() {
     local h=${1##*@}
     local x
     ssh-keygen -R $h -f $(readlink -f ~/.ssh/known_hosts)
-    x=$(timeout 1 ssh -oBatchMode=yes -v $1 |& sed -rn "s/debug1: Connecting to $h \[([^\]*)].*/\1/p")
+    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
@@ -295,6 +295,14 @@ a() {
 
 ack() { ack-grep "$@"; }
 
+anki() {
+    if which anki &>/dev/null; then
+        command anki
+    else
+        schroot -c anki -- anki
+    fi
+}
+
 astudio() {
     # googling android emulator libGL error: failed to load driver: r600
     # lead to http://stackoverflow.com/a/36625175/14456
@@ -314,6 +322,10 @@ bkrun() {
 
 bfg() { java -jar /a/opt/bfg-1.12.14.jar "$@"; }
 
+bigclock() {
+    xclock -digital -update 1 -face 'arial black-80:bold'
+}
+
 btc() {
     local f=/etc/bitcoin/bitcoin.conf
     # importprivkey will timeout if using the default of 15 mins.
@@ -321,7 +333,7 @@ btc() {
     bitcoin-cli -rpcclienttimeout=60000 -$(s grep rpcuser= $f) -$(s grep rpcpassword= $f) "$@"
 }
 
-btcusd() {
+btcusd() { # $1 btc in usd
     local price
     price="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
     printf "$%s\n" "$price"
@@ -329,7 +341,7 @@ btcusd() {
         printf "$%.2f\n" "$(echo "scale=4; $price * $1"| bc -l)"
     fi
 }
-usdbtc() {
+usdbtc() { # $1 usd in btc
     local price
     price="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
     printf "$%s\n" "$price"
@@ -338,7 +350,7 @@ usdbtc() {
         printf "%.8f btc\n" "$(echo "scale=10; $1 / $price "| bc -l)"
     fi
 }
-satoshi() {
+satoshi() { # $1 satoshi in usd
     local price
     price="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
     price=$(echo "scale=10; $price * 0.00000001"| bc -l)
@@ -380,7 +392,7 @@ cam() {
 }
 
 ccat () { # config cat. see a config without extra lines.
-    grep '^\s*[^[:space:]#]' "$@"
+    grep '^\s*[^;[:space:]#]' "$@"
 }
 
 cdiff() {
@@ -438,7 +450,13 @@ cl() {
 }
 
 chrome() {
-    CHROMIUM_FLAGS='--enable-remote-extensions' chromium &r
+    if type -p chromium &>/dev/null; then
+        cmd=chromium
+    else
+        cd
+        cmd="schroot -c stretch chromium"
+        CHROMIUM_FLAGS='--enable-remote-extensions' $cmd &r
+    fi
 }
 
 d() { builtin bg; }
@@ -514,6 +532,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,15 +557,34 @@ faf() { # find all files
          -o -name .undo-tree-history -prune \) 2>/dev/null
 }
 
-fastboot() { /a/opt/androidsdk/platform-tools/fastboot "$@"; }
+# one that comes with distros is too old for newer devices
+fastboot() {
+    /a/opt/android-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=(
-    at.bitfire.davdroid
     com.alaskalinuxuser.justnotes
     com.artifex.mupdfdemo
     com.fsck.k9
@@ -554,10 +596,8 @@ fdroid_pkgs=(
     com.zoffcc.applications.zanavi
     cz.martykan.forecastie
     de.danoeh.antennapod
-    de.marmaro.krt.ffupdater
     im.vector.alpha # riot
     info.papdt.blackblub
-    me.ccrama.redditslide
     me.tripsit.tripmobile
     net.gaast.giggity
     net.osmand.plus
@@ -689,6 +729,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:
@@ -753,6 +816,10 @@ and works in older versions of git which did not have that."
     echo "${p%%/.git}"
 }
 
+gitian() {
+    git config user.email ian@iankelling.org
+}
+
 gmacs() {
     # quit will prompt if the program crashes.
     gdb -ex=r -ex=quit --args emacs "$@"; r;
@@ -956,6 +1023,8 @@ mbenable() {
     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() {
@@ -968,6 +1037,11 @@ mbdisable() {
     set +x
 }
 
+mdt() {
+    markdown -o /tmp/mdtest.html "$1"
+    firefox /tmp/mdtest.html
+}
+
 
 mkc() {
     mkdir "$1"
@@ -982,11 +1056,13 @@ mkt() { # mkdir and touch file
 
 mkdir() { command mkdir -p "$@"; }
 
+mo() { xset dpms force off; } # monitor off
+
 otp() {
-    echo "oathtool --totp -b"
-    oathtool --totp -b "$@"
+    oathtool --totp -b "$@" | xclip -selection clipboard
 }
 
+# pithosfly is broken due to bitrot.
 pithos() {
     cd /
     export PYTHONPATH=/a/opt/Pithosfly
@@ -1135,6 +1211,7 @@ 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
@@ -1231,7 +1308,7 @@ ser() {
 
 sgo() { # service go
     service=$1
-    ser restart $service
+    ser restart $service || return 1
     if type -p systemctl &>/dev/null; then
         ser enable $service
     fi
@@ -1330,6 +1407,19 @@ srun() {
     ssh $1 /tmp/${2##*/} "${@:2}"
 }
 
+sss() { # ssh solo
+    ssh -oControlMaster=no -oControlPath=/ "$@"
+}
+ssk() {
+    local -a opts=()
+    while [[ $1 == -* ]]; do
+        opts+=("$1")
+        shift
+    done
+    m pkill -f "^ssh: /tmp/ssh_mux_${USER}_${1#*@}_22_"
+    m ssh "${opts[@]}" "$@"
+}
+
 swap() {
     local tmp
     tmp=$(mktemp)
@@ -1399,6 +1489,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}"
@@ -1409,7 +1500,8 @@ testmail() {
     # MSG_ID is in /var/log/exim4/mainlog, looks like 1ccdnD-0001nh-EN
 }
 
-# to test sieve, use below command. make modifications, then copy to live file, 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,
@@ -1417,17 +1509,19 @@ testmail() {
 
 # 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/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
@@ -1455,6 +1549,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;
@@ -1493,7 +1594,7 @@ m() { printf "%s\n" "$*";  "$@"; }
 
 
 vpncmd() {
-    m s nsenter -t $(pgrep -f "/usr/sbin/openvpn --suppress-timestamps --nobind --config /etc/openvpn/client/client.conf") -n -m "$@"
+    m s nsenter -t $(pgrep -f "/usr/sbin/openvpn .* --config /etc/openvpn/client/client.conf") -n -m "$@"
 }
 vpnf() {
     vpncmd gksudo -u ian "firefox -no-remote -P firefox-main-profile" &r
@@ -1503,22 +1604,6 @@ vpnbash() {
 }
 
 
-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
@@ -1546,14 +1631,31 @@ vmunshare() {
 
 
 vpn() {
+    if [[ -e /lib/systemd/system/openvpn-client@.service ]]; then
+        local vpn_service=openvpn-client
+    else
+        local vpn_service=openvpn
+    fi
+
     [[ $1 ]] || { echo need arg; return 1; }
-    journalctl --unit=openvpn-client@$1 -f -n0 &
-    s systemctl start openvpn-client@$1
+    journalctl --unit=$vpn_service@$1 -f -n0 &
+    s systemctl start $vpn_service@$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() {
     [[ $1 ]] || { echo need arg; return 1; }
-    s systemctl stop openvpn-client@$1
+    if [[ -e /lib/systemd/system/openvpn-client@.service ]]; then
+        local vpn_service=openvpn-client
+    else
+        local vpn_service=openvpn
+    fi
+    s systemctl stop $vpn_service@$1
 }
 
 
@@ -1574,10 +1676,13 @@ vspicy() { # usage: VIRSH_DOMAIN
 wtr() { curl wttr.in/boston; }
 
 xl() {
-    # this succeeds even if gnome-screensaver isn't running.
+    # this succeeds even if gnome-screensaver isn\'t running.
     gnome-screensaver-command --exit &>/dev/null
+    mate-screensaver-command --exit &>/dev/null
     if ! pidof xscreensaver; then
+        pushd /
         xscreensaver &
+        popd
         # 1 was not long enough
         sleep 3
     fi