#!/usr/bin/perl -w
use strict;
use Data::Dumper;

my $inbugsummary = 0;
my $validatedfixes = 0;
my $validatednotfixes = 0;
my $notvalidated = 0;

my %validatednotfixes = ();
my %validatedsecnotfixes = ();
my %notvalidated = ();
my %notvalidatedsec = ();

my $anyvalidated;
my $anyvalidatedincidents = 0;
my $incidents = 0;

my %timestamps = ();

open(FIND,"find /mounts/qam/testreports/ -name log|");
while (<FIND>) {
	chomp;

	# in what quarter did this happen:
	my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat $_;
	my ($dummy,$dummy1,$dummy2,$mday,$mon,$year,$dummy3,$dummy4,$dummy5) = localtime($mtime);
	# mon starts at 0
	$timestamps{$_} = sprintf("%d-%d",($year+1900),($mon/3)+1);
}
close(FIND);

my %quarters = ();

sub
addbug($$$) {
	my ($dataref,$key,$bug) = @_;
	my %data = %{$dataref};
	my %fixes = ();
	if (defined($data{$key})) {
		%fixes = %{$data{$key}};
	}
	$fixes{$bug}++;
	$data{$key} = \%fixes;
	return %data;
}

sub
removebug($$$) {
	my ($dataref,$key,$bug) = @_;
	my %data = %{$dataref};
	if (defined($data{$key})) {
		my %fixes = %{$data{$key}};
		delete $fixes{$bug};
		$data{$key} = \%fixes;
	}
	return %data;
}

foreach my $file (keys %timestamps) {
	my $stamp = $timestamps{$file};

	my %data = ();

	if (defined($quarters{$stamp})) {
		%data = %{$quarters{$stamp}};
	}
	open(LOG,"<$file");

	if ($file =~ /SUSE:Maintenance:(\d*):(\d*)/) {
		my $incident = $1;
		my $rr = $2;

		%data = addbug(\%data,'uniqueincidents',$incident);
	}

	my %thisbugs = ();

	while (<LOG>) {
		chomp;
		if (/BUGS SUMMARY/) {
			$inbugsummary=1;
			$data{'incidents'}++;
		}
		if (/bugs not validated/)  {
			$validatedfixes = 0;
			$validatednotfixes = 0;
			$notvalidated = 1;
		}
		if (/bugs validated fixed/)  {
			$validatedfixes = 1;
			$validatednotfixes = 0;
			$notvalidated = 0;
		}
		if (/bugs validated not fixed/)  {
			$validatedfixes = 0;
			$validatednotfixes = 1;
			$notvalidated = 0;
		}

		if (/INSTALL TESTS SUMMARY/) {
			$inbugsummary = 0;
			$validatedfixes = 0;
			$validatednotfixes = 0;
			$notvalidated = 0;

			$data{'anyvalidatedincidents'}++ if ($anyvalidated);
			$anyvalidated = 0;
		}
		if ($inbugsummary && /bsc#(\d*)/) {
			my $bug = $1;

			$thisbugs{$bug}++;

			if ($validatedfixes)		{ %data = addbug(\%data,'validatedfixes',$bug); }
			if ($validatedfixes && /VUL-/)	{ %data = addbug(\%data,'validatedsecfixes',$bug); }
			if ($validatednotfixes)		{ %data = addbug(\%data,'validatednotfixes',$bug); }
			if ($validatednotfixes && /VUL-/){%data = addbug(\%data,'validatedsecnotfixes',$bug); }
			if ($notvalidated)		{ %data = addbug(\%data,'notvalidated',$bug); }
			if ($notvalidated && /VUL-/)	{ %data = addbug(\%data,'notvalidatedsec',$bug); }

			$anyvalidated++ if ($validatedfixes || $validatednotfixes);
		}
		if (/^Packages:.*( kernel-|kgraft|kernel-live)/) {
			print STDERR "skipping $file due to $_\nbugs: " . (keys %thisbugs) . "\n";

			$data{'kernelincidents'}++;
			foreach my $bug (keys %thisbugs) {
				%data = addbug(\%data,'kernelbugs',$bug);

				%data = removebug(\%data,'validatedfixes',$bug);
				%data = removebug(\%data,'notvalidated',$bug);
				%data = removebug(\%data,'validatednotfixes',$bug);
				%data = removebug(\%data,'validatedsecfixes',$bug);
				%data = removebug(\%data,'notvalidatedsec',$bug);
				%data = removebug(\%data,'validatedsecnotfixes',$bug);
			}
		}
		if (/^Bug (\d*) /) {
			my %fixes = ();
			my $bug = $1;
			if (!defined($thisbugs{$bug})) {
				if (defined($data{'validatedfixes'})) {
					%fixes = %{$data{'validatedfixes'}};
				}
				if (!defined($fixes{$bug})) {
					%fixes = ();
					if (defined($data{'notvalidated'})) {
						%fixes = %{$data{'notvalidated'}};
					}
					if (!defined($fixes{$bug})) {
						print STDERR "bug $bug not listed in header section -> putting not validated\n";
						$fixes{$bug}++;
						$data{'notvalidated'} = \%fixes;
					}
				}
			}
		}
		if (/^Bug (\d*).*VUL-/) {
			my %fixes = ();
			my $bug = $1;
			if (!defined($thisbugs{$bug})) {
				if (defined($data{'validatedsecfixes'})) {
					%fixes = %{$data{'validatedsecfixes'}};
				}
				if (!defined($fixes{$bug})) {
					%fixes = ();
					if (defined($data{'notvalidatedsec'})) {
						%fixes = %{$data{'notvalidatedsec'}};
					}
					if (!defined($fixes{$bug})) {
						print STDERR "bug $bug not listed in header section -> putting not validated\n";
						$fixes{$bug}++;
						$data{'notvalidatedsec'} = \%fixes;
					}
				}
			}
		}
	}
	$quarters{$stamp} = \%data if (keys %data);
	close(LOG);
}
print "#quarter,total incidents,unique incidents,incidents with validation,validated fixes,not validated fixes, validated not fixed,validated security fixes, not validated security fixes, valided security not fixed,kernel incidents,kernel bugs\n";
foreach my $tstamp (sort keys %quarters) {
	my %data = %{$quarters{$tstamp}};
	#print $tstamp;
	#print Dumper(\%data);

	my %validatedfixes = ();	if (defined($data{'validatedfixes'}))	{ %validatedfixes = %{$data{'validatedfixes'}}; }
	my %notvalidated = ();		if (defined($data{'notvalidated'}))	{ %notvalidated = %{$data{'notvalidated'}}; }
	my %validatednotfixes = ();	if (defined($data{'validatednotfixes'})){ %validatednotfixes = %{$data{'validatednotfixes'}}; }
	my %validatedsecfixes = ();	if (defined($data{'validatedsecfixes'})){ %validatedsecfixes = %{$data{'validatedsecfixes'}}; }
	my %notvalidatedsec = ();	if (defined($data{'notvalidatedsec'}))	{ %notvalidatedsec = %{$data{'notvalidatedsec'}}; }
	my %validatedsecnotfixes = ();	if (defined($data{'validatedsecnotfixes'})) { %validatedsecnotfixes = %{$data{'validatedsecnotfixes'}}; }
	my %kernelbugs = ();		if (defined($data{'kernelbugs'}))	{ %kernelbugs = %{$data{'kernelbugs'}}; }
	my %uniqueincidents = ();		if (defined($data{'uniqueincidents'}))	{ %uniqueincidents = %{$data{'uniqueincidents'}}; }

	print $tstamp . "," . $data{'incidents'} . "," . (keys %uniqueincidents) . "," . $data{'anyvalidatedincidents'} . "," . (keys %validatedfixes) . "," . (keys %notvalidated) . "," . (keys %validatednotfixes) . "," . (keys %validatedsecfixes) . "," . (keys %notvalidatedsec) . "," . (keys %validatedsecnotfixes ). "," . $data{'kernelincidents'} . "," . (keys %kernelbugs) . "\n";
}

#print "validated fixes: " . (keys %validatedfixes) . "\n";
#print "not validated fixes: " . (keys %notvalidated) . "\n";
#print "validated not fixes: " . (keys %validatednotfixes) . "\n";
#
#print "validated security fixes: " . (keys %validatedsecfixes) . "\n";
#print "not validated security fixes: " . (keys %notvalidatedsec) . "\n";
#print "validated not fixes: " . (keys %validatedsecnotfixes) . "\n";
#
#print "incidents $incidents\n";
#print "validated any in $anyvalidatedincidents\n";
