diff --git a/src/tools/pginclude/pgrminclude b/src/tools/pginclude/pgrminclude
index 07e2cb68376364bf65a32740b7beab0dbc080a82..33dd11eeff8f206be6c35dba21fac061f74141bf 100755
--- a/src/tools/pginclude/pgrminclude
+++ b/src/tools/pginclude/pgrminclude
@@ -1,7 +1,7 @@
 :
 # remove extra #include's
 
-# $PostgreSQL: pgsql/src/tools/pginclude/pgrminclude,v 1.12 2006/07/11 20:51:25 momjian Exp $
+# $PostgreSQL: pgsql/src/tools/pginclude/pgrminclude,v 1.13 2006/07/12 16:28:27 momjian Exp $
 
 trap "rm -f /tmp/$$.c /tmp/$$.o /tmp/$$ /tmp/$$a /tmp/$$b" 0 1 2 3 15
 find . \( -name CVS -a -prune \) -o -type f -name '*.[ch]' -print | 
@@ -15,18 +15,15 @@ do
 	else	IS_INCLUDE="N"
 	fi
 	
-	# remove defines
-	if [ "$IS_INCLUDE" = "Y" ]
-	then	cat "$FILE" | grep -v "^#if" | grep -v "^#else" | 
-		grep -v "^#endif" | sed 's/->[a-zA-Z0-9_\.]*//g' >/tmp/$$a
-	else	cat "$FILE" >/tmp/$$a
-	fi
-
 	# loop through all includes
-	cat /tmp/$$a | grep "^#include" |
+	cat "$FILE" | grep "^#include" |
 	sed 's/^#include[ 	]*[<"]\([^>"]*\).*$/\1/g' |
 	while read INCLUDE
 	do
+		if [ "$1" = "-v" ]
+		then	echo "checking $FILE $INCLUDE"
+		fi
+
 		[ -s /usr/include/$INCLUDE ] && continue
 		[ "$INCLUDE" = postgres.h ] && continue
 		[ "$INCLUDE" = config.h ] && continue
@@ -39,9 +36,17 @@ do
 		grep -A1 '^#include[ 	][ 	]*[<"]'"$INCLUDE"'[>"]' "$FILE" |
 		     egrep -q '^#else|^#endif' && continue
 
+		# remove defines from include files
+		if [ "$IS_INCLUDE" = "Y" ]
+		then	cat "$FILE" | grep -v "^#if" | grep -v "^#else" | 
+			grep -v "^#endif" | sed 's/->[a-zA-Z0-9_\.]*//g' >/tmp/$$a
+		else	cat "$FILE" >/tmp/$$a
+		fi
+
 		# set up initial file contents
-		cat /tmp/$$a |
-		grep -v '^#include[ 	]*[<"]'"$INCLUDE"'[>"]' >/tmp/$$b
+		grep -v '^#include[ 	][ 	]*[<"]'"$INCLUDE"'[>"]' \
+			/tmp/$$a >/tmp/$$b
+
 		if [ "$IS_INCLUDE" = "Y" ]
 		then	echo "#include \"postgres.h\"" >/tmp/$$.c
 		else	>/tmp/$$.c
@@ -66,11 +71,7 @@ do
 				cat /tmp/$$b
 				cat /tmp/$$.c
 			fi
-			grep -v '^#include[ 	][ 	]*[<"]'"$INCLUDE"'[>"]' "$FILE" >/tmp/$$b
 			mv /tmp/$$b "$FILE"
-		elif [ "$1" = "-v" ]
-		then	echo "$FILE"
 		fi
-
 	done
 done