my $i = $ARGV[0];

my @output = ();
my @lines = ();
open (my $maint, '<', "MAINTAINERS")
    or die "$P: Can't open MAINTAINERS file 'MAINTAINERS': $!\n";
while (<$maint>) {
    my $line = $_;
    push(@lines, $line);
}
close($maint);

my $low_index = $i - 1;

while ($lines[$low_index] =~ /^.:/ && $low_index > 0) {
    $low_index--;
}

my $high_index = $i - 1;
while ($lines[$high_index] =~ /^.:/ && $high_index < $#lines) {
    $high_index++;
}

push(@output, "A file pattern line in this section of the MAINTAINERS file in linux-next\n");
push(@output, "does not have a match in the linux source files.\n");
push(@output, "\n");
push(@output, "This could occur because a matching filename was never added, was deleted\n");
push(@output, "or renamed in some other commit.\n");
push(@output, "\n");
push(@output, "The commits that added and if found renamed or removed the file pattern\n");
push(@output, "are shown below.\n");
push(@output, "\n");
push(@output, "Please fix this defect appropriately.\n");
push(@output, "\n");

push(@output, "1: ---------------------------------------------------------------------------\n");
push(@output, "\n");
push(@output, "linux-next MAINTAINERS section:\n");
push(@output, "\n");

my $file = $lines[$i-1];
$file =~ s/^[A-Z]:\s+//;
chomp $file;

my $section = $lines[$low_index];

while ($low_index < $high_index) {
    push(@output, "-->") if ($low_index == $i-1);
    push(@output, "\t" . ($low_index+1) . "\t" . $lines[$low_index]);
    $low_index++;
}

push(@output, "\n");
push(@output, "2: ---------------------------------------------------------------------------\n");
push(@output, "\n");

my @blame = `git blame -L$i,+1 MAINTAINERS`;
my @commits = split(' ', $blame[0]);
my $commit = $commits[0];
my @commit_lines = `git log -1 --stat $commit`;

push(@output, "The most recent commit that added or modified file pattern '$file':\n");
push(@output, "\n");
push(@output, @commit_lines);

push(@output, "\n");
push(@output, "3: ---------------------------------------------------------------------------\n");
push(@output, "\n");

my @follow = `git log --follow -1 --stat -M -- '$file'`;
if (!@follow) {
    push(@output, "No commit with file pattern '$file' was found\n");
} else {
    my $commit = $follow[0];
    $commit =~ s/^commit //;
    @follow = `git log --stat -1 -M $commit`;
    push(@output, "The last commit with a real presence of file pattern '$file':\n");
    push(@output, "\n");
    push(@output, @follow);
}

my @maintainers = grep(/^\s+\d+\s+[LRM]:.*$/, @output);
my @signers = grep(/^\s+.*-by:.*$/, @output);

open (my $fileh, '>', "patterns/$i.txt");

$section =~ s/^\s+\d+\s+//;
$section =~ s/\n$//;

print $fileh "Subject: Bad file pattern in MAINTAINERS section '$section'\n";
print $fileh "\n";
print $fileh (@output);

foreach my $m (@maintainers) {
    $m =~ s/^\s+\d+\s+.:\s+//;
    $m =~ s/\s*\(.*\).*$//;
}

foreach my $s (@signers) {
    $s =~ s/^\s*[\w\-]+-by:\s*//;
}

#print $fileh ("\nmaintainers:\n\n");
#print $fileh (@maintainers);
#print $fileh ("\nsigners:\n\n");
#print $fileh (@signers);

my @emails = ();
foreach my $email (@maintainers,@signers) {
    push(@emails, $email) unless $a{$email}++;
}

#print $fileh ("\nemails:\n\n");
#print $fileh (@emails);

my $cc = "";
foreach my $email (@emails) {
    my $t = $email;
    $t =~ s/\n$//;
    $t =~ s/"//g;
    $cc .= " --cc \"$t\"";
}

close $fileh;

my $gsm = "git send-email --to \"linux-kernel\@vger.kernel.org\"" . $cc . " patterns/$i.txt";
print("$gsm\n");

#my @gsm_output = `$gsm`;

#print(@gsm_output);
