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

use Data::Dumper;

my $dn = `dirname $0`;chomp($dn);
my $cverepobase=`dirname $dn`;
chomp($cverepobase);

%PInt::image2cves = ();
%PInt::image2pkg2changes = ();
%PInt::image2pkg2vers = ();
%PInt::image2cve2pkgs = ();
%PInt::cve2images = ();
%PInt::image2url = ();
%PInt::image2changes = ();

%PInt::images = (
	"sles15sp6chostaws" => {
		"name" => "SLES 15 SP6 CHOST Image for Amazon EC2",
		"regexp" => "amazon/suse-sles-15-sp6-chost-byos-v([0-9]*)-hvm-ssd-x86_64",
	},
	"sles15sp5chostaws" => {
		"name" => "SLES 15 SP5 CHOST Image for Amazon EC2",
		"regexp" => "amazon/suse-sles-15-sp5-chost-byos-v([0-9]*)-hvm-ssd-x86_64",
	},
	"sles15sp4chostaws" => {
		"name" => "SLES 15 SP4 CHOST Image for Amazon EC2",
		"regexp" => "amazon/suse-sles-15-sp4-chost-byos-v([0-9]*)-hvm-ssd-x86_64",
	},
	"sles15sp3chostaws" => {
		"name" => "SLES 15 SP3 CHOST Image for Amazon EC2",
		"regexp" => "amazon/suse-sles-15-sp3-chost-byos-v([0-9]*)-hvm-ssd-x86_64",
	},
	"sles15sp2chostaws" => {
		"name" => "SLES 15 SP2 CHOST Image for Amazon EC2",
		"regexp" => "amazon/suse-sles-15-sp2-chost-byos-v([0-9]*)-hvm-ssd-x86_64",
	},
	"sles15sp1chostaws" => {
		"name" => "SLES 15 SP1 CHOST Image for Amazon EC2",
		"regexp" => "amazon/suse-sles-15-sp1-chost-byos-v([0-9]*)-hvm-ssd-x86_64",
	},
	"sles15gachostaws" => {
		"name" => "SLES 15 GA CHOST Image for Amazon EC2",
		"regexp" => "amazon/suse-sles-15-chost-byos-v([0-9]*)-hvm-ssd-x86_64",
	},
	"sles15sp6chostgoogle" => {
		"name" => "SLES 15 SP6 CHOST Image for Google",
		"regexp" => "google/sles-15-sp6-chost-byos-v([0-9]*)",
	},
	"sles15sp5chostgoogle" => {
		"name" => "SLES 15 SP5 CHOST Image for Google",
		"regexp" => "google/sles-15-sp5-chost-byos-v([0-9]*)",
	},
	"sles15sp4chostgoogle" => {
		"name" => "SLES 15 SP4 CHOST Image for Google",
		"regexp" => "google/sles-15-sp4-chost-byos-v([0-9]*)",
	},
	"sles15sp3chostgoogle" => {
		"name" => "SLES 15 SP3 CHOST Image for Google",
		"regexp" => "google/sles-15-sp3-chost-byos-v([0-9]*)",
	},
	"sles15sp2chostgoogle" => {
		"name" => "SLES 15 SP2 CHOST Image for Google",
		"regexp" => "google/sles-15-sp2-chost-byos-v([0-9]*)",
	},
	"sles15sp1chostgoogle" => {
		"name" => "SLES 15 SP1 CHOST Image for Google",
		"regexp" => "google/sles-15-sp1-chost-byos-v([0-9]*)",
	},
	"sles15sp1chostazure" => {
		"name" => "SLES 15 SP1 CHOST Image for Azure",
		"regexp" => "microsoft/suse-sles-15-sp1-chost-byos-v([0-9]*)",
	},
	"sles15sp2chostazure" => {
		"name" => "SLES 15 SP2 CHOST Image for Azure",
		"regexp" => "microsoft/suse-sles-15-sp2-chost-byos-v([0-9]*)",
	},
	"sles15sp3chostazure" => {
		"name" => "SLES 15 SP3 CHOST Image for Azure",
		"regexp" => "microsoft/suse-sles-15-sp3-chost-byos-v([0-9]*)",
	},
	"sles15sp4chostazure" => {
		"name" => "SLES 15 SP4 CHOST Image for Azure",
		"regexp" => "microsoft/suse-sles-15-sp4-chost-byos-v([0-9]*)",
	},
	"sles15sp5chostazure" => {
		"name" => "SLES 15 SP5 CHOST Image for Azure",
		"regexp" => "microsoft/suse-sles-15-sp5-chost-byos-v([0-9]*)",
	},
	"sles15sp6chostazure" => {
		"name" => "SLES 15 SP6 CHOST Image for Azure",
		"regexp" => "microsoft/suse-sles-15-sp6-chost-byos-v([0-9]*)",
	},
);

foreach my $pint (<$cverepobase/data/pint/*>) {
	my $provider = $pint;

	$provider =~ s#.*/##;

	open(PINT,"<$pint")||die "$pint: $!";
	my $name;
	my $changelog;
	while (<PINT>) {
		$name = $1 if (/name="([^"]*)"/);
		$changelog = $1 if (/changeinfo="([^"]*)"/);

		if (defined($name) && defined($changelog)) {
			$PInt::image2url{"$provider/$name"} = $changelog;
		}
		undef $name;
		undef $changelog;
	}
	close(PINT);
}

my @pintproviders = <$cverepobase/smash/pintdata/publiccloudimagechangeinfo.suse.com/*>;

foreach my $pintprovider (sort @pintproviders) {
	# print "$pintprovider\n";

	my $provider = $pintprovider;

	$provider =~ s#.*/##;

	my @images = <$pintprovider/*>;
	foreach my $image (sort @images) {
		next unless (-f "$image/index.html");


		my $tag = $image;
		$tag =~ s#.*/##;

		my $cname = "$provider/$tag";

		# print "\t$image\n";
		# cve_fixes.html
		#	CVEs
		# image_changes.html

		# index.html
		# package_changelogs.html
		#	structured pacakge by change... use for content

		my $package = "unknown";
		my $inchanges = 0;
		my $curchanges = "";
		my $allchanges = "This image update for $cname contains the following changes:\n";
		if (open(HTML,"<$image/package_changelogs.html")) {
			while (<HTML>) {
				if (/<dt>(.*)<\/dt>/) {
					$package = $1;
					$allchanges .= "Package $package was updated:\n\n";
					# print "package $package\n";
				}

				my $cveline = $_;
				while ($cveline =~ /(CVE-\d\d\d\d-\d\d\d\d\d*)/) {
					my $cve = $1;
					#print "\t$cve\n";
					$cveline =~ s/(CVE-\d\d\d\d-\d\d\d\d\d*)//g;

					$PInt::image2cves{$cname}->{$cve} = 1;
					$PInt::cve2images{$cve}->{$cname} = 1;

					$PInt::image2cve2pkgs{$cname}->{$cve}->{$package} = 1;
				}

				# rest does changelog fetching ... perhaps use a HTML reader
				if (/<pre>(.*)/) {
					$curchanges = $1;
					$inchanges = 1;
					next;
				}
				if (/(.*)<\/pre>/) {
					$curchanges .= $1;
					$inchanges = 0;

					$PInt::image2pkg2changes{$cname}->{$package} = $curchanges;
					$allchanges .= $curchanges . "\n\n";

					$curchanges = "";
				}
				if ($inchanges) {
					$curchanges .= $_;
					next;
				}
			}
			close(HTML);
		} else {
			warn "$image/package_changelogs.html:$!\n";
		}
		$PInt::image2changes{$cname} = $allchanges;
		if (open(HTML,"<$image/package_version_changes.html")) {
			while (<HTML>) {
				#       <li>SUSEConnect 0.3.29 (build 3.6.1)</li>

				if (/<li>(\S*)\s*(\S*)\s\(build (\S*)\)</) {
					$PInt::image2pkg2vers{$cname}->{$1} = "$2-$3";
				}
			}
			close(HTML);
		} else {
			warn "$image/package_version_changes.html:$!\n";
		}
		# package_version_changes.html
	}
}

#print Dumper(\%PInt::image2cves);
#print Dumper(\%PInt::cve2images);
#print Dumper(\%PInt::image2url);
#print Dumper(\%PInt::image2pkg2vers);
#print Dumper(\%PInt::image2changes);

1;
