various fixes and improvements
authorIan Kelling <ian@iankelling.org>
Thu, 30 Mar 2017 23:29:33 +0000 (16:29 -0700)
committerIan Kelling <ian@iankelling.org>
Thu, 4 May 2017 23:42:08 +0000 (16:42 -0700)
brc

diff --git a/brc b/brc
index 67a113f7a2a4839f515cd61909d0b453dcb74ea2..d8cfd227b6e7315d62f3405b8ba3c849675a1b79 100644 (file)
--- a/brc
+++ b/brc
@@ -170,6 +170,7 @@ source $(dirname $(readlink -f $BASH_SOURCE))/path_add-function
 source /a/bin/log-quiet/logq-function
 path_add /a/exe
 path_add --ifexists --end /a/opt/adt-bundle*/tools /a/opt/adt-bundle*/platform-tools
+export WCDHOME=/a
 # based on readme.debian. dunno if this will break on other distros.
 _x=/usr/share/wcd/wcd-include.sh
 if [[ -e $_x ]]; then source $_x; fi
@@ -269,8 +270,12 @@ _cdiff-prep() {
 
 _khfix_common() {
     local h=${1##*@}
+    local x
     ssh-keygen -R $h -f $(readlink -f ~/.ssh/known_hosts)
-    local x=$(timeout 0.1 ssh -v $1 |& sed -rn "s/debug1: Connecting to $h \[([^\]*)].*/\1/p");
+    if ! x=$(timeout 0.1 ssh -oBatchMode=yes -v $1 |& sed -rn "s/debug1: Connecting to $h \[([^\]*)].*/\1/p"); then
+        echo "khfix: ssh failed"
+        return 1
+    fi
     ssh-keygen -R $x -f $(readlink -f ~/.ssh/known_hosts)
 }
 khfix() { # known hosts fix
@@ -309,18 +314,39 @@ bfg() { java -jar /a/opt/bfg-1.12.14.jar "$@"; }
 
 btc() {
     local f=/etc/bitcoin/bitcoin.conf
-    bitcoin-cli -$(s grep rpcuser= $f) -$(s grep rpcpassword= $f) "$@"
+    # importprivkey will timeout if using the default of 15 mins.
+    # upped it to 1 hour.
+    bitcoin-cli -rpcclienttimeout=60000 -$(s grep rpcuser= $f) -$(s grep rpcpassword= $f) "$@"
 }
 
 btcusd() {
     local price
-    price="$(curl -s https://blockchain.info/ticker | jq .USD.last)"
+    price="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
     printf "$%s\n" "$price"
     if [[ $1 ]]; then
-        printf "$%.2f\n" "$(echo "scale=2; $price * $1"| bc -l)"
+        printf "$%.2f\n" "$(echo "scale=4; $price * $1"| bc -l)"
+    fi
+}
+usdbtc() {
+    local price
+    price="$(curl -s https://api.coinbase.com/v2/prices/BTC-USD/spot | jq -r .data.amount)"
+    printf "$%s\n" "$price"
+    if [[ $1 ]]; then
+        # 100 mil satoshi / btc. 8 digits after the 1.
+        printf "%.8f btc\n" "$(echo "scale=10; $1 / $price "| bc -l)"
+    fi
+}
+satoshi() {
+    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)
+    printf "$%f\n" "$price"
+    if [[ $1 ]]; then
+        printf "$%.2f\n" "$(echo "scale=10; $price * $1"| bc -l)"
     fi
 }
 
+
 # c. better cd
 if [[ $RLC_INSIDE_EMACS ]]; then
     c() { wcd -c -z 50 -o "$@"; }
@@ -331,6 +357,14 @@ fi
 
 caa() { git commit --amend --no-edit -a; }
 
+caf() {
+    find -L $1 -type f -not \( -name .svn -prune -o -name .git -prune \
+         -o -name .hg -prune -o -name .editor-backups -prune \
+         -o -name .undo-tree-history -prune \) \
+         -exec bash -lc 'hr; echo "$1"; hr; cat "$1"' _ {} \; 2>/dev/null
+
+}
+
 calc() { echo "scale=3; $*" | bc -l; }
 # no having to type quotes, but also no command history:
 clc() {
@@ -401,6 +435,10 @@ cl() {
     c =
 }
 
+chrome() {
+    CHROMIUM_FLAGS='--enable-remote-extensions' chromium &r
+}
+
 d() { builtin bg; }
 complete -A stopped -P '"%' -S '"' d
 
@@ -489,7 +527,9 @@ fa() {
 }
 
 faf() { # find all files
-    find $@ -type f
+    find -L $1 -type f -not \( -name .svn -prune -o -name .git -prune \
+         -o -name .hg -prune -o -name .editor-backups -prune \
+         -o -name .undo-tree-history -prune \) 2>/dev/null
 }
 
 fastboot() { /a/opt/androidsdk/platform-tools/fastboot "$@"; }
@@ -704,7 +744,7 @@ hl() { # history limit. Write extra history to archive file.
 }
 
 hr() { # horizontal row. used to break up output
-    printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(seq $COLUMNS)
+    printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(seq ${COLUMNS:-60})
     echo
 }
 
@@ -737,7 +777,7 @@ ifn() {
     # insensitive find
     find -L . -not \( -name .svn -prune -o -name .git -prune \
          -o -name .hg -prune -o -name .editor-backups -prune \
-    -o -name .undo-tree-history -prune \) -iname "*$**" 2>/dev/null
+         -o -name .undo-tree-history -prune \) -iname "*$**" 2>/dev/null
 }
 
 
@@ -790,23 +830,26 @@ lcn() { locate -i "*$**"; }
 
 lld() { ll -d "$@"; }
 
-low() {  # make filenames all lowercase
-    local x y
-    for x in "$@"; do
-        y=$(tr "[A-Z]" "[a-z]" <<<"$x")
-        [[ $y != $x ]] && mv "$x" "$y"
+low() {  # make filenames lowercase, remove bad chars
+    local f new
+    for f in "$@"; do
+        new="${f,,}" # downcase
+        new="${new//[^[:alnum:]._-]/_}" # sub bad chars
+        new="${new#"${new%%[[:alnum:]]*}"}" # remove leading/trailing non-alnum
+        new="${new%"${new##*[[:alnum:]]}"}"
+        # remove bad underscores, like __ and _._
+        new=$(echo $new | sed -r 's/__+/_/g;s/_+([.-])|([.-])_+/\1/g')
+        safe_rename "$f" "$new" || return 1
     done
+    return 0
 }
 
-
-
-
 lower() { # make first letter of filenames lowercase.
     local x
     for x in "$@"; do
         if [[ ${x::1} == [A-Z] ]]; then
             y=$(tr "[A-Z]" "[a-z]" <<<"${x::1}")"${x:1}"
-            safe_rename "$x" "$y"
+            safe_rename "$x" "$y" || return 1
         fi
     done
 }
@@ -996,15 +1039,17 @@ s() {
     fi
 }
 
-safe_rename() {
+safe_rename() { # warn and don't rename if file exists.
+    # mv -n exists, but it's silent
     if [[ $# != 2 ]]; then
         echo safe_rename error: $# args, need 2 >2
         return 1
-    elif [[ $1 != $2 ]]; then
-        if [[ -e $2 ]]; then
-            echo Cannot rename "$1" to "$2" as it already exists.
+    fi
+    if [[ $1 != $2 ]]; then # yes, we want to silently ignore this
+        if [[ -e $2 || -L $2 ]]; then
+            echo "Cannot rename $1 to $2 as it already exists."
         else
-            mv "$1" "$2"
+            mv -vi "$1" "$2"
         fi
     fi
 }
@@ -1169,12 +1214,49 @@ te() {
 
 testmail() {
     declare -gi _seq; _seq+=1
-    echo "test body" | m mail -s "test mail from $HOSTNAME, $_seq" "${1:-root@localhost}"
+    echo "test body" | m mail -s "test mail from $HOSTNAME, $_seq" "${@:-root@localhost}"
+    # for testing to send from an external address, you can do for example
+    # -aFrom:ian@iank.bid web-6fnbs@mail-tester.com
+    # note in exim, you can retry a deferred message
+    # s exim -M MSG_ID
+    # MSG_ID is in /var/log/exim4/mainlog, looks like 1ccdnD-0001nh-EN
+}
+
+# use -eW to actually modify mailbox
+testsievelist() {
+    sieve-filter ~/sieve/main.sieve "$@" >/a/tmp/slog 2> >(tail) && sed -rn '/^Performed actions:/{n;n;p}' /a/tmp/slog | sort -u
+}
+
+
+testsieve() {
+    sieve-filter ~/sieve/main.sieve "$@"
+}
+
+testexim() {
+    # testmail above calls sendmail, which is a link to exim/postfix.
+    # it's docs don't say a way of adding an argument
+    # to sendmail to turn on debug output. We could make a wrapper, but
+    # 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
+    # adding -d, etc to COMMONOPTIONS in
+    # /etc/default/exim4
+    # for testing external mail, you need the to address as final cmdline arg
+    exim -d+tls -t <<'EOF'
+From: root@frodo.lan
+To: ian@mail.iankelling.org
+Subject: Testing Exim
+
+This is a test message.
+EOF
 }
 
 tm() {
     # timer in minutes
-    (sleep $(calc "$@ * 60") && mpv --volume 50 /a/bin/data/alarm.mp3 --loop=no) > /dev/null 2>&1 &
+    # --no-config
+    (sleep $(calc "$@ * 60") && mpv --no-config --volume 50 /a/bin/data/alarm.mp3) > /dev/null 2>&1 &
 }