#!/bin/sh


DAYOFWEEK=`date +%w`
DAYSTAMP=`date +%d%b%Y`

#export http_proxy=
#export ftp_proxy=


SOPHOSDIR="/usr/local/sav"

export SAV_IDE="$SOPHOSDIR/tmp"

cd $SOPHOSDIR >> $SOPHOSDIR/update_sophos-$DAYOFWEEK.log 2>&1
mkdir -p tmp old >> $SOPHOSDIR/update_sophos-$DAYOFWEEK.log 2>&1
cd tmp > $SOPHOSDIR/update_sophos-$DAYOFWEEK.log 2>&1
cp -f $SOPHOSDIR/vdl.dat .
rm -f *.ide
wget --header="Pragma: no-cache" -q http://www.sophos.com/downloads/ide/ides.zip > $SOPHOSDIR/update_sophos-$DAYOFWEEK.log 2>&1
if [ ! -f "ides.zip" ]; then
	echo "Something's wrong - couldn't find primary download site, exiting..." > $SOPHOSDIR/update_sophos-$DAYOFWEEK.log 2>&1
	exit
fi

FILE="ides.zip"

#looks like it's up!
CURRENT=`/bin/ls -trc $SOPHOSDIR/*ides.zip 2>/dev/null|tail -1`
if [ "$CURRENT" != "" ]; then
if [ "`cat $CURRENT |md5sum `" = "`cat $FILE|md5sum `"  ]; then
	 echo "No change - exiting"  > $SOPHOSDIR/update_sophos-$DAYOFWEEK.log 2>&1
	 rm -f $FILE
	 exit
fi
fi

#Great, rename current and carry on
mv -f $FILE $DAYSTAMP-$FILE
FILE="$DAYSTAMP-$FILE"

unzip -t $FILE >/dev/null 2>&1
if [ "$?" != "0" ]; then
	 echo "Downloaded zip file - but it's corrupt - exiting..."  > $SOPHOSDIR/update_sophos-$DAYOFWEEK.log 2>&1
	 #rm -f $FILE
	 exit
else
	echo "Unpacked ZIP file - now test..."  > $SOPHOSDIR/update_sophos-$DAYOFWEEK.log 2>&1
fi
#sweet. Copy old dat files away - don't rename as new zip file
#may not contain all the dat files!!!!


#Now unzip zip file into tmp dir for test run
unzip -oL $FILE >/dev/null 2>&1
#
#Now run sophos against test virus to ensure it's working

#Create test virus file
echo 'X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*' > eicar.com
DD=`sweep -all -ss -nb -archive eicar.com`  > $SOPHOSDIR/update_sophos.log 2>&1
rm -f eicar.com
if [ "`echo $DD|grep \"Virus 'EICAR-AV-Test' found in file eicar.com\"`" = '' ]; then
	echo "Didn't detect EICAR test virus - Email admin - exiting"  >> $SOPHOSDIR/update_sophos-$DAYOFWEEK.log 2>&1
	(cat<<EOF
From: Virus Scanner Downloader <root>
To: virus-reports@xxx
Subject: Bad ZIP file of Sophos DAT files

$FILE contains DAT files that do not recognise the EICAR.COM test virus. As
such the DAT files will NOT go live.

Transaction log follows:

EOF
cat $SOPHOSDIR/update_sophos-$DAYOFWEEK.log
)|/usr/sbin/sendmail -oi -t
	exit
else
	echo "Passed EICAR test - move new DAT files live"
	#Joy - everything is sweeeeet! Move files live
	GOODFILES="`/bin/ls *.ide $FILE`"
	chmod 755 $GOODFILES
	mv -f $GOODFILES $SOPHOSDIR/
	chmod 755 $SOPHOSDIR/*
	REGEX="`echo $GOODFILES|sed 's/ /|/g'`"
	OLDFILES="`/bin/ls $SOPHOSDIR|egrep -v \"$REGEX\"|egrep \"\.ide\"`"
	echo "OLDFILES=$OLDFILES"
	cd $SOPHOSDIR
	mv -f $OLDFILES $SOPHOSDIR/old/
	#Clean out old files
	find $SOPHOSDIR  -mtime +14 \( -name '*.zip' -o  -name 'lpt*-*' \) -exec rm -f {} \;
	
	#Send Email notification
	        (cat<<EOF
From: Virus Scanner Downloader <root>
To: virus-reports@xxx
Subject: Sophos update to $FILE has occurred

A new release of DAT files for Sophos have been downloaded and installed
on the Qmail-Scanner systems:

$FILE

EOF
)|/usr/sbin/sendmail -oi -t
	
fi
