#!/bin/sh
#
#  CFD - version-0.97 
#  Changelog File Downloader.
#  
#  CFD uses 'wget' to download the ChangeLog.txt from a Slackware 
#  mirror to the ~/.cfd directory and emails a diff of recent updates
#  and security announcements to root or user.
#
# For a cron job at 8:15pm daily use 'crontab -e root' to add a
# line like the following. see 'man crontab' for details.
# 15 20 * * * /usr/local/bin/cfd.sh 1> /dev/null
#
#  Comes with no warranty and can be shared and modified as stated
#  in the Gnu Software licence at http://gnu.org/copyleft.
#
#  author - keithmg at e3dotnetdotnz -22/07/05 
###########################################################################
# description of variables. 
#MIRROR    - slackware mirrors. 
#VER       - slackware version 8.0 - 12.0 - current. 
#MAIL      - address to mail diff to, is user by default.
#CFDDIR    - changelog is downloaded to ~/.cfd         
###########################################################################


MIRROR=ftp://ftp.slackware.com/pub/slackware
#MIRROR=ftp://ftp.planetmirror.com/pub/slackware
#MIRROR=ftp://mirror.pudas.net/slackware
#MIRROR=ftp://distro.ibiblio.org/pub/linux/distributions/slackware
#MIRROR=ftp://ftp.slackware.at

VER=current
#VER=8.0
#VER=8.1
#VER=9.0
#VER=9.1
#VER=10.0
#VER=10.1
#VER=10.2
#VER=11.0
#VER=11.1
#VER=12.0
#VER=12.1

MAIL=`id -un`
#MAIL=root

CFDDIR=~/.cfd

########################################################################


if [ -x $CFDDIR ]; then
echo "dir ok"
else
mkdir $CFDDIR
fi

cd $CFDDIR

if [ ! -f "ChangeLog.old" ]; then
touch ChangeLog.old
echo "creating 1st changelog.old"
echo ""
fi

if [ ! -f "ChangeLog.txt" ]; then
touch ChangeLog.txt
echo "creating changelog.txt"
fi

MD5NEW1=`md5sum ChangeLog.txt | cut -d" " -f1`
MD5OLD1=`md5sum ChangeLog.old | cut -d" " -f1`

if [ "$MD5NEW1" != "$MD5OLD1" ]; then
mv -fb ChangeLog.txt ChangeLog.old
echo ""
echo "moved Changelog to old, downloading new Changelog" 
elif [ "$MD5NEW1" = "$MD5OLD1" ]; then
rm -f ChangeLog.txt
echo ""
echo "previous changelogs are identical, downloading new changelog" 
fi

if [ "$VER" = "current" ]; then
wget $MIRROR/slackware-$VER/ChangeLog.txt
echo "changelog-$VER downloaded" 
else 
wget $MIRROR/slackware-$VER/patches/ChangeLog.txt
echo "changelog-$VER downloaded" 
fi



MD5NEW2=`md5sum ChangeLog.txt | cut -d" " -f1`
MD5OLD2=`md5sum ChangeLog.old | cut -d" " -f1`


if [ "$MD5NEW2" = "$MD5OLD2" ]; then
echo "" | mail -s "no $VER diff" $MAIL
echo ""
echo "no $VER diff"
fi

if [ "$MD5NEW2" != "$MD5OLD2" ]; then
diff ChangeLog.txt ChangeLog.old > ChangeLog.diff
cat $CFDDIR/ChangeLog.diff | mail -s "changelogs $VER diff" $MAIL 
echo ""
echo "new diff $VER mailed to $MAIL"
fi

