7 my $diffname = $ARGV[0];
8 my $specname = $ARGV[1];
10 if (!defined($diffname) || ! -f $diffname) {
12 print "$0: <diffname>\n";
19 my @specs = grep { /\.spec$/ } readdir(D);
22 # choose the one with the shortest name (heuristic)
23 $specname = ( sort { length($a) - length($b) } @specs)[0];
27 if (!defined($specname) || ! -f $specname) {
32 open(S, '<', $specname) or die;
37 my $last_patch_in_prep_index = 0;
38 my $last_patch_in_global_index = 0;
39 my $already_found_patch = 0;
43 # first read the specfile, parse useful information
49 $last_patch_in_prep_index = $index if ($in_prep && $ifdef_level == 0);
51 die if ($ifdef_level < 0);
52 $ifdef_level++ if(/^\s*%\s*if/);
54 if ($ifdef_level == 0 && !$in_prep && $in_global
55 && /^\%(?:prep|build|install|package|description|doc)/) {
59 if (!$in_prep && /^%prep/i) {
66 $last_patch_in_prep_index = $index;
70 && /^\%(?:build|install|package|description|doc)/) {
74 die if (($in_prep + $in_global) > 1);
76 if ($in_global && /^Patch(?:\d+)?:/) {
77 $last_patch_in_global_index = $index;
80 if ($in_global && $ifdef_level == 0 && /^Source(?:\d+)?:/) {
81 $last_patch_in_global_index = $index;
84 if ($in_global && /^Patch.*?:\s+$diffname/) {
85 $already_found_patch = 1;
88 if ($in_prep && $ifdef_level == 0 && /^\%patch/) {
89 $last_patch_in_prep_index = $index;
96 exit 0 if ($already_found_patch);
98 print "Adding patch $diffname to $specname\n";
100 die if ($ifdef_level > 0);
101 die if ($in_global || $in_prep);
102 die if ($last_patch_in_prep_index == 0);
103 die if ($last_patch_in_global_index == 0);
105 #print "adding Patch: $diffname to line $last_patch_in_global_index\n";
106 #print "adding %patch to line $last_patch_in_prep_index\n";
108 # determine patch number
110 $patchnum = $1+1 if ($c[$last_patch_in_global_index] =~ /Patch(\d+):/);
111 $patchnum = 1 if ($c[$last_patch_in_global_index] =~ /Patch:/);
113 # determine strip level
115 open(P, '<', $diffname) or die;
117 $striplevel = " -p1" if (m/^--- a/ or m/^--- [^\/]+-\d+\./);
124 splice @c, $last_patch_in_prep_index+1, 0, ("\%patch$patchnum$striplevel\n");
125 splice @c, $last_patch_in_global_index+1, 0,
126 (sprintf "Patch%s:%s%s\n", $patchnum, ' ' x (10-length($patchnum)), $diffname);
128 open(O, '>', "$specname.new") or die;
132 system("diff", "-u", $specname, "$specname.new");
133 rename("$specname.new", $specname);