#!/bin/sh
#---------------------------------------------------------------------------
#
# Copyright (c) 2004 Hellmuth Michaelis
# Copyright (c) 2001 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 new virus scanner version from sophos.com
#	=============================================
#
#	This shell script automates the download of new virus scanners
#	from Sophos (http://www.sophos.com).
#
#	This script is FreeBSD (http://www.freebsd.org) specific and
#	postfix (http://www.postfix.org) is used as the MTA !!
#
#	Feedback welcome!
#
#	last edit-date: [Sat Jan 31 20:50:48 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 !!!

USERPASS="<userid>:<password>"
MAILADDR="postmaster@localhost"

# end of configuration section

ARCHIVE="freebsd.elf.tar.Z"
URL="http://$USERPASS@www.sophos.com/sophos/products/full/$ARCHIVE"
SOPHDIR=/usr/local/sophos
INSTALLDIR=sav-install
SAVDIR=/usr/local/sav
BAKDIR=previous
MACHINE=`hostname`

cd $SOPHDIR

if [ ! -d $BAKDIR ]
then
	mkdir $BAKDIR
else
	rm -rf $BAKDIR
	mkdir $BAKDIR
fi

mv -f * $BAKDIR

fetch -q -T3600 -a $URL

if [ ! $? ]
then

# restore backup dir 

	mv -f $BAKDIR/* .

# send mail and exit

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

             fetch $URL failed, terminating!

             Sincerly yours,
                   the get_sophos_update script on $MACHINE

ENDOFDATA
		exit 1
fi

# unpack scanner

tar xvzf $ARCHIVE
if [ ! $? ]
then

# restore backup dir 

	mv -f $BAKDIR/* .

# send mail and exit

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

             tar xvzf $ARCHIVE failed, terminating!

             Sincerly yours,
                   the get_sophos_update script on $MACHINE

ENDOFDATA
		exit 1
fi

# stop mailer

/usr/local/sbin/postfix stop

sleep 10

# move old ide's away

cd $SAVDIR

if [ ! -d $BAKDIR ]
then
	mkdir $BAKDIR
else
	rm -rf $BAKDIR
	mkdir $BAKDIR
fi

mv -f * $BAKDIR

cd $SOPHDIR/$INSTALLDIR

# install new virus scanner

./install.sh -v >install.sh.log 2>&1 

# start mailer

/usr/local/sbin/postfix start

# send mail 

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

             The Sophos Virus Database was successfully updated!

             Sincerly yours,
                   the get_sophos_update script on $MACHINE

ENDOFDATA
exit 0
