summaryrefslogtreecommitdiff
path: root/addbr
blob: b101e3989cd316ee608dc66e2b4cd8d9842ef7d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/perl

use strict;
use warnings;

my $fname = shift @ARGV or die;
my $add = shift @ARGV or die;
my $msg = shift @ARGV || "requested";

$SIG{__WARN__} = sub
{
	local $_ = shift;
	chomp;
	print STDERR "\033[32;1m" . $_ . "\033[0m\n"
};

warn "Adding: $add\n";

my $file = $fname;
$file = "$ENV{HOME}/rpm/packages/$fname/$fname.spec"
	unless -r $file;
-r $file or die;

open FILE, "<", $file;
my @in = <FILE>;
close FILE;

my $brm = qr/#?(?:%\{!?\?with_\S+:)?\s*BuildRequires:\s*(\S+)\s*(?:(>|>=|==|<=|<).*)?\}?/i;
foreach ( @in ) {
	if ( /^$brm$/o ) {
		if ( $1 eq $add ) {
			warn "$add already on the list\n";
			exit;
		}
	}

}

my @out;
while ( $_ = shift @in ) {
	if ( /^\s*(build(requires|root|conflicts|arch)|requires(\(.*\))?|provides|conflicts|obsoletes|excludearch):/i ) {
		unshift @in, $_;
		last;
	}
	push @out, $_;
}

my @buf;
while ( $_ = shift @in ) {
	if ( /^%if/ ) {
		push @buf, $_;
		my $i = 1;
		while ( $_ = shift @in ) {
			push @buf, $_;
			if ( /^%if/ ) {
				$i++;
			} elsif ( /^%endif/ ) {
				$i--;
				last unless $i;
			}
		}

	} elsif ( /^$brm$/o ) {
		my $name = $1;
		push @buf, $_;

		if ( $add lt $name ) {
			push @out, "# AUTO: $msg\n";
			push @out, "BuildRequires:\t$add\n";
			$add = undef;
		}
		push @out, @buf;
		@buf = ();
	} elsif ( /^\s*$/ or /^\s*#/ ) {
		push @buf, $_;
	} else {
		push @buf, $_;
		push @out, "# AUTO: $msg\n";
		push @out, "BuildRequires:\t$add\n";
		last;

	}
	last unless $add;
}

push @out, @buf;
push @out, @in;


open FILE, ">", $file;
print FILE @out;
close FILE;