#!/bin/sh

SCANNERSCONF=/usr/local/etc/MailScanner/virus.scanners.conf

#LOCKFILE=/var/lock/MailScanner.autoupdate.lock
LOCKFILE=/tmp/MailScanner.autoupdate.lock

# the lockfile is not meant to be perfect, it's just in case the
# two cron scripts get run close to each other to keep them from
# stepping on each other's toes.
[ x`perl -e 'print "young" if -f "'$LOCKFILE'" && -M "'$LOCKFILE'"<0.042'` = "xyoung" ] && exit 0
#[ -f $LOCKFILE ] && [ "`find $LOCKFILE -type f -mmin +60 -print`" = "" ] && exit 0
trap "rm -f $LOCKFILE" EXIT
touch $LOCKFILE

# Set umask so user mail can read (and share-lock) the Busy.lock files
umask 022

# Solaris requires xpg4 not normal grep for "-e" option
OS=`uname`
if [ "$OS" = "SunOS" ]; then
  GREP=/usr/xpg4/bin/grep
else
  GREP=grep
fi

cat ${SCANNERSCONF} | sed -e 's/#.*$//' | ${GREP} -e -wrapper | \
while read NAME WRAPPER PACKAGEDIR
do
  #echo String is $NAME $WRAPPER $PACKAGEDIR
  UPDATER=`echo $WRAPPER | sed -e 's/-wrapper$/-autoupdate/'`
  if [ -n "${NAME}" -a -n "${WRAPPER}" -a -n "${PACKAGEDIR}" ]
  then
    if ${WRAPPER} "${PACKAGEDIR}" -IsItInstalled
    then
      #echo Found $NAME installed
      logger -p mail.info -t update.virus.scanners Found $NAME installed
      if [ -x ${UPDATER} ]
      then
        #echo Updating $NAME
        logger -p mail.info -t update.virus.scanners Running autoupdate for $NAME
        ${UPDATER} "${PACKAGEDIR}" >/dev/null 2>&1
      fi
    fi
  fi
done

exit 0
