#!/bin/sh
#---------------------------------------------------------------------------
#
# Copyright (c) 2004 Hellmuth Michaelis
# Copyright (c) 2001, 2003 HCS Hanseatischer Computerservice GmbH
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#---------------------------------------------------------------------------
#
#	get ide's from sophos.com
#	=========================
#
#	This shell script automates the download of IDE's for the
#	virus scanner manufactured by Sophos (http://www.sophos.com).
#
#	This script is FreeBSD (http://www.freebsd.org) specific !!
#
#	Feedback welcome!
#
#	last edit-date: [Sat Jan 31 20:49:29 2004]
#
#	Hellmuth Michaelis IT Beratung und Dienstleitung
#	Hallstraße 20
#	25462 Rellingen
#	Mail: hm@freebsd-support.de
#	Tel: +49 (0)4101 / 85 299 20
#
#---------------------------------------------------------------------------

# configure me !!!

MAILADDR="postmaster@localhost"
HTTP_PROXY=192.168.169.170:8000
export HTTP_PROXY

# end of configuration section

URL="http://www.sophos.com/downloads/ide"
SAVDIR=/usr/local/sav
WORKDIR=$SAVDIR/update
MACHINE=`hostname`
PVERSION=`/usr/local/bin/sweep -v | grep "Product version" | awk '{print $4}' | tr -d -c [:digit:]`
LIST_FILE="${PVERSION}_list.txt"
NEW_FILE="${PVERSION}_list.new"
IDES_FILE="${PVERSION}_ides.zip"

# create working directory in case its not there
#---------------------------------------------------------------------------
if [ ! -d $WORKDIR ]
then
	mkdir $WORKDIR
fi

# change to working dir
#---------------------------------------------------------------------------
cd $WORKDIR

# if the text list file is locally not available, fetch the ide zip file
#---------------------------------------------------------------------------
if [ ! -s $WORKDIR/$LIST_FILE ]
then
	fetch $URL/$IDES_FILE
	if [ $? -ne 0 ]
	then

# FAILED, send mail and exit
#---------------------------------------------------------------------------

cat << ENDOFDATA | mail -s "Virus Database Update ERROR" $MAILADDR

             fetch $URL/$IDES_FILE failed, terminating!

             Sincerly yours,
                   the get_sophos_ide script on $MACHINE

ENDOFDATA
		# clean up 
		rm *

		exit 1
	fi

# fetch the text list file of ide's
#---------------------------------------------------------------------------

	fetch $URL/$LIST_FILE
	if [ $? -ne 0 ]
	then

# FAILED, send mail and exit
#---------------------------------------------------------------------------

cat << ENDOFDATA | mail -s "Virus Database Update ERROR" $MAILADDR

             fetch $URL/$LIST_FILE failed, terminating!

             Sincerly yours,
                   the get_sophos_ide script on $MACHINE

ENDOFDATA
		# clean up 
		rm *

		exit 1
	fi

# the text list file is locally available, fetch a possibly NEW version
#---------------------------------------------------------------------------
else
	fetch -o $NEW_FILE $URL/$LIST_FILE
	if [ $? -ne 0 ]
	then

# FAILED, send mail and exit
#---------------------------------------------------------------------------

cat << ENDOFDATA | mail -s "Virus Database Update ERROR" $MAILADDR

             fetch -o $NEW_FILE $URL/$LIST_FILE failed, terminating!

             Sincerly yours,
                   the get_sophos_ide script on $MACHINE

ENDOFDATA
		# clean up 
		rm *

		exit 1
	fi

# compare the old and the new text list file
#---------------------------------------------------------------------------

# compare file checksums

	md5new=`md5 -q $NEW_FILE`
	md5old=`md5 -q $LIST_FILE`

	if [ $md5old = $md5new ]
	then

# verify ide files are in place
# ---------------------------------------------------------------------------

		extract=0
		
		while read -r line
		do 
			base=`basename $line`
			if [ ! -r $SAVDIR/$base ]
			then
				echo $base not found!
				extract=1
			fi
		done < $LIST_FILE
		
		if [ $extract -ne 0 ]
		then
			unzip $IDES_FILE
			mv *.ide $SAVDIR
			chmod a-x $SAVDIR/*.ide
		fi

# files are same, exit
#---------------------
		exit 0
	fi

# files differ, mv new to old text list file
#-------------------------------------------
	mv -f $NEW_FILE $LIST_FILE

# fetch new ide's zip file
#-------------------------
	fetch $URL/$IDES_FILE
	if [ $? -ne 0 ]
	then

# FAILED, send mail and exit
# ---------------------------------------------------------------------------
cat << ENDOFDATA | mail -s "Virus Database Update ERROR" $MAILADDR

             fetch new $URL/$IDES_FILE failed, terminating!

             Sincerly yours,
                   the get_sophos_ide script on $MACHINE

ENDOFDATA
		# clean up 
		rm *

		exit 1
	fi
fi

# unzip and install new ide files
#---------------------------------------------------------------------------

unzip $IDES_FILE
mv *.ide $SAVDIR
chmod a-x $SAVDIR/*.ide

# GOOD, send mail and exit
#---------------------------------------------------------------------------

cat << ENDOFDATA | mail -s "Virus Database Updated" $MAILADDR

             The Sophos Virus Database was successfully updated!

             Sincerly yours,
                   the get_sophos_ide script on $MACHINE

ENDOFDATA
exit 0
