#!/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 "Can't read $file"; open FILE, "<", $file; my @in = ; 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(1); } } } 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;