#!/bin/sh

# Script to obtain Slackware's current changelog information, and mail
# to local recipients if there's been a change.
#
# 2006-01-18 Sylvain Robitaille
# 2012-04-19 Sylvain Robitaille: due to frequent outages on
#               "www.slackware.com", retrieve changelogs from the
#               more reliable "ftp.slackware.com" instead.

# The local mail recipient(s):
MAILRECIP=syl@therockgarden.ca

# The program we use to send mail:
MAILPROG=/usr/sbin/sendmail

# First get a copy of the current pages:
for d in slackware-current slackware; do
   lynx -dump -nolist -nostatus \
      ftp://ftp.slackware.com/pub/slackware/${d}/ChangeLog.txt \
      > /tmp/${d}.changelog.NEW
   
   # If we couldn't get it, mail a complaint.  Perhaps our network
   # connection is down?
   if [ $? -ne 0 ]; then
      ( cat << END_OF_ERROR
From: slackware.changelog watcher <${MAILRECIP}>
To: ${MAILRECIP}
Subject: ERROR retrieving ${d}.changelog

There was an error Retrieving ${d}.changelog.  Check the
network connection.
END_OF_ERROR
      ) | ${MAILPROG} -t -oi -om
      exit 1
   fi
done

# If we don't have an old copy of the file, or if the new copy is
# different than the old, mail the new file to the recipient(s):
for d in slackware-current slackware; do

   # Assume that we won't be mailing a report ...
   mailit="no"

   if [ -f /tmp/${d}.changelog ]; then
      diff -wiq /tmp/${d}.changelog /tmp/${d}.changelog.NEW > /dev/null
      if [ $? -ne 0 ]; then
         mailit="yes"
      fi
   else
      mailit="yes"
   fi

   # Mail the report if necessary:
   if [ "${mailit}" = "yes" ]; then
      # Mail the output:
      ( cat << END_OF_HEADERS
From: slackware.changelog watcher <${MAILRECIP}>
To: ${MAILRECIP}
Subject: New ${d}.changelog

END_OF_HEADERS
       diff -uwP /tmp/${d}.changelog /tmp/${d}.changelog.NEW
      ) | ${MAILPROG} -t -oi -om

      # Save a copy for next time:
      mv /tmp/${d}.changelog.NEW /tmp/${d}.changelog
   else
      # else we simply clean up our new copy of the file:
         rm /tmp/${d}.changelog.NEW
   fi
done

