#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
#
# $Id: distlint,v 1.10 2003/04/25 00:32:57 nanardon Exp $

use strict;
use URPM;
use URPM::Build;
use MDK::Common;
use urpmchecker;

#my $urpm = URPM->new;
my $urpmchecker = urpmchecker->new;

my (%flag, %path, %conflictf, @list);

sub help {
	print urpmchecker::version();
    print "
Usage: $0 [options] [--] [+]arg1 [[+]arg2 ... [+]argn]
Check missing dependances for each arg passed.

arg can be:
 -  a hdlist (not synthesis)
 -  dir, it look for dir/*.rpm
 -  a rpm

-p packager show error if changelog text contain 'packager'
-n name     show error if package name contain 'name'
-a arch     set system architecture to arch, then check only compatible rpms
-no-cf      do not check for conflict files

--          set or unset report flag (start to off)
/           up to next level of rpms, check order dependancies
--src       set or unset check link src <=> binary
--no-dep    set or unset non-checking dependencies

-h          print this help
--ho [TYPE] print output explanations

Each arg can be setup in ~/.chkdep, to refer to a macros, call the macros name
with a '+' as prefix. ~/.chkdep format is:
macros = arg

ex: $0 Mandrake/RPMS +macros / -- *.rpm
"; 
    exit 0;
}

sub help_output {
    print "$0 output description:
	
packager name in changelog <packager\@email> 
 packagename (source_package)
    TYPE : description 2nd_package_concerned

If too many problem are found for a package, description is the number of problem.

";
	print join("\n",urpmchecker::explanation_error(@_));
    exit 0;
}

if (-f "$ENV{HOME}/.chkdep") {
    foreach (cat_("$ENV{HOME}/.chkdep")) {
	s/#.*//;         # no comments
        s/^\s+//;        # no leading white
        s/\s+$//;        # no trailing white
        length or next;      # anything left?
        my ($name, $value) = split(/\s*=\s*/, $_, 2);
        $path{"+$name"} = $value;
    }
}
while (local $_ = shift @ARGV) {
	# repository flag
	/^--$/        and do { $flag{c}= $flag{c} ? 0 : 1 ; $flag{yc}=1 ; next };
	/^\/$/        and do { $flag{nextid}=1; next };
	/^--src$/     and do { $flag{checksrc}= $flag{checksrc} ? 0 : 1 ; next };
	/^--no-dep$/  and do { $flag{nodep}= $flag{nodep} ? 0 : 1 ; next };
	/^--no-cf$/   and do { $flag{nocf} = $flag{nocf} ? 0 : 1 ; next };

	# global options
	/^-p$/ and do { $flag{p} = shift @ARGV; next };
	/^-n$/ and do { $flag{n} = shift @ARGV; next };
	/^-v$/ and do { $flag{v} = 1; next };
	/^-a$/ and do { $urpmchecker->{arch}= shift @ARGV; next };

	
	/^-h$|^--help$/  and help();
	/^--ho$/ and help_output(@ARGV);

	# /^--no-name$/ and do { $flag{noname} = 1 ; next };

    /^\+/ and $_ = $path{$_};
    $urpmchecker->add($_,{
		checkdep => $flag{nodep}, 
		checksrc => $flag{checksrc},
	    checkcf =>  $flag{nocf},	
		check => $flag{c}, 
		nextid => $flag{nextid},
		}) or do { print STDERR "Can't add $_\n"; next };
    $flag{nextid}=0;
}

# Nothing on list, hehe read this !
scalar @{$urpmchecker->{repository}} or help();

# Nothing to check, checking all
if (!$flag{yc}) {
	$_->{check} = 1 foreach @{$urpmchecker->{repository}};
}

$urpmchecker->parse_list();

print STDERR "Computing dep\n" if ($flag{v});

$urpmchecker->analyze_dep();
$urpmchecker->analyze_files();
$urpmchecker->analyze_src();

my %report;
foreach my $l (@{$urpmchecker->{repository}}) {
    $l->{check} or next;
	foreach my $id (@{$l->{list}}) {
		exists $urpmchecker->{error}->{$id} or next;
		my $idinfo=$urpmchecker->{error}->{$id};
		
		$flag{p} && $idinfo->{packager} !~ /$flag{p}/ and next;
		$flag{n} && $idinfo->{fullname} !~ /$flag{n}/ && $idinfo->{sourcerpm} !~ /$flag{n}/ and next;
		my $msg;
		my %tmp;
		$msg .= " $idinfo->{fullname} ($idinfo->{sourcerpm}) [$idinfo->{level}]\n";
		foreach (@{$idinfo->{err}}) {
			push(@{$tmp{$_->{type}}->{sprintf("%s %s",$_->{msgsum},$_->{fullname})}}, $_->{msg});
		}	
		foreach my $type (keys %tmp) {
			foreach my $fullname (keys %{$tmp{$type}}) {
				my $n=scalar @{$tmp{$type}{$fullname}};
				if ($n > 3) {
					$msg.= "    $type\: (x$n) $fullname\n";
				} else {
					$msg.=join("",map { "    $type\: $_ $fullname\n" } @{$tmp{$type}{$fullname}});
				}
			}
		}
		push(@{$report{$idinfo->{packager}}},$msg);
    }
}
undef $urpmchecker;
foreach my $name (sort keys %report) {
    $flag{p} && $name !~ /$flag{p}/ and next;
    print "$name:\n";
    print @{$report{$name}}, "\n";
}
