#!/bin/sh

# Trend Autoupdate Script.
# V0.1 Dan Bird. dbird@sghms.ac.uk
# 12/08/2003
# V1.1 Julian Field. mailscanner@ecs.soton.ac.uk
# 22/11/2008

# Make it work on new POSIX systems
export _POSIX2_VERSION=199209

PackageDir=$1
export PackageDir
shift
# Throw away lockfile
shift

TEMPDIR=/tmp/trend-update.$$
# In case it's a link
rm -f $TEMPDIR >/dev/null 2>&1
# In case it's a dir
rm -rf $TEMPDIR >/dev/null 2>&1
# And make the new one
mkdir $TEMPDIR || exit 1
OPRINI=opr.ini.$$
export OPRINI

trap "rm -rf $TEMPDIR" EXIT

# source of IScan dat files
FTPSERV="ftp://ftp.antivirus.com/products/pattern/"

# Get the info on new version...
echo "Checking for latest DAT version..."
# In case it crept into existence
rm -f $TEMPDIR/$OPRINI
wget -q -O $TEMPDIR/$OPRINI $FTPSERV/opr.ini
#NEWVER=`grep PatternVersion /tmp/opr.ini.$$ | sed s/^PatternVersion=//g | cut -c 1-3`
NEWVER=`grep PatternVersionNPF $TEMPDIR/$OPRINI | sed s/^PatternVersionNPF=//g | cut -c 3-5`

# What's out current version?
# For 2006 onwards CURRENTVER=`ls $PackageDir/* | grep lpt | tail -1 |  cut -d. -f 2`
CURRENTVER=`ls -rt $PackageDir/*lpt* /etc/iscan/*lpt* | tail -1 |  cut -d. -f 2`

if [ "$CURRENTVER" = "" ]
then
        CURRENTVER=0
fi
echo "Current version is : $CURRENTVER, new version is : $NEWVER"

fail () {
        trap '' EXIT
        echo "Test run failed -- removing bad Trend data files"
        echo "$OUT"
        rm -f $PackageDir/lpt*$NEWVER*
	rm -rf $TEMPDIR
	logger -p mail.warn -t trend-autoupdate Trend: update failed
        exit 1
}

# If our current one is older, download the new one!!!
# For 2006 onwards if [ $CURRENTVER -lt $NEWVER ]
if [ $CURRENTVER -ne $NEWVER ]
then
        echo "Getting new DAT version : $NEWVER"
	# In case it crept into existence
	rm -f $TEMPDIR/lpt$NEWVER.zip
        wget -q -P $TEMPDIR  $FTPSERV/lpt$NEWVER.zip
        printf "Testing file for corruption...."

        DATCHECK=`unzip -o -t $TEMPDIR/lpt$NEWVER.zip | grep "No errors"`

        if [ "$DATCHECK" != "" ]
        then
                echo "OK"
                mv $TEMPDIR/lpt$NEWVER.zip /etc/iscan
                printf "installing...."
                cd /etc/iscan # $PackageDir
                unzip -q -o lpt$NEWVER.zip
                echo "DONE"
        else
                echo "BAD ARCHIVE"
        fi


        #CMD="$PackageDir/vscan -v 2>&1"
        OUT=`$PackageDir/vscan -v 2>&1`
        case "$OUT" in
                *"read pattern failed"* | \
                *"invalid pattern file"* | \
                *"pattern file not found"* | \
                *"incorrect pattern format"* | \
                *"read error"* | \
                *"out of memory"* )
        fail
        ;;
        esac
        trap '' EXIT
	logger -p mail.info -t trend-autoupdate Trend: DAT files updated from $CURRENTVER to $NEWVER
else
        echo "DAT files are upto date"
	logger -p mail.info -t trend-autoupdate Trend: DAT files are up to date
fi
# In case our dir turned into a link on its own...
rm -f $TEMPDIR >/dev/null 2>&1
rm -rf $TEMPDIR
rm -f  $PackageDir/*.zip
rm -f  /etc/iscan/*.zip
exit 0

