#!/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 ################################################################################ script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) set -o allexport source ${script_dir}/../config/state_machine set +o allexport mkdir -p ${script_dir}/../storage/state_machine # 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="${script_dir}/../storage/state_machine/${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