#!/usr/bin/perl 

# Probably buggy

use warnings;
use strict;
my %rpms;       	# hash to create a list of rpm's and versions
my %svnignorelist;   	# hash to create a list of rpm's and versions
my %ignorelist; 	# hash to create a list of rpm's and versions
my $srpm;
my $srpmname;
my $rpm_dir;
my $cache_dir;
my $nbe_env;

# Include nbe.pl
require("/usr/local/nbe/bin/nbe.pm");

# Get environment vars.
$nbe_env = get_nbe_env();

#
# Do we include all base/updates/extras rpms ?
#
my $rpm_list = $ARGV[0];
my $name     = $ARGV[0];
$name        =~ s/^.*rpmlist\.//g;
chomp($name);
my $status=`grep fedora_$name /etc/nbe/external-rpms|sed s/^.*=//g`;

#
# Create ignore list.
#
open(IGNORELIST,"$nbe_env->{MYSHORTLINK}/svn.list")|| die "Cannot open svnlist: $!\n";
while ( my $line =<IGNORELIST> )
{
	chomp($line);

	if (!($line =~ m/^#/) | ($line =~ m/^\s*$/)) {

		# Remove everyting but the package name.
		$line =~ s/\s+.*//g;

		# Remove the - from packages we want to remove.
		$line =~ s/^-//g;

		$ignorelist{$line} = "TRUE";
	}
}

if ( -f "$nbe_env->{MYSHORTLINK}/external_rpms.filter") {
	open(IGNORELIST,"$nbe_env->{MYSHORTLINK}/external_rpms.filter")|| die "Cannot open svnlist: $!\n";
	while ( my $line =<IGNORELIST> )
	{
		chomp($line);

		if (!($line =~ m/^#/) | ($line =~ m/^\s*$/)) {

			# Remove everyting but the package name.
			$line =~ s/\s+.*//g;

			# Remove the - from packages we want to remove.
			$line =~ s/^-//g;

  			$ignorelist{$line} = "TRUE";
  		}	
	}
}

if ($status =~ "y") {

	open(RPMSLIST,$rpm_list)|| die "Cannot open rpmlist: $!\n";
	while ( my $line =<RPMSLIST> )
	{
    		chomp $line;
    		next if $line=~ /^\s{0,}$/; # skip empty lines
    		next if $line=~ /^#/;       # skip we only check rpms
    		next if $line=~ /^-/;       # skip we only check rpms
    		next if $line!~ /\.rpm$/;   # skip we only check rpms

    		$rpm_dir   = "/home/$nbe_env->{MYRELMAN}/current/reltree/allrpms/$nbe_env->{MYVENDOR}/$nbe_env->{MYDIST}/$nbe_env->{MYARCH}/RPMS/";
    		$cache_dir = "$nbe_env->{MYSHORTLINK}/$nbe_env->{MYCOMPANY}/RPMS/cache";
    		$srpm      = get_srpms($line,$rpm_dir,$cache_dir);

    		#
    		# Get info from srpm name.
    		#	
    		my ( $srpmname, $srpmversion, $srpmrevision, $arch );
       		   ( $srpmname, $srpmversion, $srpmrevision, $arch ) = $srpm =~ /(.*)?-(\w*\d+.*)?-(\d{0,}|\w{0,}.*)?\.(\w+)\.rpm/; 

    		# If the rpm is on the ignore list add srpm name to the svnignorelist.
		if (exists $ignorelist{$srpmname}) {
		} else {	 	
			print "$line\n";
		}
  	}
  	close RPMSLIST;
}
