Install/OnPrem/Install-Module.psm1

Import-Module "$PSScriptRoot\..\..\Common\Run-Pipelines.psm1" -Force
$ErrorActionPreference = "Stop"

function ResolvePipeline {
    [CmdletBinding()]
    Param
    (
        [switch]$Quickly
    )

    if ($Quickly.IsPresent -and ($global:Configuration.system.serverRole -eq "AllInOne")) {
        return "quicklyInstall$($global:Configuration.system.hosting)$($global:Configuration.system.serverRole)-$($global:Configuration.system.sitecoreMode)"
    }

    if ($null -ne $global:Configuration.system.sitecoreMode) {
        return "install$($global:Configuration.system.hosting)$($global:Configuration.system.serverRole)-$($global:Configuration.system.sitecoreMode)"
    }
    else {
        return "install$($global:Configuration.system.hosting)$($global:Configuration.system.serverRole)"
    }
}

function StartInstall {
    [CmdletBinding()]
    Param
    (
        [switch]$Force,
        [switch]$Quickly
    )
    
    $pipeline = ResolvePipeline -Quickly:$Quickly
    RunSteps -Pipeline $pipeline -Force:$Force -RunExtensions
}

function GetSIFConfiguration {
    [CmdletBinding()]
    Param
    (
        [string]$Name,
        [string]$PackageName,
        [string]$DefaultValue
    )

    $releases = Get-ChildItem -Directory "$PSScriptRoot\VersionSpecificConfigs" | Select-Object -ExpandProperty BaseName
    
    foreach ($release in $releases) {
        if($PackageName.Contains($release)){
            $patchConfiguration = "$PSScriptRoot\VersionSpecificConfigs\$release\$Name"
            Write-Warning $patchConfiguration
            if(Test-Path $patchConfiguration) {
                return $patchConfiguration
            }
        }
    }

    return $DefaultValue
}

Export-ModuleMember -Function "StartInstall"
Export-ModuleMember -Function "GetSIFConfiguration"