+ @spec = ("");
+
+ open(F, "< $_[0]") or die("failed opening: " . $_[0]);
+ while (<F>) {
+ chomp;
+ if (/^\s*(\%(description|package|prep|install|pre|post|files)|BuildRoot|URL)/) {
+ last;
+ } elsif (/^\s*(\%if.*|\%else|\%endif|\%define\s+.*|Version.*|Name.*)\s*$/) {
+ $_ = $1;
+ if ($spec[$#spec] =~ /\%if/) {
+ if (/\%else/) {
+ next; # don't include empty %if-%else
+ } elsif (/\%endif/) {
+ # remove empty %if-%endif
+ pop @spec;
+ next;
+ }
+ }
+ push @spec, $_;
+ } elsif (/^NoSource\s*:\s*(\d+)\s*$/i) {
+ $no_source{ "source" . $1 } = 1;
+ } elsif (my ($patchset) = /^%patchset_source\s+(.+)$/) {
+ use Getopt::Long qw(GetOptionsFromString);
+ my ($f, $s);
+ my ($ret, $args) = GetOptionsFromString($patchset, 's=s' => \$s, 'f=s' => \$f);
+ %patchset = (
+ pattern => $f,
+ filelist => $s,
+ start => $args->[0],
+ end => $args->[1],
+ );
+ }
+ }
+ close(F);
+
+ shift @spec;
+} # }}}
+
+# read in 'sources' file
+sub read_sources_file {
+ my $filename = $_[0] || $sources_file;
+ return () unless $filename and -e $filename;
+
+ my %files;
+
+ open(my $fh, '<', $filename) or die $!;
+ while (<$fh>) {
+ chomp;
+ next unless my ($hash, $filename) = /^([a-f0-9]{32})\s+\*?(.+)$/;
+ $files{$filename} = $hash;
+ }
+ return \%files;
+}
+
+sub process_patchset($) {
+ my $macros = shift;
+ my $checksums;
+
+ return unless %patchset;
+
+ # print all files from sources file
+ if ($patchset{filelist}) {
+ my $prefix = expand($patchset{pattern}, $macros);
+ my $filelist = expand($patchset{filelist}, $macros);
+ $checksums = read_sources_file($filelist);
+ while (my($file, $hash) = each %$checksums) {
+ my $url = $prefix . $file;
+ print_source "patchset $file", $hash, $url;
+ }
+ return;
+ }
+
+ # parse sources file sequences
+ $checksums = read_sources_file() or return;
+
+ # print out patchset entries which source md5 is present in source file
+ my $start = expand($patchset{start}, $macros);
+ my $end = expand($patchset{end}, $macros);
+ my $pattern = expand($patchset{pattern}, $macros);
+ for (my $i = $start; $i <= $end; $i++) {
+ my $url = sprintf($pattern, $i);
+ my ($basename) = $url =~ m{/([^/]+$)};
+ my $hash = $checksums->{$basename} or next;
+ print_source "patchset $i", $hash, $url;
+ }