From 3ed82a97af4f0c65d59bbf748a19ac907db40753 Mon Sep 17 00:00:00 2001
From: Markus Heiser <markus.heiser@darmarit.de>
Date: Sun, 12 Jan 2020 15:51:04 +0100
Subject: [PATCH] utils/filtron.sh: add command 'update filtron'

BTW: tidy up some code style and fix some issues reported by the linter.

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
---
 utils/filtron.sh | 50 ++++++++++++++++++++++++++++++++++--------------
 utils/lib.sh     |  2 +-
 2 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/utils/filtron.sh b/utils/filtron.sh
index 597d0c634..5c8a738b0 100755
--- a/utils/filtron.sh
+++ b/utils/filtron.sh
@@ -12,8 +12,12 @@ source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
 FILTRON_ETC="/etc/filtron"
 
 FILTRON_RULES="$FILTRON_ETC/rules.json"
+
+# shellcheck disable=SC2034
 FILTRON_API="127.0.0.1:4005"
+# shellcheck disable=SC2034
 FILTRON_LISTEN="127.0.0.1:4004"
+# shellcheck disable=SC2034
 FILTRON_TARGET="127.0.0.1:8888"
 
 SERVICE_NAME="filtron"
@@ -28,6 +32,7 @@ GO_ENV="${SERVICE_HOME}/.go_env"
 GO_PKG_URL="https://dl.google.com/go/go1.13.5.linux-amd64.tar.gz"
 GO_TAR=$(basename "$GO_PKG_URL")
 
+# shellcheck disable=SC2034
 CONFIG_FILES=(
     "${FILTRON_RULES}"
     "${SERVICE_SYSTEMD_UNIT}"
@@ -44,6 +49,7 @@ usage:
 
   $(basename "$0") shell
   $(basename "$0") install    [all|user]
+  $(basename "$0") update     [filtron]
   $(basename "$0") remove     [all]
   $(basename "$0") activate   [service]
   $(basename "$0") deactivate [service]
@@ -51,8 +57,10 @@ usage:
 
 shell
   start interactive shell from user ${SERVICE_USER}
-install / remove all 
+install / remove all
   complete setup of filtron service
+update filtron
+  Update filtron installation of user ${SERVICE_USER}
 activate
   activate and start service daemon (systemd unit)
 deactivate service
@@ -71,33 +79,39 @@ main(){
     local _usage="ERROR: unknown or missing $1 command $2"
 
     case $1 in
-	--source-only)  ;;
+        --source-only)  ;;
         -h|--help) usage; exit 0;;
 
-	shell)
-	    sudo_or_exit
-	    interactive_shell
-	    ;;
+        shell)
+            sudo_or_exit
+            interactive_shell
+            ;;
         show)
             case $2 in
                 service)
-		    sudo_or_exit
-		    show_service
-		    ;;
+                    sudo_or_exit
+                    show_service
+                    ;;
                 *) usage "$_usage"; exit 42;;
             esac ;;
         install)
             sudo_or_exit
             case $2 in
                 all) install_all ;;
-		user) assert_user ;;
+                user) assert_user ;;
+                *) usage "$_usage"; exit 42;;
+            esac ;;
+        update)
+            sudo_or_exit
+            case $2 in
+                filtron) update_filtron ;;
                 *) usage "$_usage"; exit 42;;
             esac ;;
         remove)
             sudo_or_exit
             case $2 in
                 all) remove_all;;
-		user) remove_user ;;
+                user) remove_user ;;
                 *) usage "$_usage"; exit 42;;
             esac ;;
         activate)
@@ -242,16 +256,24 @@ EOF
     install_template --no-eval "$FILTRON_RULES" root root 644
 }
 
+update_filtron() {
+    rst_title "Update filtron" section
+    echo
+    tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" 2>&1 | prefix_stdout "$_service_prefix"
+go get -v -u github.com/asciimoo/filtron
+EOF
+}
+
 show_service () {
     rst_title "service status & log"
     echo
     systemctl status filtron.service
     echo
-    read -s -n1 -t 5  -p "// use CTRL-C to stop monitoring the log"
+    read -r -s -n1 -t 5  -p "// use CTRL-C to stop monitoring the log"
     echo
     while true;  do
-	trap break 2
-	journalctl -f -u filtron
+        trap break 2
+        journalctl -f -u filtron
     done
     return 0
 }
diff --git a/utils/lib.sh b/utils/lib.sh
index d06cabf26..fd6b92129 100755
--- a/utils/lib.sh
+++ b/utils/lib.sh
@@ -75,7 +75,7 @@ info_msg() { echo -e "INFO:  $*"; }
 
 clean_stdin() {
     if [[ $(uname -s) != 'Darwin' ]]; then
-        while $(read -n1 -t 0.1); do : ; done
+        while read -n1 -t 0.1; do : ; done
     fi
 }