From: Gustav Eek <gustav.eek@fripost.org>
Date: Wed, 11 Dec 2024 12:29:09 +0000 (+0100)
Subject: [watchsite] Provide a reverse mode and changing mode
X-Git-Url: https://git.g-eek.se/?a=commitdiff_plain;p=users%2Fgustav%2Fscripts.git

[watchsite] Provide a reverse mode and changing mode
---

diff --git a/watchsite b/watchsite
index 8c61744..b577cd4 100644
--- a/watchsite
+++ b/watchsite
@@ -1,16 +1,23 @@
 #!/usr/bin/env bash
 
-HELP="Usage: watchsite [-h] URL PATTERN
+HELP="Usage: watchsite [-h] [-v] URL [PATTERN]
 
-Whatch a website for a pattern and send an alarm email when found.
+Whatch a website for a pattern and send an alarm email when found. In
+MATCHING mode the PATTERN is compared against the output document. In
+CHANGING mode the document is compared against itself for changes.
 
 PARAMETERS
 
+    URL        page to watch
+    PATTERN    alarm when pattern found at URL
     EMAIL      read as environmental variable
 
+OPTIONS
+
+    -h         print help and exit
+    -v         watch for when pattern is not present (grep -v)
 "
 
-DEFAULT_EMAIL="gustav.eek@fripost.org"
 SLEEP=3600 
 
 if [ "$1" == "-h" ]; then
@@ -18,30 +25,49 @@ if [ "$1" == "-h" ]; then
     exit
 fi
 
+if [ "$1" == "-v" ]; then
+    rvrs=1
+    shift
+fi
+
 if [ -z "$EMAIL" ]; then
-    email="$DEFAULT_EMAIL"
-else
-    email="$EMAIL"
+    echo "${0##*/}: E: No EMAIL available in environment. See \`${0##*/} -h\`." 1>&2
+    exit 1
 fi
 
+email="$EMAIL"
 url="$1"
 ptrn="$2"
 
+if [ -n "$ptrn" -a -n "$rvrs" ]; then
+    text0="Check that '$ptrn' is not at <$url>!"
+elif [ -n "$ptrn" ]; then
+    text0="Check result of '$ptrn' at <$url>!"
+else
+    text0="The content at <$url> is updated!"
+fi
 text="from: $email
 to: $email
 subject: Watchsite result found
 
-Check result of '$ptrn' at <$url> !
+$text0
 "
 
+sum=$(curl -sL "$url" | md5sum | cut -d\  -f1)
+
 while true; do
-    if curl -sL "$url" | grep -qi "$ptrn"; then
+    if [ -n "$ptrn" -a -n "$rvrs" ] && \
+           ! curl -sL "$url" | grep -i "$ptrn" | grep -qi "$ptrn"; then
+        echo "$text" | msmtp "$email"
+        exit
+    elif [ -n "$ptrn" -a -z "$rvrs" ] && \
+             curl -sL "$url" | grep -qi "$ptrn"; then
+        echo "$text" | msmtp "$email"
+        exit
+    elif [ -z "$ptrn" ] && \
+             [ "$(curl -sL "$url" | md5sum | cut -d\  -f1)" != "$sum" ]; then
         echo "$text" | msmtp "$email"
         exit
     fi
     sleep $SLEEP
 done
-
-        
-
-