#!/bin/sh
# Read standard input and filter out files which match provided 'extrafiles' patterns

LEFT=''
if [ `ls -l /var/lib/dpkg/info/*.extrafiles 2>/dev/null | wc -l` -ge 1 ]; then
	LEFT='/var/lib/dpkg/info/*.extrafiles'
fi

MIDDLE=''
if [ `ls -l /etc/cruft/filters/* 2>/dev/null | wc -l` -ge 1 ]; then
	MIDDLE='/etc/cruft/filters/*'
fi

RIGHT=''
if [ `ls -l /usr/lib/cruft/filters/* 2>/dev/null | wc -l` -ge 1 ]; then
	for f in /usr/lib/cruft/filters/*
	do
		pkg=`basename $f`
		if [ -f /var/lib/dpkg/info/$pkg.list ] || \
		   [ -f /var/lib/dpkg/info/$pkg.prerm ] || \
		   [ -f /var/lib/dpkg/info/$pkg.postrm ]
		then
			# dpkg can still remove (some of) this package.
			# Therefore its associated files are not yet cruft and
			# may be filtered.
			RIGHT="$RIGHT/usr/lib/cruft/filters/$pkg "
		else
			# dpkg can't remove any of this package.  Any
			# associated files are probably cruft and should not be
			# filtered here (they might still be filtered by
			# another, installed package.)
			:
		fi
	done
fi

/usr/lib/cruft/filter_shell $LEFT $MIDDLE $RIGHT

