#!/usr/bin/env bash
CURL="/usr/bin/curl --fail"
DIFF="/usr/bin/diff"
CAT="/bin/cat"
LOGGER="/usr/bin/logger -p INFO -t UDSIG-update"
UDSIG=udwhitelist.ign2
WKDIR=/tmp
CLAMAVDB=`/bin/grep DatabaseDirectory /etc/clamd.conf|awk '{print $2}'`
CLAMDPID=`/sbin/pidof clamd`
if [ $? -ne 0 ]; then
   CLAMDPID=-1
fi

DBURL="http://repo.virtualhosting.hk/clamavdb/$UDSIG"
# grab the signatures from repository
$CURL $DBURL 2>/dev/null > $WKDIR/$UDSIG
RTNVAL=$?
if [ $RTNVAL -ne 0 ]; then
   $LOGGER "Download failed or File not found in repository!"
   exit $RTNVAL
fi

if [ -e $CLAMAVDB/$UDSIG ]; then
        $DIFF $WKDIR/$UDSIG $CLAMAVDB/$UDSIG 2>&1 >/dev/null
        RTNVAL=$?
        if [ $RTNVAL -eq 0 ]; then
           $LOGGER "No update!"
           exit $RTNVAL
        fi
fi

$LOGGER "Replace $UDSIG"
$CAT $WKDIR/$UDSIG > $CLAMAVDB/$UDSIG

if [ $CLAMDPID -gt 0 ]; then
   /bin/kill -USR2 $CLAMDPID
   $LOGGER "Done! CLAMD will re-read database!"
else
   $LOGGER "Done! But CLAMD is dead!"
fi
