#!/usr/bin/perl -w

# FIXME: this module -> containing product relationship needs to come from SCC

use strict;
use JSON;
use Data::Dumper;

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 %notified = ();

sub
contained($) {
	my ($module) = @_;

	my @baseproducts;

	# base products
	return () if ($module =~ /^openSUSE/);
	return () if ($module =~ /^SUSE Linux Enterprise Desktop/);
	return () if ($module =~ /^SUSE Linux Enterprise High Performance Computing/);
	return () if ($module =~ /^SUSE Linux Enterprise Server/);
	return () if ($module =~ /^SUSE Linux Enterprise Point of Service/);
	return () if ($module =~ /^SUSE Linux Enterprise High Availability/);
	return () if ($module =~ /^SUSE Enterprise Storage/);
	return () if ($module =~ /^SUSE Manager/);
	return () if ($module =~ /^SUSE CaaS Platform/);
	return () if ($module =~ /^SUSE Cloud/);
	return () if ($module =~ /^SUSE OpenStack Cloud/);
	return () if ($module =~ /^SUSE MicroOS/);
	return () if ($module =~ /^SUSE Linux Enterprise Micro/);
	return () if ($module =~ /^HPE Helion Openstack/i);
	return () if ($module =~ /^SLES-LTSS-SAP/);
	return () if ($module =~ /^SLES-LTSS-ERICSSON/);
	return () if ($module =~ /^Image/);
	return () if ($module =~ /^Container/);
	return () if ($module =~ /^SUSE Studio Onsite/);
	return () if ($module =~ /^HPE Helion OpenStack/);
	return () if ($module =~ /^SUSE Container as a Service Platform/);
	return () if ($module =~ /^SUSE Lifecycle Management Server/);
	return () if ($module =~ /^SUSE Linux Enterprise Real Time/);
	return () if ($module =~ /^SUSE Linux Enterprise Point of Sale/);
	return () if ($module =~ /^Subscription Management Tool/);
	return () if ($module =~ /^SUSE Package Hub/);
	return () if ($module =~ /SUSE Linux Enterprise Live Patching/);


	if (	($module =~ /^SUSE Linux Enterprise Software Development Kit (\d*)(.SP\d)?/) ||
		($module =~ /^SUSE Linux Enterprise Workstation Extension (\d*)(.SP\d)?/)
	) {
		my $prod = $1;
		my $sp = defined($2)?$2:"";
		@baseproducts = ("SUSE Linux Enterprise Server $prod$sp", "SUSE Linux Enterprise Server for SAP Applications $prod$sp");
		if (($prod eq "15") || ($prod eq "11") || (($prod eq "12") && ($sp ne " SP5"))) {	# filter out SLED 12 SP5.
			push @baseproducts, "SUSE Linux Enterprise Desktop $prod$sp";
		}
		return @baseproducts;
	}

	if (($module =~ /SUSE Linux Enterprise Module for SAP Applications (\d*)(.SP\d)?/)) {
		my $prod = $1;
		my $sp = defined($2)?$2:"";
		return ("SUSE Linux Enterprise Server for SAP Applications $prod$sp");
	}
	if (($module =~ /SUSE Real Time Module (\d*)(.SP\d)?/)) {
		my $prod = $1;
		my $sp = defined($2)?$2:"";
		return ("SUSE Linux Enterprise Real Time $prod$sp");
	}
	if (($module =~ /SUSE Linux Enterprise Module for HPC (\d*)(.SP\d)?/)) {
		my $prod = $1;
		my $sp = defined($2)?$2:"";
		if ($prod eq "12") {
			push @baseproducts,
			"SUSE Linux Enterprise Server 12 SP2", "SUSE Linux Enterprise Server for SAP Applications 12 SP2", "SUSE Linux Enterprise High Performance Computing 12 SP2",
			"SUSE Linux Enterprise Server 12 SP3", "SUSE Linux Enterprise Server for SAP Applications 12 SP3", "SUSE Linux Enterprise High Performance Computing 12 SP3",
			"SUSE Linux Enterprise Server 12 SP4", "SUSE Linux Enterprise Server for SAP Applications 12 SP4", "SUSE Linux Enterprise High Performance Computing 12 SP4",
			"SUSE Linux Enterprise Server 12 SP5", "SUSE Linux Enterprise Server for SAP Applications 12 SP5", "SUSE Linux Enterprise High Performance Computing 12 SP5";
		} else {
			@baseproducts = ("SUSE Linux Enterprise High Performance Computing $prod$sp");
		}
		return @baseproducts;
	}
	if (	($module =~ /SUSE Linux Enterprise Module for Basesystem (\d*)(.SP\d)?/) ||
		($module =~ /SUSE Linux Enterprise Installer Updates (\d*)(.SP\d)?/) ||
		($module =~ /SUSE Linux Enterprise Module for Development Tools (\d*)(.SP\d)?/)	||
		($module =~ /SUSE Linux Enterprise Module for Desktop Applications (\d*)(.SP\d)?/) ||
		($module =~ /SUSE Linux Enterprise Module for Python 2 (\d*)(.SP\d)?/) ||
		($module =~ /SUSE Linux Enterprise Module for Python 3 (\d*)(.SP\d)?/)

		# is unsupported, so lets not expand Package Hub for base products.
		# ($module =~ /SUSE Linux Enterprise Module for Package Hub (\d*)(.SP\d)?/)
	) {
		my $prod = $1;
		my $sp = defined($2)?$2:"";
		@baseproducts = ("SUSE Linux Enterprise Server $prod$sp", "SUSE Linux Enterprise Desktop $prod$sp","SUSE Linux Enterprise Server for SAP Applications $prod$sp", "SUSE Linux Enterprise High Performance Computing $prod$sp");

		if ($module =~ /15.SP1/) {
			push @baseproducts,"SUSE Manager Server 4.0", "SUSE Manager Proxy 4.0", "SUSE Manager Retail Branch Server 4.0", "SUSE Enterprise Storage 6";
		}
		if ($module =~ /15.SP2/) {
			push @baseproducts,"SUSE Manager Server 4.1", "SUSE Manager Proxy 4.1", "SUSE Manager Retail Branch Server 4.1", "SUSE Enterprise Storage 7";

		}
		if ($module =~ /15.SP3/) {
			push @baseproducts,"SUSE Manager Server 4.2", "SUSE Manager Proxy 4.2", "SUSE Manager Retail Branch Server 4.2", "SUSE Enterprise Storage 7.1";
		}
		if ($module =~ /15.SP4/) {
			push @baseproducts,"SUSE Manager Server 4.3", "SUSE Manager Proxy 4.3", "SUSE Manager Retail Branch Server 4.3";
		}
		return @baseproducts;
	}
	# no desktop
	if (	($module =~ /SUSE Linux Enterprise Module for Containers (\d*)(.SP\d)?/)	||
		($module =~ /SUSE Linux Enterprise Module for Public Cloud (\d*)(.SP\d)?/)	||
		($module =~ /SUSE Linux Enterprise Module for Development Tools (\d*)(.SP\d)?/)	||
		($module =~ /SUSE Linux Enterprise Module for Legacy (\d*)(.SP\d)?/)		||
		($module =~ /SUSE Linux Enterprise Module for Web Scripting (\d*)(.SP\d)?/)	||
		($module =~ /SUSE Linux Enterprise Module for Web and Scripting (\d*)(.SP\d)?/)	||
		($module =~ /SUSE Linux Enterprise Module for Advanced Systems Management (\d*)(.SP\d)?/) ||
		($module =~ /SUSE Linux Enterprise Module for Toolchain (\d*)(.SP\d)?/)		||
		($module =~ /SUSE Linux Enterprise Module for Server Applications (\d*)(.SP\d)?/)
	) {
		my $prod = $1;
		my $sp = defined($2)?$2:"";
		@baseproducts = ("SUSE Linux Enterprise Server $prod$sp", "SUSE Linux Enterprise Server for SAP Applications $prod$sp");

		push @baseproducts , "SUSE Linux Enterprise High Performance Computing $prod$sp" if ($prod >= 12);

		if ($module =~ /15.SP1/) {
			push @baseproducts,"SUSE Manager Server 4.0", "SUSE Manager Proxy 4.0", "SUSE Manager Retail Branch Server 4.0", "SUSE Enterprise Storage 6";
		}
		if ($module =~ /15.SP2/) {
			push @baseproducts,"SUSE Manager Server 4.1", "SUSE Manager Proxy 4.1", "SUSE Manager Retail Branch Server 4.1", "SUSE Enterprise Storage 7";
		}
		if ($module =~ /15.SP3/) {
			push @baseproducts,"SUSE Manager Server 4.2", "SUSE Manager Proxy 4.2", "SUSE Manager Retail Branch Server 4.2", "SUSE Enterprise Storage 7.1";
		}
		if ($module =~ /15.SP4/) {
			push @baseproducts,"SUSE Manager Server 4.3", "SUSE Manager Proxy 4.3", "SUSE Manager Retail Branch Server 4.3";
		}
		if (!defined($prod)) {	print STDERR "parsing $module failed?\n"; }
		if ($prod eq "12") {
			push @baseproducts,
			"SUSE Linux Enterprise Server 12 SP3", "SUSE Linux Enterprise Server for SAP Applications 12 SP3",
			"SUSE Linux Enterprise Server 12 SP4", "SUSE Linux Enterprise Server for SAP Applications 12 SP4",
			"SUSE Linux Enterprise Server 12 SP5", "SUSE Linux Enterprise Server for SAP Applications 12 SP5";
		}
		return @baseproducts;
	}

	if (	($module =~ /SUSE Linux Enterprise High Availability Extension (\d*)(.SP\d)?/)	||
		($module =~ /SUSE Linux Enterprise High Availability GEO Extension (\d*)(.SP\d)?/)	||
		($module =~ /SUSE Linux Enterprise High Availability (\d*)(.SP\d)?/)
	)  {
		my $prod = $1;
		my $sp = defined($2)?$2:"";

		@baseproducts = ("SUSE Linux Enterprise Server $prod$sp");
		return @baseproducts if ($prod eq "11");	# sle11 only had server

		push @baseproducts , "SUSE Linux Enterprise High Performance Computing $prod$sp";
		return @baseproducts if ($prod eq "12");	# sle12 has server + sle hpc 

		# sle15 has more
		push @baseproducts , "SUSE Linux Enterprise Server for SAP Applications $prod$sp";

		if ($module =~ /15.SP1/) {
			push @baseproducts,"SUSE Manager Server 4.0", "SUSE Manager Proxy 4.0", "SUSE Manager Retail Branch Server 4.0";
		}
		if ($module =~ /15.SP2/) {
			push @baseproducts,"SUSE Manager Server 4.1", "SUSE Manager Proxy 4.1", "SUSE Manager Retail Branch Server 4.1";
		}
		if ($module =~ /15.SP3/) {
			push @baseproducts,"SUSE Manager Server 4.2", "SUSE Manager Proxy 4.2", "SUSE Manager Retail Branch Server 4.2";
		}
		if ($module =~ /15.SP4/) {
			push @baseproducts,"SUSE Manager Server 4.3", "SUSE Manager Proxy 4.3", "SUSE Manager Retail Branch Server 4.3";
		}

		return @baseproducts;
	}
#	if (	($module =~ /SUSE Linux Enterprise Live Patching (\d*)(.SP\d)?/)
#	) {
#		my $prod = $1;
#		my $sp = defined($2)?$2:"";
#		@baseproducts = ("SUSE Linux Enterprise Server $prod$sp", "SUSE Linux Enterprise Server for SAP Applications $prod$sp", "SUSE Linux Enterprise High Performance Computing $prod$sp");
#
#		if ($module =~ /15.SP3/) {
#			push @baseproducts,"SUSE Linux Enterprise Micro 5.1","SUSE Linux Enterprise Micro 5.2";
#		}
#		if ($module =~ /15.SP4/) {
#			push @baseproducts,"SUSE Linux Enterprise Micro 5.3";
#		}
#		return @baseproducts;
#	}
	if (	($module =~ /SUSE Linux Enterprise Module for (SUSE Manager (Proxy|Server) \d.\d)/)) {
		@baseproducts = ($1);
		return @baseproducts;
	}
	if (!defined($notified{$module})) {
		print STDERR "notsure where $module is contained?\n";
		$notified{$module} = 1;
	}
	return ();
}

1;
