#!/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
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
-
-
-
-