cred source

Home
#!/bin/bash
#
#	cred 0.8.1 (13-Mar-2016) - Copyright (C) 2016 Dario Niedermann
#
#	Check REmote Document, report if it changed since last check
#
#	$Id: cred 87 2016-03-13 21:28:48Z ndr $

umask 077
myName="${0##*/}"
myVerbosity=0
#DEBUG_GETOPT=y
wgetVerbosity='q'
myVersion=`sed -ne 3p "$0"|tr ' \t' "\n\n"|grep '^[0-9]'|head -n1`
myPlatform=`uname -sm`
wgetVersion=`wget -V |head -n1 |tr ' ' '\n' |head -n3|tail -n1`
userAgent="cred/$myVersion ($myPlatform) Wget/$wgetVersion"
comment=''; urls=''
pingServer='www.pingtest.net'

if [ $# -lt 1 ]; then 
	grep -A1 '^Usage' "$0" |sed -e "s,PROGNAME,$myName,"; exit 64
fi
# else...
if [ -e /dev/stderr -a -w /dev/stderr ]; then
	echoerr()
	{
		echo -e $myName: $@ >/dev/stderr
	}
else
	echoerr()
	{
		echo -e $myName: $@
	}
fi

temp=$(getopt -n "$myName" -o chnsvLNV -l comment,help,licence,license,nomail,report-unchanged,verbose,only-if-connected,version -- "$@")

if [ $? -ne 0 ]; then echoerr "Try 'cred --help' for more information."; exit 1; fi

[ -n "$DEBUG_GETOPT" ] && echoerr 'reading command line options'

eval set -- "$temp"
[ -n "$DEBUG_GETOPT" ] && echoerr "got command line options: $temp"

while true; do
	[ $DEBUG_GETOPT ] && echoerr "examining argument '$1'"
	case "$1" in
		-c|--comment) comment='--header="X-Comment: Please consider adding an RSS feed to your website"'
				shift ;;
		-v|--verbose) myVerbosity=$((myVerbosity++))
				shift ;;
		-s|--report-unchanged)
				reportUnchanged=y
				shift ;;
		-h|--help)	sed -n -e '/^Usage/,/^-----$/p' "$0" \
				|sed -e "s/PROGNAME/$myName/;2d;"'$d'
				exit ;;
		-n|--nomail)	noMail=y
				shift ;;
		-N|--only-if-connected)
				chkConnection=y
				shift ;;
		-L|-V|--licence|--license|--version)
			sed -n -e '3p' "$0" |sed -e 's/^#[[:blank:]]\+//'
			if [ "$1" = '-L' -o "${1:0:-2}" = '--licen' ]; then
				echo
				sed -n -e '/^-----$/,$p' "$0" |sed -e 1d
			fi
			exit ;;
		--)
			shift; break ;;
		*)
			echoerr "Internal error\041"; exit 70 ;;
	esac
done

if [ $# -eq 0 ]; then
	echoerr "no URL specified"; exit 64
fi
# else...
# If we must first check for net connection...
if [ -n "$chkConnection" ]; then
	ping -c1 $pingServer >& /dev/null
	[ $? -ne 0 ] && exit 0		# no connection? exit without errors
fi
# else...
urls="$@"

if [ $myVerbosity -gt 1 ]; then
	echoerr "URL is '$urls'"
	echoerr "picking a suitable working directory... "
fi

[ -z "$TMP" ] && TMP='/tmp'
[ -z "$TMPDIR" ] && TMPDIR='/tmp'
cd
for d in "$HOME/tmp" "$TMP" "$TMPDIR"; do
	if [ -d "$d" -a -w "$d" ]; then
		cd "$d"; break
	fi
done
if [ "$PWD" = "$HOME" ]; then	# haven't found an existing writable dir?
	echoerr 'WARNING: creating \047tmp/\047 in your home directory'
	echoerr '         set $TMP or $TMPDIR env variables to use those dirs instead'
	[ $myVerbosity -gt 0 ] && echoerr '         if said vars are already set, make sure you have write access'
	mkdir tmp
	if [ $? -ne 0 ]; then
		echoerr 'Whoa\041 I couldn\047t write to your Home dir...'
		echoerr 'Sorry mate, this system is too broken for me. Aborting.'
		exit 73
	else
		cd tmp
	fi
fi
if [ ! -w "$PWD" ]; then
	echoerr "You have no write access to \0140$PWD\047. Aborting."
	exit 77
fi
[ $myVerbosity -gt 1 ] && echoerr "\0140$PWD\047 will do"

#--------------------------------------------------------Find the 'mail' program

if [ -z "$noMail" ]; then				# but only if needed
	[ $myVerbosity -gt 1 ] && echoerr "Looking for the \047mail\047 program..."
	for mailPrgCandidate in /{,usr/{,local/}}bin/{m,M}ail{,x} ; do
		mailPrg=`which $mailPrgCandidate`
		rc=$?
		[ $myVerbosity -gt 2 ] && echoerr "checking $mailPrg..."
		[ $rc -eq 0 ] && break
	done
	if [ $rc -ne 0 ]; then
		echoerr 'The "mail" program was not found. Aborting.'
		exit 65
	fi
	[ $myVerbosity -gt 1 ] && echoerr "found $mailPrg"
fi

export MAILRC=/dev/null
export LC_ALL=en_US.utf8 # just so mailx won't complain on multibyte chars. Making the 
			 # script's locale conf'able might however be a good idea
mailOpts="-n -S 'sendcharsets=iso-8859-1,utf-8,utf-16' -r ${myName}@localhost"

for url in $urls ; do
	# make filename from $url
	fileName=''; temp="${url%/}"	# remove trailing /
	shortFileName="${temp##*/}"

	# a file name prefix is made from the URL's hash. This is to prevent
	# 'a.com/index.html' and 'b.com/index.html' from ending up written
	# to the same file
	prefix=`echo -n "$url"|openssl dgst -md5|tr ' ' '\n'|tail -n1|fold -w4|head -n1`
	fileName="${prefix}-$shortFileName"
	[ $myVerbosity -gt 1 ] && wgetVerbosity='d'

	eval wget -"$wgetVerbosity" -t5 -U "'$userAgent'" "$comment" -O "${fileName}~~" "$url"
	wgetErr=$?
	[ $wgetErr -ne 0 ] && echoerr "wget returned error $wgetErr on '$url'"

	if [ -e "$fileName" -a $wgetErr -eq 0 ]; then
		mv "$fileName" "$fileName~"
		mv "$fileName~~" "$fileName"

		diff -q "$fileName" "$fileName~" >& /dev/null
		diffExit=$?
		if [ -z "$noMail" ]; then
			if [ $diffExit -gt 0 ]; then
				$mailPrg $mailOpts		\
				-s "$shortFileName changed!"	\
				"$USER" < "$fileName"
			else
				if [ -n "$reportUnchanged" ]; then
					$mailPrg $mailOpts		 \
					-s "$shortFileName unchanged!" \
					"$USER" < "$fileName"
				fi
			fi
		else
			echo -n "'$url' "
			if [ $diffExit -ne 0 ]; then
				echo 'has changed'
			else
				echo 'is unchanged'
			fi
		fi
	elif [ $wgetErr -eq 0 ]; then	# 1st download
		mv "$fileName~~" "$fileName"
		[ -n $reportUnchanged ] && echo "'$url' not previously checked"
	fi
done
exit

Usage: PROGNAME [OPTION]... URL...
Try `PROGNAME --help' for more information.
Check document at URL, report if it has changed since last check

  -v, --verbose             output messages detailing program operation;
                            specify twice for extra verbosity
  -n, --nomail              do not send email; report results to STDOUT
  -s, --report-unchanged    also report when file at URL did not change
  -c, --comment             add a comment header to the HTTP request
                            which says: 'Please consider adding an RSS
                            feed to your website'
  -N, --only-if-connected   if no net connection, exit without errors
  -V, --version             display program version
  -L, --licence, --license  display program version and licence
  -h, --help                display this help and exit

Report bugs to: Dario Niedermann <dnied@tiscali.it>
-----
This is  free software.  You may redistribute  copies of it
under the terms of the GNU General Public License Version 3
<http://www.gnu.org/licenses/gpl-3.0.html>.

This program is distributed  WITHOUT ANY WARRANTY;  without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.  See  the GNU General Public  License
Version 3 for more details.
“cred” is Copyright © Dario Niedermann
released with no warranty under the terms of the GPLv3 license.
Home Valid HTML 4.01 Strict  Valid CSS!