#! /bin/bash

# Copyright 2004, 2005, 2007  Alexandre Oliva  <aoliva@redhat.com>
# version pi.1

# 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 3 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, see <http://www.gnu.org/licenses/>.

# This script checks for multiply-installed packages in the RPM
# database and lists them.  Exceptions are gpg-pubkey and kernel,
# that are always excluded from the list.  egrep regular expressions
# can be passed in the command line and will exclude further packages.
# The expressions must match the full package name for a package to be
# excluded.  For example:

#  root# .../rpmdup-install mypkg mypkg-devel mypkg-plugin-.\*

# This will NOT exclude mypkg-extras.

list=`rpm -qa --queryformat '%{name}/%{arch}\n' | sort`
duplist=`diff -C0 <(echo "$list" | uniq) <(echo "$list") | sed -n 's,^[+] ,,p'`

for exclude in gpg-pubkey kernel kernel-smp kernel-kdump kernel-PAE \
 kernel-xen kernel-xen0 kernel-xenU ${1+"$@"}; do
  duplist=`echo "$duplist" | egrep -v "^($exclude)(/.*)?$"`
done

if test -z "$duplist"; then
  echo no duplicate packages installed
  exit 0
fi

list=
for pkg in $duplist; do
  name=`echo $pkg | sed 's,/.*,,'`
  arch=`echo $pkg | sed 's,.*/,,'`
  list="$list
"`rpm -q --queryformat '%{name}-%{version}-%{release}.%{arch}.rpm\n' $name |
    grep "\.$arch\.rpm$"`
done

echo packages to reinstall: "$list"
duplist=`echo $list`
