]>
Commit | Line | Data |
---|---|---|
a7d61e66 | 1 | #!/usr/bin/perl |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | my $fname = shift @ARGV or die; | |
7 | my $add = shift @ARGV or die; | |
b8ef893a | 8 | my $msg = shift @ARGV || "requested"; |
a7d61e66 | 9 | |
10 | $SIG{__WARN__} = sub | |
11 | { | |
12 | local $_ = shift; | |
13 | chomp; | |
14 | print STDERR "\033[32;1m" . $_ . "\033[0m\n" | |
15 | }; | |
16 | ||
17 | warn "Adding: $add\n"; | |
18 | ||
614c5b13 | 19 | my $file = $fname; |
20 | $file = "$ENV{HOME}/rpm/packages/$fname/$fname.spec" | |
21 | unless -r $file; | |
a7d61e66 | 22 | -r $file or die; |
23 | ||
24 | open FILE, "<", $file; | |
25 | my @in = <FILE>; | |
26 | close FILE; | |
27 | ||
3ac4924e | 28 | my $brm = qr/#?(?:%\{!?\?with_\S+:)?\s*BuildRequires:\s*(\S+)\s*(?:(>|>=|==|<=|<).*)?\}?/i; |
a7d61e66 | 29 | foreach ( @in ) { |
30 | if ( /^$brm$/o ) { | |
31 | if ( $1 eq $add ) { | |
32 | warn "$add already on the list\n"; | |
33 | exit; | |
34 | } | |
35 | } | |
36 | ||
37 | } | |
38 | ||
39 | my @out; | |
40 | while ( $_ = shift @in ) { | |
37893947 | 41 | if ( /^\s*(build(requires|root|conflicts|arch)|requires(\(.*\))?|provides|conflicts|obsoletes|excludearch):/i ) { |
a7d61e66 | 42 | unshift @in, $_; |
43 | last; | |
44 | } | |
45 | push @out, $_; | |
46 | } | |
47 | ||
48 | my @buf; | |
49 | while ( $_ = shift @in ) { | |
50 | if ( /^%if/ ) { | |
51 | push @buf, $_; | |
52 | my $i = 1; | |
53 | while ( $_ = shift @in ) { | |
54 | push @buf, $_; | |
55 | if ( /^%if/ ) { | |
56 | $i++; | |
57 | } elsif ( /^%endif/ ) { | |
58 | $i--; | |
59 | last unless $i; | |
60 | } | |
61 | } | |
62 | ||
63 | } elsif ( /^$brm$/o ) { | |
64 | my $name = $1; | |
65 | push @buf, $_; | |
66 | ||
67 | if ( $add lt $name ) { | |
68 | push @out, "# AUTO: $msg\n"; | |
69 | push @out, "BuildRequires:\t$add\n"; | |
70 | $add = undef; | |
71 | } | |
72 | push @out, @buf; | |
73 | @buf = (); | |
74 | } elsif ( /^\s*$/ or /^\s*#/ ) { | |
75 | push @buf, $_; | |
76 | } else { | |
77 | push @buf, $_; | |
78 | push @out, "# AUTO: $msg\n"; | |
79 | push @out, "BuildRequires:\t$add\n"; | |
80 | last; | |
81 | ||
82 | } | |
83 | last unless $add; | |
84 | } | |
85 | ||
86 | push @out, @buf; | |
87 | push @out, @in; | |
88 | ||
89 | ||
90 | open FILE, ">", $file; | |
91 | print FILE @out; | |
92 | close FILE; |