#!/usr/bin/perl 

# Probably buggy

use warnings;
use strict;
my $rpmlist=shift || 'rpmlist';
my $svnlist=shift || 'svnlist';
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();

$cache_dir = "$nbe_env->{MYSHORTLINK}/Neonova/RPMS/cache";
$rpm_dir   = "$nbe_env->{MYSHORTLINK}/Neonova/RPMS/tmp";

open(SVNLIST,$svnlist)|| die "Cannot open rpmlist: $!\n";
while ( my $line =<SVNLIST> )
{
	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

	# Get the name of the source rpm.
	if ( -f "$line" ) {

		$srpm = get_srpms($line,$rpm_dir,$cache_dir);
		#$srpm = get_srpms($line);

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

# Create ignore list.
open(IGNORELIST,"ignorelist")|| die "Cannot open ignorelist: $!\n";
while ( my $line =<IGNORELIST> )
{
	chomp($line);
	$ignorelist{$line} = "TRUE";
}

open(RPMLIST,$rpmlist)|| die "Cannot open rpmlist: $!\n";
while ( my $line =<RPMLIST> )
{
	chomp $line;
	$line =~ s/\\//g; 	
	next if $line=~ /^\s{0,}$/; # skip empty lines
	next if $line!~ /\.rpm$/; # we only check rpms
	my ( $rpmname, $rpmversion, $rpmrevision, $arch );
	( $rpmname, $rpmversion, $rpmrevision, $arch ) = $line=~ /(.*)?-(\w*\d+.*)?-(\d{0,}|\w{0,}.*)?\.(\w+)\.rpm/; 

	# Here we add some rpms to the svnignorelist.
	if ( -f $line ) {
		# Get the name of the source rpm.
		$srpm = get_srpms($line,$rpm_dir,$cache_dir);
		#$srpm = get_srpms($line);

		# 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{$rpmname}) {
			$svnignorelist{$srpmname} = "TRUE";
			#print "$srpmname\n";
		}
	} 

	# Only add the newest rpm to the list.
	my @values;
	push @values, $rpmversion;
	push @values, $rpmrevision;
	if ( exists $rpms{$arch}->{$rpmname} )
	{
		if ( &is_newer( $rpms{$arch}->{$rpmname}, \@values ) == 0 )
		{
			$rpms{$arch}->{$rpmname}=\@values;
		}
	}
	else
	{
		$rpms{$arch}->{$rpmname}=\@values;
	}
}
close RPMLIST;

foreach my $arch ( keys( %rpms) )
{
	while (( my $rpmname , my @values ) = each %{$rpms{$arch}})
	{
		# Create rpm name.  
		$rpmname = "$rpmname-$rpms{$arch}->{$rpmname}->[0]-$rpms{$arch}->{$rpmname}->[1].$arch.rpm";
		chomp($rpmname);

		if ( -f "$rpmname" ) {

			# Get the name of the source rpm.
			$srpm = get_srpms($rpmname,$rpm_dir,$cache_dir);
			#$srpm = get_srpms($rpmname);

			# Get info from rpm name.
			my ( $srpmname, $srpmversion, $srpmrevision, $arch );
			( $srpmname, $srpmversion, $srpmrevision, $arch ) = $srpm =~ /(.*)?-(\w*\d+.*)?-(\d{0,}|\w{0,}.*)?\.(\w+)\.rpm/; 
			# Source rpm is not in the svn list so we use the fedora rpm.
			if (!(exists($svnignorelist{$srpmname}))) {
				print "$rpmname\n";
			}
		}
	}
}
