#!/usr/bin/perl -w

use strict;
use JSON;
use Data::Dumper;
use LWP;
use LWP::UserAgent;
use DateTime;

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);

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

my $token = "xx";

sub get_url($) {
	my ($url) = @_;

	# print STDERR "getting $url\n";
# Create a request
	my $req = HTTP::Request->new(GET => $url);
	$req->header( 'Accept' => 'application/json' );
	$req->header( 'Authorization' => "Token $token" );
	my $res = $ua->request($req);
	if ($res->is_success) {
		if ($res->content eq "") {
			print STDERR "empty result on retrieving $url?\n";
		}
		return $res->content;
	} else {
		print STDERR "ERROR on: $url:\n";
		print STDERR $res->status_line;
		print STDERR "\n";
		return "";
	}
}

sub get_url_curl($) {
	my ($url) = @_;

	print STDERR "curl --insecure -s -H 'Authorization: Token SMASHTOKEN' \"$url\"|";
	open(CURL,"curl -s -H 'Authorization: Token $token' \"$url\"|");
	my @lines = <CURL>;
	close(CURL);
	return join("",@lines);
}

%SMASHData::prod2cpe = (
	'openSUSE Leap 15.1' => 'cpe:/o:opensuse:leap:15.1',
	'openSUSE Leap 15.1 NonFree' => 'cpe:/o:opensuse:leap:15.1',
	'openSUSE Leap 15.2' => 'cpe:/o:opensuse:leap:15.2',
	'openSUSE Leap 15.2 NonFree' => 'cpe:/o:opensuse:leap:15.2',
	'openSUSE Leap 15.3' => 'cpe:/o:opensuse:leap:15.3',
	'openSUSE Leap 15.3 NonFree' => 'cpe:/o:opensuse:leap:15.3',
	'SUSE Linux Enterprise High Availability 15' => 'cpe:/o:suse:sle-ha:15',
	'SUSE Linux Enterprise Desktop 15' => 'cpe:/o:suse:sled:15',
	'SUSE Linux Enterprise Desktop 15 SP1' => 'cpe:/o:suse:sled:15:sp1',
	'SUSE Linux Enterprise Desktop 15 SP2' => 'cpe:/o:suse:sled:15:sp2',
	'SUSE Linux Enterprise Storage 6' => 'cpe:/o:suse:ses:6',
	'SUSE Linux Enterprise Storage 7' => 'cpe:/o:suse:ses:7',
	'SUSE Linux Enterprise Real Time Extension 12 SP5' => 'cpe:/o:suse:suse-linux-enterprise-rt:12:sp5',
	'SUSE Linux Enterprise Software Development Kit 12 SP2' => 'cpe:/o:suse:sle-sdk:12:sp2',
	'SUSE Linux Enterprise Module for Open Buildservice Development Tools 15 SP2' => 'cpe:/o:suse:sle-module-development-tools-obs:15:sp2',
	'SUSE Linux Enterprise Build System Kit 12' => 'cpe:/o:suse:sle-bsk:12',
	'SUSE Linux Enterprise Build System Kit 12 SP1' => 'cpe:/o:suse:sle-bsk:12:sp1',
	'SUSE Linux Enterprise Build System Kit 12 SP2' => 'cpe:/o:suse:sle-bsk:12:sp2',
	'SUSE Linux Enterprise Build System Kit 12 SP3' => 'cpe:/o:suse:sle-bsk:12:sp3',
	'SUSE Linux Enterprise Build System Kit 12 SP4' => 'cpe:/o:suse:sle-bsk:12:sp3',
	'SUSE Linux Enterprise High Availability 12 SP2' => 'cpe:/o:suse:sle-ha:12:sp2',
	'SUSE Linux Enterprise High Availability 12 SP3' => 'cpe:/o:suse:sle-ha:12:sp3',
	'SUSE Linux Enterprise High Availability 12 SP4' => 'cpe:/o:suse:sle-ha:12:sp4',
	'SUSE Linux Enterprise High Availability 12 SP5' => 'cpe:/o:suse:sle-ha:12:sp5',
	'SUSE Linux Enterprise High Availability 15 SP1' => 'cpe:/o:suse:sle-ha:15:sp1',
	'SUSE Linux Enterprise High Availability 15 SP2' => 'cpe:/o:suse:sle-ha:15:sp2',
	'SUSE Linux Enterprise High Performance Computing 15-ESPOS' => 'cpe:/o:suse:sle_hpc-espos:15',
	'SUSE Linux Enterprise High Performance Computing 15-LTSS' => 'cpe:/o:suse:sle_hpc-ltss:15',
	'SUSE Linux Enterprise High Performance Computing 15 SP1-ESPOS' => 'cpe:/o:suse:sle_hpc-espos:15:sp1',
	'SUSE Linux Enterprise High Performance Computing 15 SP1-LTSS' => 'cpe:/o:suse:sle_hpc-ltss:15:sp1',
	'SUSE Linux Enterprise Live Patching 12' => 'cpe:/o:suse:sle-live-patching:12',
	'SUSE Linux Enterprise Server for Raspberry Pi 12 SP2' => 'cpe:/o:suse:sles:12:sp2',
);

sub
get_cpe($) {
	my ($product) = @_;

	if (defined($SMASHData::prod2cpe{$product})) {
		return $SMASHData::prod2cpe{$product};
	}
	if ($product =~ /-LTSS/) {
		$product =~ s/-LTSS/ LTSS/;
		if (defined($SMASHData::prod2cpe{$product})) {
			return $SMASHData::prod2cpe{$product};
		}
	}
	if ($product =~ /-ESPOS/) {
		$product =~ s/-ESPOS/ ESPOS/;
		if (defined($SMASHData::prod2cpe{$product})) {
			return $SMASHData::prod2cpe{$product};
		}
	}
	$product =~ s/ for \S*$//;	# filter the for x86_64
	if (defined($SMASHData::prod2cpe{$product})) {
		return $SMASHData::prod2cpe{$product};
	}
	return undef;
}

open(PRODUCTJSON,"$cverepobase/data/sccproductlist.json")||die;
my $json = join("",<PRODUCTJSON>);
close(PRODUCTJSON);

# this is an array of products.

my @map = ();
eval {
	my $mapref = decode_json($json);
	@map = @{$mapref};
} or do {
	warn "json invalid: $json\n";
	$json = "";
};

foreach my $prod (@map) {
	#print Dumper(\$prod);
	print $prod->{"friendly_name"} . " => " . $prod->{"cpe"} . "\n";


	foreach my $ext (@{$prod->{extensions}}) {
		#print Dumper(\$ext);
		
		print "extension: " . $ext->{"friendly_name"} . " => " . $ext->{"cpe"} . "\n";
	}
}

0;
