#! /bin/bash

sed -i s/%configure/%pkg_buildrequires_configure/ *.spec
for SPEC in *.spec ; do
    PBR=" pkg-buildrequires"
    cat $SPEC | while read -r ; do
	case $REPLY in
	    BuildRequires:* )
		SP=${REPLY#BuildRequires: }
		SP=${SP#*[-_A-Za-z0-9]}
		SP=${REPLY%??$SP}
		echo -n "$SP"
		DELETED=
		for PKG in ${REPLY#BuildRequires:} ; do
		    case $PKG in
			pkg-config )
			    ;;
			* )
			    if rpm -qlp /work/CDs/all/full-10.0-x86_64/suse/*/$PKG.rpm | grep -q \.pc$ ; then
				DELETED="$DELETED $PKG"
			    else
				echo -n " $PKG"
			    fi
			    ;;
		    esac
		done
		echo "$PBR"
		PBR=
		if test -n "$DELETED" ; then
		    cat <<EOF
#BEGIN pkg-buildrequires
# Initial bootstrap created by pkg-buildrequirize-prepare-spec
# It will fail and then overwritten by the consequent pkg-buildrequires-update
BuildRequires:$DELETED
#END pkg-buildrequires
EOF
		fi
	    ;;
	* )
	    echo "$REPLY"
	    ;;
	esac
    done >$SPEC.pbrnew
    mv $SPEC.pbrnew $SPEC
done
