#!/bin/sh

# debian_advisory_check.sh
# Copyright 2004 Luke Plant <L.Plant.98@cantab.net>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

##########################################################################
# To be run on an e-mail containing a Debian security advisory
# Exit code 0 if the package affected is installed
# Exit code 1 otherwise

# Should be run using a test condition in sylpheed:
# debian_security_check.sh %F

# Get package name:
PACKAGES=`egrep 'Package *:' $1`

if [ $? -ne 0 ]
then 
	exit 1
fi

# FIXME - multiple packages possible?

PACKAGES=`echo $PACKAGES | cut -f 2 -d ':' | tr -d ' ' | tr ',' ' '`

for PACKAGE in $PACKAGES
do
	# Check if it's installed
	cat /var/lib/dpkg/status | grep "Package: $PACKAGE" -A1 | egrep 'Status:.* installed' > /dev/null && exit 0

	# Sometimes the source name is used
	cat /var/lib/dpkg/status | grep "Source: $PACKAGE" -B6 | egrep 'Status:.* installed' > /dev/null && exit 0

	# Check for virtual packages
	cat /var/lib/dpkg/status | egrep "Provides:.*[, ]$PACKAGE" -B9 | egrep 'Status:.* installed' > /dev/null && exit 0

done
exit 1

