From 85359f604daf07356b82436df151c8e2871858c7 Mon Sep 17 00:00:00 2001
From: Eric Claeys <83164203+EricClaeys@users.noreply.github.com>
Date: Sun, 29 Dec 2024 02:33:38 -0600
Subject: [PATCH] Update addMessage.sh: Create new-format messages
---
scripts/addMessage.sh | 125 +++++++++++++++++++++++++++++++++---------
1 file changed, 98 insertions(+), 27 deletions(-)
diff --git a/scripts/addMessage.sh b/scripts/addMessage.sh
index e159a8e60..086469d33 100755
--- a/scripts/addMessage.sh
+++ b/scripts/addMessage.sh
@@ -10,21 +10,93 @@ ME="$( basename "${BASH_ARGV0}" )"
#shellcheck disable=SC1091 source=variables.sh
source "${ALLSKY_HOME}/variables.sh" || exit "${EXIT_ERROR_STOP}"
-if [ $# -lt 2 ]; then
- # shellcheck disable=SC2154
+usage_and_exit()
+{
+ local RET=${1}
{
- echo -e "${wERROR}"
- echo "Usage: ${ME} message_type message [url]"
- echo -e "${wNC}"
+ echo
+ [[ ${RET} -ne 0 ]] && echo -en "${wERROR}"
+ echo "Usage: ${ME} [--id ID [--cmd TEXT]] [--delete] --type message_type --msg message [--url url]"
+ [[ ${RET} -ne 0 ]] && echo -en "${wNC}"
echo -e "\n'message_type' is 'success', 'warning', 'error', 'info', or 'debug'."
echo -e "\n'url' is a URL to (normally) a documentation page."
} >&2
- exit 1
+ exit "${RET}"
+}
+
+OK="true"
+DO_HELP="false"
+DEBUG="false"
+ID=""
+CMD_TEXT=""
+TYPE=""
+MESSAGE=""
+URL=""
+while [[ $# -gt 0 ]]; do
+ ARG="${1}"
+ case "${ARG,,}" in
+ "--help")
+ DO_HELP="true"
+ ;;
+ "--debug")
+ DEBUG="true"
+ ;;
+ "--id")
+ ID="${2}"
+ shift
+ ;;
+ "--delete")
+ DELETE="true"
+ shift
+ ;;
+ "--cmd")
+ CMD_TEXT="${2}"
+ shift
+ ;;
+ "--type")
+ TYPE="${2,,}"
+ shift
+ ;;
+ "--msg")
+ MESSAGE="${2}"
+ shift
+ ;;
+ "--url")
+ URL="true"
+ shift
+ ;;
+ -*)
+ echo -e "${wERROR}Unknown argument '${ARG}' ignoring.${wNC}" >&2
+ OK="false"
+ ;;
+ *)
+ break
+ ;;
+ esac
+ shift
+done
+
+[[ ${DO_HELP} == "true" ]] && usage_and_exit 0
+[[ ${OK} == "false" ]] && usage_and_exit 1
+
+if [[ ${DELETE} == "true" ]]; then
+ [[ ! -f ${ALLSKY_MESSAGES} ]] && exit 0
+ if [[ -z ${ID} ]]; then
+ echo "${ME}: ERROR: delete specified but no message id given." >&2
+ exit 1
+ fi
+
+ REST="$( grep -v "^${ID}${TAB}" "${ALLSKY_MESSAGES}" )"
+ if [[ -z ${REST} ]]; then
+ rm -f "${ALLSKY_MESSAGES}" # was only message
+ else
+ echo -e "${REST}" > "${ALLSKY_MESSAGES}"
+ fi
+ exit 0
fi
# The CSS classes are all lower case, so convert.
# Our "error" and "debug" message types have a different CSS class name, so map them.
-TYPE="${1,,}"
if [[ ${TYPE} == "error" ]]; then
TYPE="danger"
elif [[ ${TYPE} == "debug" ]]; then
@@ -35,34 +107,33 @@ elif [[ ${TYPE} != "warning" && ${TYPE} != "info" && ${TYPE} != "success" ]]; th
echo -e "${wWARNING}Warning: unknown message type: '${TYPE}'. Using 'info'.${wNC}" >&2
TYPE="info"
fi
-MESSAGE="${2}"
-URL="${3}"
DATE="$( date '+%B %d, %r' )"
# The file is tab-separated: type date count message url
TAB="$( echo -e "\t" )"
-# Convert newlines to HTML breaks.
-MESSAGE="$( echo -en "${MESSAGE}" |
- awk 'BEGIN { l=0; } { if (++l > 1) printf("
"); printf("%s", $0); }' )"
+if [[ -n ${MESSAGE} ]]; then
+ # Convert newlines to HTML breaks.
+ MESSAGE="$( echo -en "${MESSAGE}" |
+ awk 'BEGIN { l=0; } { if (++l > 1) printf("
"); printf("%s", $0); }' )"
-# Make 2 spaces in a row viewable in HTML.
-MESSAGE="${MESSAGE// /\ \ }"
+ # Make 2 spaces in a row viewable in HTML.
+ MESSAGE="${MESSAGE// /\ \ }"
-# Convert tabs to spaces because we use tabs as field separators.
-# Tabs in the input can either be an actual tab or \t
-MESSAGE="${MESSAGE//${TAB}/\ \ \ \ }"
-MESSAGE="${MESSAGE//\\t/\ \ \ \ }"
+ # Convert tabs to spaces because we use tabs as field separators.
+ # Tabs in the input can either be an actual tab or \t
+ MESSAGE="${MESSAGE//${TAB}/\ \ \ \ }"
+ MESSAGE="${MESSAGE//\\t/\ \ \ \ }"
-# Messages may have "/" in them so we can't use that to search in sed,
-# so use "%" instead, but because it could be in a message (although unlikely),
-# convert all "%" to the ASCII code.
-# The pound sign in escaped only to make gvim look nicer.
-MESSAGE="${MESSAGE//%/\&\#37;}"
-
-# If ${MESSAGE} contains "*" it hoses up the grep and sed regular expression, so escape it.
-ESCAPED_MESSAGE="${MESSAGE//\*/\\*}"
+ # Messages may have "/" in them so we can't use that to search in sed,
+ # so use "%" instead, but because it could be in a message (although unlikely),
+ # convert all "%" to the ASCII code.
+ # The pound sign in escaped only to make gvim look nicer.
+ MESSAGE="${MESSAGE//%/\&\#37;}"
+ # If ${MESSAGE} contains "*" it hoses up the grep and sed regular expression, so escape it.
+ ESCAPED_MESSAGE="${MESSAGE//\*/\\*}"
+fi
if [[ -f ${ALLSKY_MESSAGES} ]] && M="$( grep "${TAB}${ESCAPED_MESSAGE}${TAB}" "${ALLSKY_MESSAGES}" )" ; then
COUNT=0
@@ -82,4 +153,4 @@ else
COUNT=1
fi
-echo -e "${TYPE}${TAB}${DATE}${TAB}${COUNT}${TAB}${MESSAGE}${TAB}${URL}" >> "${ALLSKY_MESSAGES}"
+echo -e "${ID}${TAB}${CMD_TEXT}${TAB}${TYPE}${TAB}${DATE}${TAB}${COUNT}${TAB}${MESSAGE}${TAB}${URL}" >> "${ALLSKY_MESSAGES}"