#!/bin/sh


#Utils that are needed are listed below
xlogger=`which logger`
xlogger="/usr/bin/logger -i"
xncftpget=`which ncftpget`
xncftpget="/usr/bin/ncftpget"
xwget=`which wget`
xwget="/usr/bin/wget"
xunzip=`which unzip`
xunzip="/usr/bin/unzip"
xmail=`which mail`
xmail="/bin/mail"
#xavpupdater=`which AvpUpdater`
xavpupdater="/opt/AVP/AvpUpdater"

#Anti-Virus Update Sites are listed below
sophosdl="http://www.sophos.com/downloads/ide/ides.zip"
sophosinstall="/usr/local/sav/"
avpdl="ftp://ftp.avp.ru/updates/*"
avpinstall="/opt/AVP/update"
uvscandl="ftp://ftp.nai.com/pub/datfiles/english/dat-*.zip"
uvscaninstall="/usr/local/uvscan/"

#Administers Mailbox
admin="evrim@envy.com.tr"

#Temporary Directory Location
tempdir="/tmp/virus-temp"

echo $xlogger $xncftpget $xwget $xunzip $xmail $xavpupdater $xsophosdl
echo $sophosinstall $avpdl $avpinstall $uvscandl $uvscaninstall
echo $admin $tempdir
#Let's start
$xlogger "Anti-Virus Update Started"
#Checking whether the temporary directory is accessible
if [ ! -d $tempdir ]; then
    mkdir $tempdir
    if [ ! -w $tempdir ]; then
    $xlogger "Cannot create temporary directory $tempdir"
    exit 1
    fi
elif [ ! -w $tempdir ]; then
    $xlogger "Temporary directory $tempdir exists but it is not writable to me!"
    exit 1
fi
echo "tempdircheck passed"
#Checking for utils whether they exists or not
if [ ! -x $xwget ]; then                                                                 
    $xlogger "Anti-virus update failed: reason wget not found" 
#    cat <<EOF | $xmail -s "Virus update failed!" $admin
#    Anti-virus update failed: /usr/bin/wget not found.
#    EOF
    exit 1                                                                                      
fi             
echo "wget check passed"
if [ ! -x $xncftpget ]; then                                                                 
    #echo "$0: ncftpget must be installed for the updater to run" >&2                                
    $xlogger "Anti-virus update failed: reason ncftpget not found" 
#    cat <<EOF | $xmail -s "Virus update failed!" $admin
#    Anti-virus update failed: /usr/bin/ncftpget not found.
#    EOF
    exit 1                                                                                      
fi             
echo "starting now ..."

#Start updating sophos
sophosfail=0
cd $tempdir
rm -rf *
$xwget -q $sophosdl
if [ $? -eq 0 ]; then
$xlogger "Anti-virus Sophos dowload successful" 
    $xunzip -qq -o -d $sophosinstall ides.zip
    if [ $? -eq 0 ]; then 
    $xlogger "Sophos installation successfull"
    else
    $xlogger "Sophos install failed"
#    cat <<EOF | $xmail -s "Virus update failed!" $admin
#    Anti-virus update failed: Sophos installation failed
#    EOF
    sophosfail=1
    fi
else
$xlogger "Anti-virus Sophos download failed" 
#cat <<EOF | \
#$xmail -s "Virus update failed!" $admin
#Anti-virus update failed: Sophos Download Failed
#EOF
sophosfail=1
fi
echo "where am i now??"
#Start updationg McAfee UvScan
uvscanfail=0
cd $tempdir
rm -rf *
$xncftpget -V $uvscandl
if [ $? -eq 0 ]; then
$xlogger "Anti-virus McAfee Uvscan dowload successful"
    $xunzip -qq -o dat*.zip -d $uvscaninstall
    if [ $? -eq 0 ]; then 
    $xlogger "McAfee Uvscan installation successfull"
    else
    $xlogger "McAfee Uvscan install failed"
#    cat <<EOF | $xmail -s "Virus update failed!" $admin
#    Anti-virus update failed: McAfee Uvscan installation failed
#    EOF
    uvscanfail=1
    fi
else
$xlogger "Anti-virus McAfee Uvscan download failed"
#cat <<EOF | $xmail -s "Virus update failed!" $admin
#Anti-virus update failed: McAfee Uvsan Download Failed
#EOF
uvscanfail=1
fi
                                                                                            
#Start Updating AVPLinux
avpfail=0
cd $tempdir
rm -rf *
$xwget -q $avpdl
if [ $? -eq 0 ]; then
echo "AvpLinux Download successful!"
$xlogger "Anti-virus AvpLinux dowload successful"
    $xavpupdater -ud=$tempdir -b=/opt/AVP -o -y                                                               
    if [ $? -eq 0 ]; then
    $xlogger "AvpLinux installation successfull"
    else
    $xlogger "AvpLinux install failed"
#    cat <<EOF | $xmail -s "Virus update failed!" $admin
#    Anti-virus update failed: AvpLinux installation failed
#    EOF
    avpfail=1
    fi
else
echo "AvpLinux download Failed!"
$xlogger "Anti-virus AvpLinux download failed"
#cat <<EOF | $xmail -s "Virus update failed!" $admin
#Anti-virus update failed: AvpLinux Download Failed
#EOF
avpfail=1
fi

#cleaning.
#rm -rf $tempdir
$xlogger "Updates: Sophos=$sophosfail, AVP=$avpfail,Uvscan=$uvscanfail"
$xlogger "Anti-Virus Update ends"
													     