#!/bin/sh
#
# Filter to exclude some Requires targets from those automatically
# generated by rpmbuild's helper script find-requires
#
# Specifically intended for Perl modules that PCP uses, but which
# may not be packaged in some distributions.
#

_usage()
{
    echo >&2 "Usage: $0 [option] path-to-real-find-requires [arg]"
    echo >&2 "Options:"
    echo >&2 " -f fedora_version"
    echo >&2 " -r redhat_version"
    echo >&2 " -v vendor (\"redhat\" or \"suse\" or ...)"
}

#debug# echo >&2 "$0 called as: $0 $*"

FEDORA=0
REDHAT=0
VENDOR=unknown
while getopts "f:r:v:?" c
do
    case $c
    in
	f)
	    FEDORA="$OPTARG"
	    ;;
	r)
	    REDHAT="$OPTARG"
	    ;;
	v)
	    VENDOR="$OPTARG"
	    ;;
    esac
done
shift `expr $OPTIND - 1`

if [ $# -lt 1 ]
then
    _usage
    exit 1
fi

if [ ! -f "$1" ]
then
    echo "$0: Error: rpm script $1 not found"
    exit 1
fi

# Filtering depends on distro vendor and possibly version
#
# Lines from find-requires look like this ...
# /bin/sh
# libc.so.6()(64bit)
# libc.so.6(GLIBC_2.2.5)(64bit)
# perl(Spreadsheet::Read)
# perl(strict)
#
case "$VENDOR"
in
    redhat)
	# See RedHat BZ 830923 and BZ 754678 for Spreadsheet::Read
	# issues.
	# Does not seem to matter what version of RH or Fedora.
	# and CentOS sees an issue with /usr/bin/sh
	$* \
	| sed \
	    -e '/^perl(Spreadsheet::Read)$/d' \
	    -e '/^\/usr\/bin\/sh$/d'
	;;
    *)
	$*
	;;
esac \
| sed -e '/^#/d' -e '/^[ 	]*$/d'

# last filter is because the rpm "find-requires" on some platforms emits
# lines with a leading # and blank lines ... these kill the rpm packaging
# with the dreaded "Failed to find Requires:" error message

exit 0
