#!/usr/bin/perl -w
use strict;

my $dummy;
my $year;
my $mon;
my $mday;
($dummy,$dummy,$dummy,$mday,$mon,$year,$dummy,$dummy,$dummy) = localtime(time);

$year += 1900;
my $date = sprintf("$year%02d%02d", $mon+1, $mday);

my $dn = `dirname $0`;chomp($dn);
my $pwd = `pwd`;chomp($pwd);
if ($dn !~ /^\//) { $dn = $pwd . "/" . $dn; }
push @INC,$dn;
my $cverepobase=`dirname $dn`;
chomp($cverepobase);

require CanDBReader;

use LWP;
use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->agent("cve-database/bug.pl");
$ua->timeout(300);      # we have very long queries... but lets keep it to 5 minutes

my %allbugs = ();
my %embargoedbugs = ();
foreach my $cve (keys %CanDBReader::bugzillas) {
	foreach my $bug (split(/,/,$CanDBReader::bugzillas{$cve})) {
		$allbugs{$bug} = 1;
		$embargoedbugs{$bug} = 1 if ($CanDBReader::embargoed{$cve});
	}
}
my @allbugs = keys %allbugs;

my @bugupdates = ();

open(ACCESSDENIED,">accessdenied");

my $count = 0;
my $countnostatus = 0;
foreach my $bug (@allbugs) {
	if (defined($CanDBReader::bugzilla_state{$bug}) && ($CanDBReader::bugzilla_state{$bug} =~ /RESOLVED/)) {
		$count++;
		$countnostatus++ if (!defined($CanDBReader::bugzilla_resolution{$bug}));
	}
}

print "Total bugs: " . @allbugs . "\n";
print "Resolved bugs: $count\n";
print "	Without resolution: $countnostatus\n";
print "Open bugs: " . (@allbugs - $count) ."\n";

$count = 1000;

my $nextisprio = -1;
my $nextisseverity = -1;

while (my $bug = pop @allbugs) {
	if (defined($CanDBReader::bugzilla_state{$bug})) {
		if (defined($CanDBReader::bugzilla_priority{$bug})) {
			if ($CanDBReader::bugzilla_state{$bug} =~ /RESOLVED/ && defined($CanDBReader::bugzilla_resolution{$bug})) {
				print STDERR "." if -t STDERR;
				next;
			}
			if ($CanDBReader::bugzilla_state{$bug} =~ /VERIFIED/ && defined($CanDBReader::bugzilla_resolution{$bug})) {
				print STDERR "." if -t STDERR;
				next;
			}
			if ($CanDBReader::bugzilla_state{$bug} =~ /CLOSED/ && defined($CanDBReader::bugzilla_resolution{$bug})) {
				print STDERR "." if -t STDERR;
				next;
			}
		}
	}
	last if ($count == 0);

	$count--;
	my $state = "";
	my $solution = "";
	$state = $CanDBReader::bugzilla_resolution{$bug} if (defined($CanDBReader::bugzilla_resolution{$bug}));
	$solution = $CanDBReader::bugzilla_state{$bug} if (defined($CanDBReader::bugzilla_state{$bug}));
	print STDERR "fetching $bug (currently $solution / $state )\n" if -t STDERR;
	my $url = "http://bugzilla.suse.com/show_bug.cgi?id=$bug";

	my $req = HTTP::Request->new(GET => $url);
	$req->header( 'Accept' => 'text/html' );
	my $res = $ua->request($req);
	if ($res->is_success) {
		if ($res->content eq "") {
			print STDERR "empty result on retrieving $url?\n";
		}
	} else {
		print STDERR "ERROR on: $url:\n";
		print STDERR $res->status_line;
		print STDERR "\n";
		last;
	}

	my @lines = split(/\n/,$res->content);

	my $getnext = 0;
	my $accessdenied = 0;
	foreach my $line (@lines) {
		if ($line =~ /<title>Access Denied/) {
			print STDERR "... access denied\n" if -t STDERR;
			print ACCESSDENIED "$bug\n";
			$accessdenied = 1;
		}
		if ($getnext) {
			$getnext = 0;
		       	if ($line !~ /span/ && $line !~ /</) {
				# print "Resolution is $line\n";

				$line =~ s/\s*//;
				if ($CanDBReader::bugzilla_resolution{$bug} ne $line) {
					print STDERR "Resolution of $bug changed from $CanDBReader::bugzilla_resolution{$bug} to $line\n" if -t STDERR;
					push @bugupdates,"$bug,$date,RESOLUTION:$line\n";
					$CanDBReader::bugzilla_resolution{$bug} = $line;
				}
			}
		}
		if ($line =~ /class.*bugzilla-suse-com.*/) {
			if ($line =~ /bz_status_(\S*)\s/) {
				my $status = $1;

				if ($CanDBReader::bugzilla_state{$bug} ne $status) {
					print STDERR "Status of $bug changed from $CanDBReader::bugzilla_state{$bug} to $status\n" if -t STDERR;
					push @bugupdates,"$bug,$date,STATUS:$status\n";
					$CanDBReader::bugzilla_state{$bug} = $status;
				}
			}
		}
		if ($line =~ /static_bug_status">(\S*)/) {
			my $status = $1;
			$getnext = 1;

			if ($CanDBReader::bugzilla_state{$bug} ne $status) {
				print STDERR "Status of $bug changed from $CanDBReader::bugzilla_state{$bug} to $status\n" if -t STDERR;
				push @bugupdates,"$bug,$date,STATUS:$status\n";
				$CanDBReader::bugzilla_state{$bug} = $status;
			}
		}
		if ($line =~ />Priority</) {
			$nextisprio = 2;
		}
		if ($nextisprio == 0) {
			chomp $line;
			$line =~ s/<[^>]*>//g;
			$line =~ s/^\s*//;
			$line =~ s/\s*$//;
			print STDERR "priority: |$line|\n";
			if (!defined($CanDBReader::bugzilla_priority{$bug}) || ($CanDBReader::bugzilla_priority{$bug} ne $line)) {
				print STDERR "-> changed\n";
				push @bugupdates,"$bug,$date,PRIORITY:$line\n";
			}
		}
		$nextisprio--;
		if ($line =~ />Severity</) {
			$nextisseverity = 1;
		}
		if ($nextisseverity == 0) {
			chomp $line;
			$line =~ s/<[^>]*>//g;
			$line =~ s/^\s*//;
			$line =~ s/\s*$//;
			print STDERR "severity |$line|\n";
			if (!defined($CanDBReader::bugzilla_severity{$bug}) || ($CanDBReader::bugzilla_severity{$bug} ne $line)) {
				print STDERR "-> changed\n";
				push @bugupdates,"$bug,$date,SEVERITY:$line\n";
			}
		}
		$nextisseverity--;

#         <tr>
#     <th class="field_label">
#       <label  accesskey="i">
#         <a href="page.cgi?id=glossary.html#priority">Priority</a></label>:
#     </th>
#     <td>P5 - None
#      <label for="severity"><b>Severity</b></label>:
#      Critical
#     </td>
#   </tr>



		#class="bugzilla-suse-com bz_bug bz_status_NEW bz_product_SUSE_Security_Incidents bz_component_Incidents bz_bug_1188466 yui-skin-sam">
		#      <span id="static_bug_status">NEW
		#</span>
	}
	if (!$accessdenied && defined($embargoedbugs{$bug})) {
		print "Might not be embargoed anymore: $bug\n";
	}
	sleep(3);
}
open(BUGUPDATES,">>data/bugzillas");
print BUGUPDATES join("",@bugupdates);
close(BUGUPDATES);

close(ACCESSDENIED);
