#!/bin/bash
#
# Made by Gert Hulselmans for Ultimate Boot CD
# http:/www.ultimatebootcd.com/
#
# Last time edited: 12 Apr 2010
#
# This script updates the definition files of ClamAV to the last version.

echo
echo "Update definition files of ClamAV to the last version"
echo '_____________________________________________________'
echo
echo

SCRIPTFILE="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")"
SCRIPTDIR=`dirname "$SCRIPTFILE"`
cd "$SCRIPTDIR"

ls '../../../../ubcd/tools/linux/antivirus/clamav.sh' > /dev/null 2>&1

if [ "$?" -eq "0" ]; then

    CLAMAV_TEMPDIR="/tmp/ubcd-temp/clamav"
    mkdir -p "${CLAMAV_TEMPDIR}/definitions/root/clamav/"

    if [ "$?" -eq "0" ]; then
        cd "$CLAMAV_TEMPDIR"
        CLAMAV_TEMPDIR=`echo "$PWD"`
    
        cd "$SCRIPTDIR"
    
        if [ -f "${CLAMAV_TEMPDIR}/clamav-update.html.old" ]; then
            OLDVERSION_MAIN=`grep '<a href="http://db.local.clamav.net/main.cvd">main.cvd</a>' "${CLAMAV_TEMPDIR}/clamav-update.html.old"`
            OLDVERSION_DAILY=`grep '<a href="http://db.local.clamav.net/daily.cvd">daily.cvd</a>' "${CLAMAV_TEMPDIR}/clamav-update.html.old"`
        else
            OLDVERSION_MAIN='notting'
            OLDVERSION_DAILY='notting'
        fi
    
        wget -O "${CLAMAV_TEMPDIR}/clamav-update.html" 'http://www.clamav.net/lang/en/'
    
        NEWVERSION_MAIN=`grep '<a href="http://db.local.clamav.net/main.cvd">main.cvd</a>' "${CLAMAV_TEMPDIR}/clamav-update.html"`
        NEWVERSION_DAILY=`grep '<a href="http://db.local.clamav.net/daily.cvd">daily.cvd</a>' "${CLAMAV_TEMPDIR}/clamav-update.html"`
    
        if [ "${OLDVERSION_DAILY}" = "${NEWVERSION_DAILY}" ]; then
            echo 'No update available.'
        else
            # Save IFS
            SAVED_IFS=$IFS
    
            # Poor man's 'which' command
            prgXZ=''
            IFS=:
            for i in $PATH ; do
                if [ -x "$i/xz" ] ; then
                prgXZ="$i/xz"
                break
                fi
            done

            # Restore IFS
            IFS=$SAVED_IFS
    
            if [ -n "$prgXZ" ]; then
                if [ "${OLDVERSION_MAIN}" != "${NEWVERSION_MAIN}" ]; then
                    echo "Start downloading 'main.cvd' definition file"
                    echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
                    echo
                    wget -O "${CLAMAV_TEMPDIR}/definitions/root/clamav/main.cvd" 'http://db.local.clamav.net/main.cvd'
                    echo 'Download finished.'
                    echo
                    echo
                fi
    
                echo "Start downloading 'daily.cvd' definition file"
                echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
                echo
                wget -O "${CLAMAV_TEMPDIR}/definitions/root/clamav/daily.cvd" 'http://db.local.clamav.net/daily.cvd'
                echo
                echo 'Download finished.'
                echo
                echo
        
                # Remove the old 'clamav-definitions.txz' file
                rm -f './../../../../pmagic/pmodules/clamav-definitions.txz'
        
                echo "Make 'clamav-definitions.txz' file"
                echo '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
        
                # Compress, the definition files of ClamAV
                tar -cv -C "${CLAMAV_TEMPDIR}/definitions/" . | "$prgXZ" -z > "./../../../../pmagic/pmodules/clamav-definitions.txz"
        
                # Rename 'clamav-update.html' to 'clamav-update.html.old', so the next time this script runs,
                # $OLDVERSION_MAIN and $OLDVERSION_DAILY get the values of the last downloaded version.
                mv -f "${CLAMAV_TEMPDIR}/clamav-update.html" "${CLAMAV_TEMPDIR}/clamav-update.html.old"
            else
                echo "xz was not found, install it and try again."
                echo 'Site: http://tukaani.org/xz/'
            fi
        fi
    else
        echo "ERROR: Couldn't create directory '${CLAMAV_TEMPDIR}' for storing temporary files."
    fi
else
    echo "ERROR: clamav.sh isn't run from inside the extracted UBCD directory:"
    echo '       (./ubcd/tools/linux/antivirus/clamav.sh)'
fi

echo
