From e3966f762f17ccfd0fda71cf5a5af985de66c592 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Sat, 31 Oct 2020 23:32:28 +0200
Subject: [PATCH] Another notification improvement.

- A wrapper for different notification mechanisms, for different hosts.
- Implement it where needed.
---
 .bashrc               | 13 +++---------
 Documents/bin/monitor | 13 +++++-------
 Documents/bin/notify  | 46 +++++++++++++++++++++++++++++++++++++++++++
 Documents/bin/spammer |  5 ++---
 4 files changed, 56 insertions(+), 21 deletions(-)
 create mode 100755 Documents/bin/notify

diff --git a/.bashrc b/.bashrc
index 7325628..221fc91 100644
--- a/.bashrc
+++ b/.bashrc
@@ -144,14 +144,7 @@ alias nextcloudcmd='flatpak run --command=nextcloudcmd org.nextcloud.Nextcloud'
 alias tfa='terraform apply tfplan'
 alias tfvf='tfv && terraform fmt -diff'
 
-if ! command -v notify-send > /dev/null
-then
-    alias notify-send='bell'
-elif [ -n "$GIO_LAUNCHED_DESKTOP_FILE" ]
-then
-    # shellcheck disable=SC2139
-    alias notify-send="notify-send --hint \"string:desktop-entry:$(basename "$GIO_LAUNCHED_DESKTOP_FILE")\""
-fi
+alias notify="notify --hint \"string:desktop-entry:$(basename "${GIO_LAUNCHED_DESKTOP_FILE:-io.elementary.terminal.desktop}")\""
 
 tfp () {
     workspace="$(terraform workspace show)"
@@ -372,9 +365,9 @@ __command_notifier () {
         then
             if [ "$exitstatus" -eq '0' ]
             then
-                notify-send "$last_command has finished."
+                echo "$last_command has finished." | notify
             else
-                notify-send --urgency=critical "$last_command has failed."
+                echo "$last_command has failed." | notify --urgency=critical
             fi
         fi
     fi
diff --git a/Documents/bin/monitor b/Documents/bin/monitor
index 5053f61..0d6944c 100755
--- a/Documents/bin/monitor
+++ b/Documents/bin/monitor
@@ -1,14 +1,11 @@
 #!/bin/sh
 set -eu
 
-if ! command -v notify-send > /dev/null
+if [ "$(whoami)" = "root" ] && [ -n "${SUDO_USER:-}" ]
 then
-    notify="printf '\\a'"
-elif [ "$(whoami)" = "root" ] && [ -n "${SUDO_USER:-}" ]
-then
-    notify="sudo --preserve-env --set-home --user $SUDO_USER notify-send"
+    notify="sudo --preserve-env --set-home --user $SUDO_USER notify"
 else
-    notify='notify-send'
+    notify='notify'
 fi
 
 eval "$@" || code="$?"
@@ -16,9 +13,9 @@ code="${code:-0}"
 
 if [ "$code" -eq 0 ]
 then
-    $notify "$(basename "${1#__}") has finished."
+    echo "$(basename "${1#__}") has finished." | $notify
 else
-    $notify --urgency=critical "$(basename "${1#__}") has failed."
+    echo "$(basename "${1#__}") has failed." | $notify --urgency=critical
 fi
 
 return "$code"
diff --git a/Documents/bin/notify b/Documents/bin/notify
new file mode 100755
index 0000000..0732066
--- /dev/null
+++ b/Documents/bin/notify
@@ -0,0 +1,46 @@
+#!/bin/sh
+set -eu
+
+bell () {
+    printf '\\a'
+}
+
+wall () {
+    $(command -v wall) "$(id -g)"
+}
+
+notify_send () {
+    message="$(cat)"
+    $(command -v notify-send) "$@" "$message"
+}
+
+patchbay () {
+    message="$(cat)"
+    curl -d "$message" https://patchbay.pub/pubsub/1446c969-9a54-4b5b-83e0-bf88f22f289d
+}
+
+# Try hostname if present.
+if command -v hostname > /dev/null
+then
+    hostname="$(hostname)"
+# Try using sysfs (Linux only).
+elif [ -f /proc/sys/kernel/hostname ]
+then
+    hostname="$(cat /proc/sys/kernel/hostname)"
+# Fallback to grepping sysctl.
+else
+    hostname="$(sysctl -a 2>/dev/null | awk -F '=' '/^kern[a-z]*\.hostname/ {print $2}')"
+fi
+
+#FIXME: Remove domain, just hostname.
+
+case "$hostname" in
+    ea6350) patchbay;;
+    host01) patchbay;;
+    kodi) patchbay;;
+    ns1) patchbay;;
+    ns4) patchbay;;
+    workbench) patchbay;;
+    YOGA-C930) notify_send "$@";;
+    *) ;;
+esac
diff --git a/Documents/bin/spammer b/Documents/bin/spammer
index aa55fdf..30ed9fb 100755
--- a/Documents/bin/spammer
+++ b/Documents/bin/spammer
@@ -11,10 +11,9 @@ do
     then
         message="$(echo "$message" | sed 's/message=//; s/+/ /g')"
     fi
-    notify-send \
+    echo "$message" | notify \
         --urgency low \
-        --hint "string:desktop-entry:spammer" \
-        "$message"
+        --hint "string:desktop-entry:spammer"
     # Backoff in case of an error.
     if [ "$exitstatus" -gt '0' ]
     then
-- 
GitLab