#!/bin/sh
#
# Updates avast! virus database. For use from cron table.
#

AVASTDIR=$HOME/.avast
VPSFILE=$AVASTDIR/400.vps
URL_UPDATE="http://files.avast.com/files/latest/400.vps"
URL_UPDATE_MD5="http://files.avast.com/files/latest/400vps.md5"

progname=`basename $0`
version="1.0.1"
quiet=0

func_usage ()
{
    echo "Usage: $progname [OPTION...]"
    echo "Try '$progname --help' or '$progname -h' for more information."
}

func_help ()
{
    echo "Usage: $progname [OPTION...]"
    echo "$progname -- the avast! Linux Edition VPS file updater"
    echo ""
    echo "Options:"
    echo "   -q, --quiet           Disable output"
    echo "   -h, --help            Give this help list"
    echo "   -V, --version         Print program version"
    echo ""
    echo "Report bugs to <support@avast.com>."
}

func_version ()
{
    echo "$progname: the avast! workstation VPS file updater v$version"
    echo "Copyright (C) 2005-2007. ALWIL Software. All rights reserved"
}

func_update()
{
    if test ! -d $AVASTDIR ; then
        echo "You have to run avast! at least once before you can update it." >&2
        exit 1
    fi

    if test -f $AVASTDIR/lockfile-$USER ; then
        echo "avast! is running, can't update." >&2
        exit 1
    fi

    if test -f $VPSFILE ; then
        md5_current=`md5sum $VPSFILE | cut -c1-32`
    fi

    if test $quiet -eq 0 ; then
        echo "Checking for virus signature updates..." >&2
    fi
    file_md5=$VPSFILE.new.md5
    wget -q -O $file_md5 $URL_UPDATE_MD5 && md5_update=`cat $file_md5 | cut -c1-32`
    rm -f $file_md5

    if test "x$md5_current" != "x$md5_update" ; then
        if test $quiet -eq 0 ; then
            echo "An update for $VPSFILE is available.  Fetching update..." >&2
        fi
        wget -q  -O $VPSFILE.new.$$ $URL_UPDATE && mv -f $VPSFILE.new.$$ $VPSFILE

        if test $quiet -eq 0 ; then
            vpsver=`ls -l $VPSFILE|cut -f6-8 -d' '`
            echo "Update done: $VPSFILE dated $vpsver." >&2
        fi
    else
	if test $quiet -eq 0 ; then
            echo "No updates available at this time." >&2
        fi
    fi
}


while test "$#" -gt 0
do
  arg="$1"
  shift

  case $arg in
      -*=*) optarg=`echo "$arg" | sed -e 's/[-_a-zA-Z0-9]*=//'` ;;
      *) optarg= ;;
  esac

  case "$arg" in
      --quiet | -q )
	  quiet=1 ;;
      --help | -h )
	  func_help; exit 0 ;;
      --version | -V | -v )
	  func_version; exit 0 ;;
  esac
done
func_update

exit 0
