5 ################################################################
7 # Copyright (c) 1995-2014 SUSE Linux Products GmbH
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License version 2 or 3 as
11 # published by the Free Software Foundation.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program (see the file COPYING); if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 ################################################################
28 print "$0: <patches...> [file.spec]\n";
36 if ($arg =~ /\.spec$/) {
37 helpexit() if $specname;
47 my @specs = grep { /\.spec$/ } readdir(D);
50 # choose the one with the shortest name (heuristic)
51 $specname = ( sort { length($a) - length($b) } @specs)[0];
55 if (!defined($specname) || ! -f $specname) {
60 open(S, '<', $specname) or die;
65 my $last_patch_in_prep_index = 0;
66 my $last_patch_in_global_index = 0;
70 # first read the specfile, parse useful information
76 $last_patch_in_prep_index = $index if ($in_prep && $ifdef_level == 0);
78 die if ($ifdef_level < 0);
79 $ifdef_level++ if(/^\s*%\s*if/);
81 if ($ifdef_level == 0 && !$in_prep && $in_global
82 && /^\%(?:prep|build|install|package|description|doc)/) {
86 if (!$in_prep && /^%prep/i) {
93 $last_patch_in_prep_index = $index;
97 && /^\%(?:build|install|package|description|doc)/) {
101 die if (($in_prep + $in_global) > 1);
103 if ($in_global && /^Patch(?:\d+)?:\s+(.+)/) {
104 $last_patch_in_global_index = $index;
106 print "$1 already in, skipped.";
111 if ($in_global && $ifdef_level == 0 && /^Source(?:\d+)?:/) {
112 $last_patch_in_global_index = $index;
115 if ($in_prep && $ifdef_level == 0 && /^\%patch/) {
116 $last_patch_in_prep_index = $index;
123 die if ($ifdef_level > 0);
124 die if ($in_global || $in_prep);
125 die if ($last_patch_in_prep_index == 0);
126 die if ($last_patch_in_global_index == 0);
128 #print "adding Patch: $diffname to line $last_patch_in_global_index\n";
129 #print "adding %patch to line $last_patch_in_prep_index\n";
131 # determine patch number
133 $patchnum = $1+1 if ($c[$last_patch_in_global_index] =~ /Patch(\d+):/);
134 $patchnum = 1 if ($c[$last_patch_in_global_index] =~ /Patch:/);
136 for my $diffname (keys %diffs) {
137 # determine strip level
139 open(P, '<', $diffname) or die "$diffname: $!\n";
141 $striplevel = " -p1" if (m/^--- a/ or m/^--- [^\/]+-\d+\./);
147 print "Adding patch$striplevel $diffname to $specname\n";
150 splice @c, $last_patch_in_prep_index+1, 0, ("\%patch$patchnum$striplevel\n");
151 splice @c, $last_patch_in_global_index+1, 0,
152 (sprintf "Patch%s:%s%s\n", $patchnum, ' ' x (10-length($patchnum)), $diffname);
153 ++$last_patch_in_global_index;
154 $last_patch_in_prep_index+=2; # actually line number
158 open(O, '>', "$specname.new") or die;
162 system("diff", "-u", $specname, "$specname.new");
163 rename("$specname.new", $specname);