#!/bin/sh -e
# Postinst script for openvas-plugins
# (c) 2003-2007 Javier Fernández-Sanguino <jfs@debian.org>

. /usr/share/debconf/confmodule
db_version 2.0
test $DEBIAN_SCRIPT_DEBUG && set -v -x


# Note, This is far from optimal and might take some time to run.
# (and we have to do it again since we cannot preserve it through
# configure/postinst calls)
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
if [ "$1" = "configure" ] && [ -d $PLUGINDIR ]; then
	db_get openvas-plugins/remove_unknown || true
	REMOVE="$RET"
	if [ "$REMOVE" = "true" ] ; then
		newplugs="`find_updated_plugs`"
		if [ -n "$newplugs" ] ; then
			echo $newplugs | xargs -r rm -f
		fi
	fi
fi

# Revert the change introduced in 2.0.10a-4, since
# now all plugins are in /var/
if [ -d /var/lib/openvas/.desc-plugins ] && \
   [ -d /var/lib/openvas/plugins ] ; then
   	[ -L /var/lib/openvas/plugins/.desc ] && rm -f /var/lib/openvas/plugins/.desc
   	if [ ! -e /var/lib/openvas/plugins/.desc ]; then
		mv /var/lib/openvas/.desc-plugins /var/lib/openvas/plugins/.desc
	fi
fi

#DEBHELPER#

exit 0
