#! /bin/sh

set -e

DAEMON=/usr/bin/freshclam
NAME=freshclam
DESC="clamav virus database updater"

test -x $DAEMON || exit 0

CLAMAV_CONF_FILE=/etc/clamav/clamav.conf
FRESHCLAM_CONF_FILE=/etc/clamav/freshclam.conf
PIDFILE=/var/run/clamav/freshclam.pid

if [ -e "$FRESHCLAM_CONF_FILE" ]; then
  for variable in `egrep -v '^[[:space:]]*(#|$)' $FRESHCLAM_CONF_FILE | awk '{print $1}'`; do
    value=`grep ^$variable $FRESHCLAM_CONF_FILE | head -n1 | sed -e s/$variable\ *//`
    if ! [ "$value" = "$variable" -o "$value" = "" ]; then
      export "$variable"="$value"
    fi
  done
fi

if [ -f /var/lib/clamav/interface ];then
  INTERFACE=`cat /var/lib/clamav/interface`
else 
  INTERFACE=''
fi

for inet in $INTERFACE; do
  if route | grep -q "$inet"; then
    IS_UP=true
    break
  else
    IS_UP=false
  fi
done

for inet in $INTERFACE; do
  if [ "$inet" = "$IFACE" ]; then
    match=true
    break
  else
    match=false
  fi
done

# We don't want to always start/stop if running from if-up/down.d
if ! [ "$1" = "start" -o "$1" = "stop" ]; then     # Exempt restart/reload/etc from checks
  if [ -n "$INTERFACE" ]; then                     # Want it only run from if-up.d
    if [ -z "$IFACE" ]; then                       # Is not called by if-up.d
      if [ "$IS_UP" = false ]; then                # And route isn't up (e.g., upgrade)
	echo "Interface not up.  Exiting."
	exit 0
      fi
    elif [ "$match" != 'true' ]; then              # IFACE coming up is not the one selected
      echo "Interface not up.  Exiting."
      exit 0
    fi
  fi
fi

# If user wants it run from cron, we only accept no-daemon and stop
if [ -f /etc/cron.d/clamav-freshclam ]; then
  if ! [ "$1" = "no-daemon" -o "$1" = "stop" ]; then
    echo "Cron option has been selected for running freshclam"
    echo "Please either run freshclam directly, or run the init"
    echo "script with the 'no-daemon' option"
    exit 0
  fi
fi

if [ -z "$UpdateLogFile" ]; then
  UpdateLogFile=/var/log/clamav/freshclam.log
fi

if [ -z "$DataDirectory" ]; then
  if [ -r "$CLAMAV_CONF_FILE" ]; then
    DataDirectory=$(grep 'DataDirectory' "$CLAMAV_CONF_FILE" | sed 's@DataDirectory @@')
  fi
  if [ -z "$DataDirectory" ]; then
    DataDirectory=/var/lib/clamav/
  fi
fi

case "$1" in
  no-daemon)
  echo "It takes freshclam ~3min to timeout and try the next mirror in the list"
  freshclam -l "$UpdateLogFile" --datadir "$DataDirectory"
  ;;
  start)
  echo -n "Starting $DESC: $NAME"
  start-stop-daemon -S -x $DAEMON -- -d --quiet -p $PIDFILE
  echo "."
  ;;
  stop)
  echo -n "Stopping $DESC: $NAME"
  start-stop-daemon -o -K -q -R 30 -p $PIDFILE $DAEMON 
  echo "."
  ;;
  restart|force-reload)
  start-stop-daemon -o -K -q -R 30 -p $PIDFILE $DAEMON 
  echo -n "Restarting $DESC: $NAME"
  start-stop-daemon -S -q -x $DAEMON -- -d --quiet -p $PIDFILE
  echo "."
  ;;
  reload)
  echo -n "Reloading $DESC: $NAME"
  start-stop-daemon -o -K -q -s 1 -p $PIDFILE $DAEMON 
  echo "."
  ;;
  skip)
  ;;
  *)
  echo "Usage: $0 {no-daemon|start|stop|reload|restart|force-reload|skip}" >&2
  exit 1
  ;;
esac

exit 0

