Index: /trunk/debbuild
===================================================================
--- /trunk/debbuild	(revision 206)
+++ /trunk/debbuild	(revision 207)
@@ -71,4 +71,7 @@
 # this can be changed by the Vendor: header in the spec file
 $specglobals{'vendor'} = 'debbuild';
+
+# more things that should be parsed from rpm's macro files
+$specglobals{_default_patch_fuzz} = 0;
 
 # Initialized globals
@@ -975,31 +978,87 @@
 		qq(/bin/chmod -Rf a+rX,u+w,g-w,o-w .\n);
 
-      } elsif ( my ($patchnum,$patchopts) = (/^\%patch(\d*)(\s+.+)?$/) ) {
-        unless ( $patchnum ) {
-           $patchnum = ( $patchopts =~ s/-P\s+(\d+)// ? $1 : 0 );
+      } elsif (/^\%patch(\d*)/) {
+        my $line = $_;
+
+        # Things rpmbuild Does
+        # -> blindly follows Patch(.*):  ==>  %patch$1
+        # %patch0 does not in fact equal %patch without -P
+        # spaces optional between flag and argument
+        # multiple -P options actually trigger multiple patch events.  >_<
+        # can we emulate this?
+        # yes we can!
+        # add patch{nn} to the list
+        my @patchlist;
+        push @patchlist, "$1" if $1;
+
+# options:
+# -P  patch number
+# -p  path strip.  passed to patch as-is
+# -b  backup file postfix.  literal, if eg "bkfile", backup files will be "filebkfile", not "file.bkfile".  Passed to
+#     patch as-is, with a minor flag adjustment
+# -E  Remove empty files.  Passed to patch as-is
+
+# from /usr/lib/rpm/macros;  doesn't seem to be used by rpmbuild currently
+# Default patch flags
+#%_default_patch_flags  -s
+
+        my @pbits = split /\s+/;
+        my $plev = 0;
+        my $psuff = '';
+        my $noempty = 0;
+        shift @pbits;
+        my $pnflag = 0;
+        while (my $popt = shift @pbits) {
+          if ($popt =~ /^-P(\d*)/) {
+            $pnflag = 1;
+            # push the patchnum onto the list
+            if ($1) {
+              push @patchlist, "$1";
+            } else {
+              my $tmp = shift @pbits;
+              die "Invalid patch number $tmp: $line" if $tmp !~ /^\d+$/;
+              push @patchlist, $tmp;
+            }
+          } elsif ($popt =~ (/^-b([^\s]*)/) ) {
+            if ($1) {
+              $psuff = "-b --suffix $1";
+            } else {
+              $psuff = "-b --suffix ".shift(@pbits);
+            }
+          } elsif ($popt =~ (/^-p([^\s]*)/) ) {
+            if ($1) {
+              $plev = $1;
+            } else {
+              $plev = shift @pbits;
+            }
+          } elsif ($popt =~ (/^-E/) ) {
+            $noempty = 1;
+          }
+        } # while (shift @pbits)
+
+        # add the "null" patch to the list unless we've got a -P flag, or there's already a patch on the list
+        push @patchlist, '' unless $pnflag || @patchlist;
+
+        my $patchopts = " -p$plev $psuff";
+        $patchopts .= " --fuzz=".$specglobals{_default_patch_fuzz};
+        $patchopts .= " -E" if $noempty;
+
+        foreach my $pnum (@patchlist) {
+          $prepscript .= qq(echo "Patch ).($pnum eq '' ? '' : "#$pnum ").qq(($pkgdata{main}{$pnum}):"\n);
+          if ( $pkgdata{main}{$pnum} =~ /\.(?:Z|gz|bz2|xz)$/ ) {
+            # Compressed patch.  You weirdo.
+            my $decompressor;
+            $decompressor = '/bin/gzip'  if $pkgdata{main}{$pnum} =~ /\.(?:Z|gz)$/;
+            $decompressor = '/bin/bzip2' if $pkgdata{main}{$pnum} =~ /\.bz2$/;
+            $decompressor = '/usr/bin/xz'    if $pkgdata{main}{$pnum} =~ /\.xz$/;
+            $prepscript .= qq($decompressor -dc $topdir/SOURCES/$pkgdata{main}{$pnum} | /usr/bin/patch $patchopts\n\n);
+          } elsif ( $pkgdata{main}{$pnum} =~ /\.zip$/ ) {
+            # .zip'ed patch.  *backs away slowly*
+            $prepscript .= qq(/usr/bin/unzip $topdir/SOURCES/$pkgdata{main}{$pnum} | /usr/bin/patch $patchopts\n\n);
+          } else {
+            $prepscript .= "/bin/cat $topdir/SOURCES/$pkgdata{main}{$pnum} | /usr/bin/patch $patchopts\n\n";
+          }
         }
-        $prepscript .= qq(echo "Patch #$patchnum ($pkgdata{main}{"patch$patchnum"}):"\n);
-	# If there are options passed, use'em.
-	# Otherwise, catch a bare %patch and ASS-U-ME it's '-p0'-able.
-	# Will break on options that don't provide -pnn, but what the hell.
-	# Need to decompress .bz2 and .gz patches on the fly.  Not sure why one
-	# would ever have such things, but there they are...  and rpmbuild supports 'em
-	# patch originally suggested by Lucian Cristian for .bz2
-	# reworked and expanded to support .Z/.gz/.xz as well
-	if ( $pkgdata{main}{"patch$patchnum"} =~ /\.(?:Z|gz|bz2|xz)$/ ) {
-	  my $decompressor;
-	  $decompressor = 'gzip'  if $pkgdata{main}{"patch$patchnum"} =~ /\.(?:Z|gz)$/;
-	  $decompressor = 'bzip2' if $pkgdata{main}{"patch$patchnum"} =~ /\.bz2$/;
-	  $decompressor = 'xz'    if $pkgdata{main}{"patch$patchnum"} =~ /\.xz$/;
-	  $prepscript .= qq(/bin/$decompressor -d < $topdir/SOURCES/$pkgdata{main}{"patch$patchnum"} | patch );
-	  $prepscript .= $patchopts if $patchopts;
-	  $prepscript .= "-p0" if !$patchopts;
-	  $prepscript .= qq( -s\nSTATUS=\$?\nif [ \$STATUS -ne 0 ]; then\n  exit \$STATUS\nfi\n);
-	} else {
-	  $prepscript .= "patch ";
-	  $prepscript .= $patchopts if $patchopts;
-	  $prepscript .= "-p0" if !$patchopts;
-	  $prepscript .= " -s <$topdir/SOURCES/".$pkgdata{main}{"patch$patchnum"}."\n";
-	}
+
       } else {
         $prepscript .= expandmacros($_,'gp');
@@ -1163,6 +1222,6 @@
       } elsif (my ($patchnum, $patch) = (/^patch(\d*):\s*(.+)/i)) {
         $patch =~ s/\s*$//;
-        $patchnum = 0 unless $patchnum;
-        $pkgdata{main}{"patch$patchnum"} = basename($patch);
+        $patchnum = '' if !defined($patchnum);
+        $pkgdata{main}{$patchnum} = basename($patch);
       } elsif (/^buildarch(?:itecture)?:\s*(.+)/i) {
         $pkgdata{main}{arch} = $1;
