#!/bin/bash ################################################################################ # STATE_MACHINE.SH # ---------------- # This script saves the last message for a certain key and compares the next # message for the same key to not have any repeating notifications. # # Author: Robin Meier - robin@meier.si ################################################################################ set -o allexport source /root/scripts/config/state_machine set +o allexport mkdir -p $STORAGE_PATH # Make sure STORAGE_PATH exists # Get input from standard input or via first parameter if [[ $# -eq 0 ]]; then echo "[ERROR] Not enough arguments!" exit 1 elif [[ $# -eq 1 ]]; then MESSAGE=$(timeout 32 cat) KEY=$1 elif [[ $# -eq 2 ]]; then KEY=$1 MESSAGE=$2 else echo "[ERROR] Too many arguments!" exit 1 fi # Check if KEY is empty if [[ -z "${KEY}" ]]; then echo "[ERROR] KEY argument is missing!" exit 1 fi KEY_FILE="${STORAGE_PATH}/${KEY}.txt" if [[ -f $KEY_FILE && -z "${MESSAGE}" ]]; then # Previous message present and empty message now OLD_MESSAGE=$(cat $KEY_FILE) echo "✅ Resolved" echo "$OLD_MESSAGE" rm $KEY_FILE exit 0 elif [[ -f $KEY_FILE ]]; then # Message and previous message present OLD_MESSAGE=$(cat $KEY_FILE) # Compare contents if [[ "$OLD_MESSAGE" == "$MESSAGE" ]]; then # Check last notification if [ "$(( $(date +"%s") - $(stat -c "%Y" "$KEY_FILE") ))" -gt "$RENOTIFY_AGE_SEC" ]; then touch $KEY_FILE echo "‼️Renotify" else exit 0 fi else echo "$MESSAGE" > $KEY_FILE echo "⁉️Changed" fi else if [[ -z "${MESSAGE}" ]]; then # No message present exit 0 fi # New message present, create KEY_FILE, continue to relaying echo "$MESSAGE" > $KEY_FILE echo "❗New" fi # Relay message if made it until here (Quotes are important here, so lines dont get .join(' ')-ed) echo "$MESSAGE" exit 0