#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell

# $Id: distlintbatch,v 1.12 2004/01/02 13:02:36 othauvin Exp $

use strict;
use Mail::Sendmail;
use urpmchecker;

# Ghost sub
sub get_maintener { undef };
our $o;
my ($batch_file, %options);

while (local $_=shift(@ARGV)) {
    /^-t$/ and do { $options{test} = 1; next };
    /^-o$/ and do { $options{output} = shift(@ARGV); next };
    /^--no-mail$/ and do { $options{nomail} = 1; next };
    $batch_file = $_;
}
#print "$options{test}\n";
$batch_file && -f $batch_file or die "Distlint batch mode, mail the error\nUsage $0 [ -t ] batch_file\n   -t, dump  mail on stdout istead mail\n";
my $urpmchecker = urpmchecker->new;
require $batch_file;
$urpmchecker->{arch} = $o->{arch};

foreach my $r (@{$o->{repository}}) {
	my $nextlevel = 1;
	
	foreach (@{$r->{list}}) {
		$urpmchecker->add($_, { nochkdep => $r->{nochkdep}, chksrc => $r->{chksrc},
		                        nochkcf => $r->{nochkcf},  check => $r->{check},
                                nextlevel => $nextlevel, update => $r->{update}, 
                                chkreb => $r->{chkreb}, }
				        ) or die "can't add $_, aborting\n";
		$nextlevel = 0;
	}
}

my %maint = get_maintener();
$urpmchecker->parse_list;
$urpmchecker->analyze;

my $result = $urpmchecker->{error};
undef $urpmchecker;
my %mailtosend;

foreach my $package (keys %$result) {
	# Those variables are used in the eval
	my ($packager, $maintener) = ($result->{$package}{packager}, $maint{$result->{$package}{sname}});
	my ($msg, %tmp);
	my $mailto = join(', ', grep { /\@/ } ($o->{mailto}, eval($o->{mailto_eval})));
	$mailto or next;
    
	foreach (@{$result->{$package}{err}}) {
		push(@{$tmp{$_->{type}}{sprintf("%s %s", $_->{msgsum}, $_->{fullname})}}, $_->{msg});
	}
	
	$msg = "$result->{$package}->{fullname} ($result->{$package}->{sourcerpm}) [$result->{$package}->{level}]\n";
	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}});
			}
		}
	}
	$mailtosend{$mailto} .= "$msg\n";
}

# Genereting the mail
if (! $options{nomail}) {
my ($to, $msg);
while (($to, $msg) = each %mailtosend) {
	my @currtime = localtime(time());
	my %mail = (
		'Message-ID' => "<".$currtime[5].$currtime[4].$currtime[3].$currtime[2].$currtime[1].$currtime[0]."\@distlint>\n",
		smtp => ($o->{smtp} || 'localhost.localdomain'),
		To => $to,
		From => $o->{mailfrom},
		'Reply-to' => $o->{replyto},
		'X-Mailer' => "Distlint Bot",
    );
	$mail{"Subject: "} = $o->{mailsubject};
    $o->{cc} and $mail{Cc} = $o->{cc};
	$mail{"Message: "} = 
"$o->{mailheader}

$msg
-- 
$o->{mailfoot}";
    if ($options{test}) {
        foreach ('Message-ID', 'To', 'From', 'Reply-to', 'Message: ') {
            print "$_ : $mail{$_}\n";
        }
        print "\n------\n";
    } else {
        sendmail(%mail) or warn $Mail::Sendmail::error;
    }    
}

if ($o->{mailto_summary}) {
	my @currtime = localtime(time());
	my %mail = (
		'Message-ID' => "<".$currtime[5].$currtime[4].$currtime[3].$currtime[2].$currtime[1].$currtime[0]."\@distlint>\n",
		smtp => ($o->{smtp} || 'localhost.localdomain'),
		To => $o->{mailto_summary},
		From => $o->{mailfrom},
		'Reply-to' => $o->{replyto},
		'X-Mailer' => "Distlint Bot",
    );
	$mail{"Subject: "} = $o->{mailsubject};
    $o->{cc} and $mail{Cc} = $o->{cc};
	$mail{"Message: "} = 
"Summary of distlint check:

".join("\n", map { "Sent to ".$_."\n".$mailtosend{$_} } sort keys %mailtosend) .
"-- 
$o->{mailfoot}";
	$options{test} or sendmail(%mail) or warn $Mail::Sendmail::error;
}
} # if (! $options{nomail} )

if ($options{output}) {
	if (open(OUTPUT, "> $options{output}")) {
		print OUTPUT join("\n", map { $_."\n".$mailtosend{$_} } sort keys %mailtosend);
		close OUTPUT;
	} else	{
		warn "Can't open $options{output} for output\n";
	}
}
