// If any modder adds useKopernicusSolarPanels = false to a module instead of a part, add it to the part:
@PART:HAS[@MODULE:HAS[#useKopernicusSolarPanels[?alse]]]:LAST[KOP_MULTI_STAR]
{
	%useKopernicusSolarPanels = false
}

// Uses regular expressions to convert any case variants like FalSe to false
@PART:HAS[#useKopernicusSolarPanels[*]]:LAST[KOP_MULTI_STAR]
{
    // This cfg will enable KopernicusSolarPanels
    // to allow support for multiple lightsources
    // 
    // If you want to avoid this, add "useKopernicusSolarPanels = false" to the PART node
    // That will stop Kopernicus from changing the behaviour of SolarPanel
    @useKopernicusSolarPanels,* ^= :F:f:
    @useKopernicusSolarPanels,* ^= :A:a:
    @useKopernicusSolarPanels,* ^= :L:l:
    @useKopernicusSolarPanels,* ^= :S:s:
    @useKopernicusSolarPanels,* ^= :E:e:
}

//First delete all old "KopernicusSolarPanels" fixers
@PART:HAS[@MODULE[ModuleDeployableSolarPanel]]:LAST[KOP_MULTI_STAR]
{
	!MODULE[KopernicusSolarPanels] {}
}
//Count the stars...
@Kopernicus_config:LAST[KOP_MULTI_STAR]
{
	%__kop_star_count = 0
}
@Kopernicus:LAST[KOP_MULTI_STAR]
{
    @Body:HAS[@Template[Sun]]
    {
        *@Kopernicus_config/__kop_star_count += 1
    }
}
//Put variable in all applicable parts.
@PART:HAS[@MODULE[ModuleDeployableSolarPanel],~useKopernicusSolarPanels[false]]
{
	%__kop_star_count = 0
}
@PART:HAS[@MODULE[ModuleCurvedSolarPanel],~useKopernicusSolarPanels[false]]
{
	%__kop_star_count = 0
}
@PART:HAS[@MODULE[SSTUSolarPanelStatic],~useKopernicusSolarPanels[false]]
{
	%__kop_star_count = 0
}
@PART:HAS[@MODULE[SSTUSolarPanelDeployable],~useKopernicusSolarPanels[false]]
{
	%__kop_star_count = 0
}
@PART:HAS[@MODULE[SSTUModularPart],~useKopernicusSolarPanels[false]]
{
	%__kop_star_count = 0
}
@PART:HAS[@MODULE[ModuleROSolar],~useKopernicusSolarPanels[false]]
{
	%__kop_star_count = 0
}
@PART:HAS[@MODULE[ModuleDeployableSolarPanel],~useKopernicusSolarPanels[false]]:LAST[KOP_MULTI_STAR]
{
    %__kop_star_count = #$@Kopernicus_config/__kop_star_count$
}
@PART:HAS[@MODULE[ModuleCurvedSolarPanel],~useKopernicusSolarPanels[false]]:LAST[KOP_MULTI_STAR]
{
    %__kop_star_count = #$@Kopernicus_config/__kop_star_count$
}
@PART:HAS[@MODULE[SSTUSolarPanelStatic],~useKopernicusSolarPanels[false]]:LAST[KOP_MULTI_STAR]
{
    %__kop_star_count = #$@Kopernicus_config/__kop_star_count$
}
@PART:HAS[@MODULE[SSTUSolarPanelDeployable],~useKopernicusSolarPanels[false]]:LAST[KOP_MULTI_STAR]
{
    %__kop_star_count = #$@Kopernicus_config/__kop_star_count$
}
@PART:HAS[@MODULE[SSTUModularPart],~useKopernicusSolarPanels[false]]:LAST[KOP_MULTI_STAR]
{
    %__kop_star_count = #$@Kopernicus_config/__kop_star_count$
}
@PART:HAS[@MODULE[ModuleROSolar],~useKopernicusSolarPanels[false]]:LAST[KOP_MULTI_STAR]
{
    %__kop_star_count = #$@Kopernicus_config/__kop_star_count$
}

//=======================================================================================================
// Solar panels support
//=======================================================================================================
@PART:HAS[@MODULE[ModuleDeployableSolarPanel]:HAS[#chargeRate[>0]],#__kop_star_count[>1]]:NEEDS[!RealismOverhaul,!Kerbalism]:LAST[KOP_MULTI_STAR]
{
  // duplicate every ModuleDeployableSolarPanel
  // Some parts may use multiple MDSP modules,
  // so we have to add a KopernicusSolarPanel module each of them
  +MODULE[ModuleDeployableSolarPanel],*
  {
    // delete all values
    -* = delete
    // delete all possible nodes
    -powerCurve {}
    //-temperatureEfficCurve {}
    -timeEfficCurve {}
    -UPGRADES {}
    // rename the module to KopernicusSolarPanel
    name = KopernicusSolarPanel
  }
}
@PART:HAS[@MODULE[ModuleCurvedSolarPanel],#__kop_star_count[>1]]:NEEDS[!RealismOverhaul,!Kerbalism,NearFutureSolar]:LAST[KOP_MULTI_STAR]
{
	MODULE
	{
		name = KopernicusSolarPanel
	}
}

@PART:HAS[@MODULE[SSTUSolarPanelStatic],#__kop_star_count[>1]]:NEEDS[!RealismOverhaul,!Kerbalism,SSTU]:LAST[KOP_MULTI_STAR]
{
	MODULE
	{
		name = KopernicusSolarPanel
	}
}
@PART:HAS[@MODULE[SSTUSolarPanelDeployable],#__kop_star_count[>1]]:NEEDS[!RealismOverhaul,!Kerbalism,SSTU]:LAST[KOP_MULTI_STAR]
{
	MODULE
	{
		name = KopernicusSolarPanel
	}
}

// Only patch SSTUModularPart if it has a solar panel. 
// This isn't fail-proof as a modular part can have switcheable solar panels and "Solar-None" as the default option,
// but we want to avoid adding the SolarPanelFixer on parts that don't have a solar panel.
@PART:HAS[@MODULE:[SSTUModularPart],!#currentSolar[Solar-None],#__kop_star_count[>1]]:NEEDS[!RealismOverhaul,!Kerbalism,SSTU]:LAST[KOP_MULTI_STAR]
{
	MODULE
	{
		name = KopernicusSolarPanel
	}
}

@PART:HAS[@MODULE[ModuleROSolar],#__kop_star_count[>1]]:NEEDS[!Kerbalism,RealismOverhaul]:LAST[KOP_MULTI_STAR]
{
  MODULE
  {
    name = KopernicusSolarPanel
  }
}

// clean up
@PART:HAS[#useKopernicusSolarPanels[*]]:LAST[KOP_MULTI_STAR]
{
	!useKopernicusSolarPanels = delete
}

@PART:HAS[@MODULE:HAS[#useKopernicusSolarPanels[*]]]:LAST[KOP_MULTI_STAR]
{
    @MODULE,*:HAS[#useKopernicusSolarPanels[*]]
	{
		!useKopernicusSolarPanels = delete
	}
}

@PART:HAS[#__kop_star_count]:LAST[KOP_MULTI_STAR]
{
    !__kop_star_count = delete
}

@Kopernicus_config:LAST[KOP_MULTI_STAR]
{
    !__kop_star_count = delete
}
