8 print "$0: <patches...> [file.spec]\n";
16 if ($arg =~ /\.spec$/) {
17 helpexit() if $specname;
27 my @specs = grep { /\.spec$/ } readdir(D);
30 # choose the one with the shortest name (heuristic)
31 $specname = ( sort { length($a) - length($b) } @specs)[0];
35 if (!defined($specname) || ! -f $specname) {
40 open(S, '<', $specname) or die;
45 my $last_patch_in_prep_index = 0;
46 my $last_patch_in_global_index = 0;
50 # first read the specfile, parse useful information
56 $last_patch_in_prep_index = $index if ($in_prep && $ifdef_level == 0);
58 die if ($ifdef_level < 0);
59 $ifdef_level++ if(/^\s*%\s*if/);
61 if ($ifdef_level == 0 && !$in_prep && $in_global
62 && /^\%(?:prep|build|install|package|description|doc)/) {
66 if (!$in_prep && /^%prep/i) {
73 $last_patch_in_prep_index = $index;
77 && /^\%(?:build|install|package|description|doc)/) {
81 die if (($in_prep + $in_global) > 1);
83 if ($in_global && /^Patch(?:\d+)?:\s+(.+)/) {
84 $last_patch_in_global_index = $index;
86 print "$1 already in, skipped.";
91 if ($in_global && $ifdef_level == 0 && /^Source(?:\d+)?:/) {
92 $last_patch_in_global_index = $index;
95 if ($in_prep && $ifdef_level == 0 && /^\%patch/) {
96 $last_patch_in_prep_index = $index;
103 die if ($ifdef_level > 0);
104 die if ($in_global || $in_prep);
105 die if ($last_patch_in_prep_index == 0);
106 die if ($last_patch_in_global_index == 0);
108 #print "adding Patch: $diffname to line $last_patch_in_global_index\n";
109 #print "adding %patch to line $last_patch_in_prep_index\n";
111 # determine patch number
113 $patchnum = $1+1 if ($c[$last_patch_in_global_index] =~ /Patch(\d+):/);
114 $patchnum = 1 if ($c[$last_patch_in_global_index] =~ /Patch:/);
116 for my $diffname (keys %diffs) {
117 # determine strip level
119 open(P, '<', $diffname) or die "$diffname: $!\n";
121 $striplevel = " -p1" if (m/^--- a/ or m/^--- [^\/]+-\d+\./);
127 print "Adding patch$striplevel $diffname to $specname\n";
130 splice @c, $last_patch_in_prep_index+1, 0, ("\%patch$patchnum$striplevel\n");
131 splice @c, $last_patch_in_global_index+1, 0,
132 (sprintf "Patch%s:%s%s\n", $patchnum, ' ' x (10-length($patchnum)), $diffname);
133 ++$last_patch_in_global_index;
134 $last_patch_in_prep_index+=2; # actually line number
138 open(O, '>', "$specname.new") or die;
142 system("diff", "-u", $specname, "$specname.new");
143 rename("$specname.new", $specname);