#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
#
# $Id: chk_inst_files,v 1.1 2003/12/26 08:11:27 othauvin Exp $
#
use strict;
use lib qw(/usr/lib/libDrakX);
use common;
use installchecker;
# for debugging purpose
use Data::Dumper;

#
use URPM;
use Getopt::Long;
#use MDK::Common;

my %param = (
);

# Global package set
my $pkgs;
my $groups;

my $urpm = URPM->new();


# Input: array of all rpms, dir or *hdlist.cz
# parse all header
sub parse_headers {
    foreach my $list (@_) {
    	my ($first, $last);
        -d $list and do {
            ($first, $last) = $urpm->parse_rpm($_, keep_all_tags => 1) foreach (glob("$list/*.rpm"));
        };
        -f $list && $list =~ /.*\.rpm$/ and do {
            ($first, $last) = $urpm->parse_rpm($list, keep_all_tags => 1);
        };
        -f $list && $list =~ /^synthesis.*\.cz$/ and do {
            ($first, $last) = $urpm->parse_synthesis($list);
        };
        -f $list && $list =~ /.*\.cz/ && $list !~ /^synthesis/ and do {
            ($first, $last) = $urpm->parse_hdlist($list);
        };
	foreach my $id ($first .. $last) {
		$urpm->{depslist}[$id]->arch eq 'src' and next;
		$urpm->{depslist}[$id]->name =~ /^lib/ && $urpm->{depslist}[$id]->name !~ /devel/ and next;
		if (defined $pkgs->{$urpm->{depslist}[$id]->name}{id}) {
			my $actual_pkg = $urpm->{depslist}[$pkgs->{$urpm->{depslist}[$id]->name}{id}];
			$actual_pkg->compare($urpm->{depslist}[$id]) and $pkgs->{$actual_pkg->name}{id} = $id;
		}  else {
			$pkgs->{$urpm->{depslist}[$id]->name}{id} = $id;
		}
	}
    }
}


#############
# Main Loop #
#############

GetOptions(
	"r=s" => \$param{rpmsrate},
	"c=s" => \$param{compssUsers},
	"verbose=i" => \$param{error_level},
);
my ($pkgrate, $compssgroup);
$param{rpmsrate} and do {
       $pkgrate = parse_rpmsrate($param{rpmsrate});
};
$param{compssUsers} and do {
       $compssgroup = parse_compssUsers($param{compssUsers});
};
parse_headers(@ARGV);


if (defined($pkgrate) && defined($compssgroup)) {
	my @g;
	my @gnotfound;
	foreach (@$compssgroup) {
		push @g, @{$_->{wanted}};
	}
	
	foreach my $name (keys %$pkgrate) {
		foreach my $n (map { split /\s*\&\&\s*/ } @{$pkgrate->{$name}{want}}) {
			#$n =~ /^\&\&$/ and next;
			$n =~ s/^\!//;
			$n =~ /^[0-9,A-Z]*".*"$/ and next;
			$n =~ /^INSTALL$/ and next;
			any { /^$n$/ } @g or push @gnotfound, $n; 
		}
	}
	installchecker::report_error('W',"Group '%s' in rpmsrate not found in compssUsers", $_) foreach(uniq(@gnotfound));
}
