#!/bin/sh
# Audit plugins removed from a given Nessus plugin package to another
# 
# TODO: check if they were removed because the included files are no
# longer available

set -e
OLDVERSION=2.2.3
NEWVERSION=2.2.10

OLD=`find . -name "nessus-plugins-$OLDVERSION" -type d`
[ -z "$OLD" ] && {  echo "ERR: Cannot find plugins directory for $OLDVERSION" ; exit 1 ; }
OLD=$OLD/scripts
NEW=`find . -name "nessus-plugins-$NEWVERSION" -type d`
[ -z "$NEW" ] && { echo "ERR: Cannot find plugins directory for $NEWVERSION" ; exit 1 ; }
NEW=$NEW/scripts
CVS=/home/jfs/debian/security/nessus/cvs/nessus-plugins/scripts/

# From OLD to NEW
find $OLD -type f | 
while read file ; do
	plug=`basename $file`
	cvsfile=$CVS/$plug
	if [ ! -e $NEW/$plug ] ; then
		echo -n "REMOVED: $plug "
		if [ -e $CVS/$plug ] ; then
			echo -n "[in CVS]"
			if egrep "\([cC]\) .*Tenable" $cvsfile >/dev/null; then
				if ! egrep "\([cC]\) .*Tenable" $file >/dev/null; then
					echo -n "[NEW (C) Tenable, OLD not]"
				fi
			fi
		else
			echo -n "[unavailable in CVS]"
		fi
		# Check copyright
		if grep "Noam Rathaus" $file >/dev/null; then
			echo -n "[maybe (C) nrathaus]"
		fi
		if egrep "\([cC]\) .*Tenable" $file >/dev/null; then
			echo -n "[OLD (C) Tenable]"
		fi
			
		# Extract includes
		tmpfile=`tempfile` || { echo "ERR: Cannot create temporary file!"; exit 2; } 
		egrep "^include.*(.*).*;" $OLD/$plug | sed -e 's/^.*(.\(.*\.inc\).).*$/\1/g' | 
		while read incfile; do 
			if [ ! -e "$NEW/$incfile" ] ; then 
				echo $incfile >>$tmpfile
			fi
		done 
		if [ -s "$tmpfile" ] ; then
			incfiles=`cat $tmpfile`
			echo -n " [included files no longer available: $incfiles]"
		fi
		rm -f $tmpfile
		echo 
	fi
done 


# and viceversa
find $NEW -type f |
while read file ; do
	plug=`basename $file`
	if [ ! -e $OLD/$plug ] ; then
		echo -n "ADDED: $plug "
		if [ -e $CVS/$plug ] ; then
			echo -n "[in CVS]"
		else
			echo -n "[unavailable in CVS]"
		fi
	echo
	fi
done


exit 0
