]>
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; | |
8 | my $msg = shift @ARGV or die; | |
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 | ||
19 | my $file = "$ENV{HOME}/rpm/packages/$fname/$fname.spec"; | |
20 | -r $file or die; | |
21 | ||
22 | open FILE, "<", $file; | |
23 | my @in = <FILE>; | |
24 | close FILE; | |
25 | ||
171d3ec3 | 26 | my $brm = qr/#?(?:%{!?\?with_\S+:)?\s*BuildRequires:\s*(\S+)\s*(?:(>|>=|==|<=|<).*)?}?/i; |
a7d61e66 | 27 | foreach ( @in ) { |
28 | if ( /^$brm$/o ) { | |
29 | if ( $1 eq $add ) { | |
30 | warn "$add already on the list\n"; | |
31 | exit; | |
32 | } | |
33 | } | |
34 | ||
35 | } | |
36 | ||
37 | my @out; | |
38 | while ( $_ = shift @in ) { | |
a75e2ecf | 39 | if ( /^\s*(build(requires|root|conflicts)|requires(\(.*\))?|provides|conflicts|obsoletes):/i ) { |
a7d61e66 | 40 | unshift @in, $_; |
41 | last; | |
42 | } | |
43 | push @out, $_; | |
44 | } | |
45 | ||
46 | my @buf; | |
47 | while ( $_ = shift @in ) { | |
48 | if ( /^%if/ ) { | |
49 | push @buf, $_; | |
50 | my $i = 1; | |
51 | while ( $_ = shift @in ) { | |
52 | push @buf, $_; | |
53 | if ( /^%if/ ) { | |
54 | $i++; | |
55 | } elsif ( /^%endif/ ) { | |
56 | $i--; | |
57 | last unless $i; | |
58 | } | |
59 | } | |
60 | ||
61 | } elsif ( /^$brm$/o ) { | |
62 | my $name = $1; | |
63 | push @buf, $_; | |
64 | ||
65 | if ( $add lt $name ) { | |
66 | push @out, "# AUTO: $msg\n"; | |
67 | push @out, "BuildRequires:\t$add\n"; | |
68 | $add = undef; | |
69 | } | |
70 | push @out, @buf; | |
71 | @buf = (); | |
72 | } elsif ( /^\s*$/ or /^\s*#/ ) { | |
73 | push @buf, $_; | |
74 | } else { | |
75 | push @buf, $_; | |
76 | push @out, "# AUTO: $msg\n"; | |
77 | push @out, "BuildRequires:\t$add\n"; | |
78 | last; | |
79 | ||
80 | } | |
81 | last unless $add; | |
82 | } | |
83 | ||
84 | push @out, @buf; | |
85 | push @out, @in; | |
86 | ||
87 | ||
88 | open FILE, ">", $file; | |
89 | print FILE @out; | |
90 | close FILE; |