#!/bin/sh

set -e

# Source debconf library
. /usr/share/debconf/confmodule

# This conf script is capable of backing up
db_version 2.0
db_capb backup

# Get user config

FRESHCLAMCONF='/etc/clamav/freshclam.conf'

if [ -n "$http_proxy" ];then 
  db_set clamav-freshclam/http_proxy "$http_proxy" || true
fi

#COMMON-FUNCTIONS#

slurp_config "$FRESHCLAMCONF"

# Set debconf values from config file 

[ -f /var/lib/clamav/interface ] && Interface=`cat /var/lib/clamav/interface`
if [ -n "$Interface" ]; then
  db_set clamav-freshclam/autoupdate_freshclam ifup.d || true
fi
if [ -n "$Interface" ]; then
  db_set clamav-freshclam/internet_interface "$Interface" || true
fi
if [ -n "$DatabaseMirror" ]; then
  if [ -e /usr/share/doc/clamav-freshclam/mirror-list.gz ]; then
    if zgrep -q "$DatabaseMirror" /usr/share/doc/clamav-freshclam/mirror-list.gz;then
      db_set clamav-freshclam/local_mirror `zgrep "$DatabaseMirror" /usr/share/doc/clamav-freshclam/mirror-list.gz` || true
    else
      db_set clamav-freshclam/local_mirror "$DatabaseMirror" || true
    fi
  fi
fi
if [ -n "$HTTPProxyServer" ]; then
  db_set clamav-freshclam/http_proxy "http://$HTTPProxyServer:$HTTPProxyPort/" || true
fi
if [ -n "$HTTPProxyUsername" ]; then
  db_set clamav-freshclam/proxy_user "$HTTPProxyUsername:$HTTPProxyPassword" || true
fi
if [ -n "$Checks" ]; then
  db_set clamav-freshclam/update_interval "$Checks" || true
fi
if [ -n "$NotifyClamd" ]; then
  db_set clamav-freshclam/NotifyClamd "$NotifyClamd" || true
fi

# States

StateInit()
{
  STATE="autoupdate_freshclam"
}

Stateautoupdate_freshclam()
{
  db_input medium clamav-freshclam/autoupdate_freshclam || true
  if db_go; then
    db_metaget clamav-freshclam/autoupdate_freshclam value || true
    if [ "$RET" = "ifup.d" ]; then
      STATE="internet_interface"
    else
      STATE="local_mirror"
    fi
  else
    STATE="End"
  fi
}

Stateinternet_interface()
{
  db_input high clamav-freshclam/internet_interface || true
  if db_go; then
    STATE="local_mirror"
  else
    STATE="autoupdate_freshclam"
  fi
}

Statelocal_mirror()
{
  db_input medium clamav-freshclam/local_mirror || true
  if ! db_go; then
    STATE="autoupdate_freshclam"
  else
    db_metaget clamav-freshclam/local_mirror value || true
    if [ -z "$RET" ]; then
      db_set clamav-freshclam/local_mirror 'db.local.clamav.net' || true
    else
      STATE="http_proxy"
    fi
  fi
}

Statehttp_proxy()
{
  db_input medium clamav-freshclam/http_proxy || true
  if ! db_go; then
    STATE="local_mirror"
  else
    db_metaget clamav-freshclam/http_proxy value || true
    if [ -z "$RET" ]; then
      STATE="update_interval"
    else
      STATE="proxy_user"
    fi
  fi
}

Stateproxy_user()
{
  db_input medium clamav-freshclam/proxy_user || true
  if ! db_go; then
    STATE="http_proxy"
  else
    STATE="update_interval"
  fi
}

Stateupdate_interval()
{
  db_input low clamav-freshclam/update_interval || true
  if ! db_go; then
    STATE="http_proxy"
  else
    db_metaget clamav-freshclam/update_interval value || true
    if [ -z "$RET" ]; then
      db_set clamav-freshclam/update_interval 12 || true
    fi
    STATE="notify_daemon"
  fi
}

Statenotify_daemon()
{
  db_input medium clamav-freshclam/NotifyClamd || true
  if ! db_go; then
    STATE="update_interval"
  else
    STATE="End"
  fi
}

# This is the statemachine that controls execution. All the 'real' work is 
# performed by subfunctions above. 

STATE="Init"
while [ "$STATE" != "End" ]; do
  case "$STATE" in
    Init)
    StateInit
    ;;
    autoupdate_freshclam)
    Stateautoupdate_freshclam
    ;;
    local_mirror)
    Statelocal_mirror
    ;;
    http_proxy)
    Statehttp_proxy
    ;;
    proxy_user)
    Stateproxy_user
    ;;
    internet_interface)
    Stateinternet_interface
    ;;
    update_interval)
    Stateupdate_interval
    ;;
    notify_daemon)
    Statenotify_daemon
    ;;
  esac
done
db_stop || true
exit 0
