#!/bin/sh -e
# Config script for openvas-plugins
# (c) 2003-2007 Javier Fernández-Sanguino <jfs@debian.org>
# Some ideas stolen from the geneweb package (and contributed by
# Christian Perrier)

. /usr/share/debconf/confmodule
db_version 2.0 || [ $? -lt 30 ]
db_title "OpenVAS plugins"


find_updated_plugs () {
  DIST_PLIST=`mktemp`
  ORIG_PLIST=`mktemp`
  cat $OPENVASPLIST | sort > $DIST_PLIST
  find $PLUGINDIR -maxdepth 1 -type f | sort > $ORIG_PLIST
  comm -13 $DIST_PLIST $ORIG_PLIST
  rm -f $ORIG_PLIST $DIST_PLIST
  return 0
}

PLUGINDIR=/var/lib/openvas/plugins/
OPENVASPLIST=/var/lib/dpkg/info/openvas-plugins.list
MAX_ENTRIES=5
[ -d $PLUGINDIR ] && ( [ "$1" = "configure" ] || [ "$1" = "reconfigure" ] ) && {
# We need to remove _all_ the plugins in the previous installation
# otherwise there might be stuff which we do not want

  newplugs="`find_updated_plugs`"
  if [ -n "$newplugs" ] ; then
	  countnewplugs="`echo $newplugs |wc -l`"
  else
	  countnewplugs=0
  fi
  if [ -n "$newplugs" ] || [ "$1" = "reconfigure" ] ; then

# I'm not convinced that there is any need to list all the plugins
# which are new here. This question could even be asked regardless
# of wether there are new plugins or not... (jfs)
#  	if [ $countnewplugs -gt $MAX_ENTRIES ] ; then
	  # List should be limited to, say, 5 entries at most to avoid
	  # filling up the screen
#	  newplugs="`echo $newplugs |head -$MAX_ENTRIES`"
#	  newplugs="$newplugs (...)"
#	fi
#	newplugs=`echo $newplugs | perl -pe 's/\n/, /g'` 
#  	db_subst openvas-plugins/remove_unknown newplugs "$newplugs" || true

# This will give an indication of the stuff that will be removed
  	db_subst openvas-plugins/remove_unknown countnewplugs "$countnewplugs" || true
# Ask the question
  	db_input medium openvas-plugins/remove_unknown || true
  fi
}

#DEBHELPER#

db_go

exit 0
