#!/usr/bin/perl -w

use strict;
use warnings;
use Carp;
use File::Basename;
use Getopt::Long;
use RPM::Header;
use File::Find;

use Autorepo::Config qw/@AUTOREPO_ARCH @AUTOREPO_RPM_DIRS/;

my $pkg_found=0;
my @body;
my @drop;
while (<>) {
    next if /^\+\+\+\s/;
    next if /^\-\-\-\s/;
    next if /^\@\@/;
    next if /^\@\@/;
    next if /^-/;
    if (/^\+?Package\s(\S+)\sversion\s(\S+)\shas/) {
	my $prefix=$1;
	my $ver=$2;
	$prefix=~s/\.32bit$//;
	$ver=~s/^\d+://;
	$pkg_found=&rpm_exists($prefix.'-'.$ver);
	if ($pkg_found) {
	    push @body, $_;
	} else {
	    push @drop, $_;
	}
    } elsif (/^\+?\sDepends:/) {
	if ($pkg_found) {
	    push @body, $_;
	} else {
	    push @drop, $_;
	}
    } else {
	warn "bad input: $_";
    }
}

print @body;
#print @drop if $debug;

sub rpm_exists {
    my $prefix=shift;
    foreach my $arch (@AUTOREPO_ARCH) {
	foreach my $dir (@AUTOREPO_RPM_DIRS) {
	    return 1 if -e $dir.'/'.$prefix.'.'.$arch.'.rpm';
	}
    }
    return 0;
}

__END__
format1: raw unmets dump
Package perl-Math-SimpleHisto-XS.32bit version 1.29-alt1 has an unmet dep:
 Depends: perl(SOOT.pm)
__END__
format2: raw unmets dumps diff
--- /tmp/.private/sourceimport/unmets.old       2014-06-04 07:15:38.859314435 +0400
+++ /tmp/.private/sourceimport/unmets.new       2014-06-04 07:15:33.436498327 +0400
@@ -0,0 +1,258 @@
+Package perl-Math-SimpleHisto-XS.32bit version 1.29-alt1 has an unmet dep:
+ Depends: perl(SOOT.pm)
