Sitecore.Automation.Framework.psm1

Import-Module "$PSScriptRoot\Common\Initialization-Module.psm1" -Force
Import-Module "$PSScriptRoot\Common\Utils-Module.psm1" -Force
$ErrorActionPreference = "Stop"

Set-ExecutionPolicy Bypass -Scope Process -Force

function SetGlobals {
    [CmdletBinding()]
    Param
    (
        [bool]$Offline,
        [bool]$AutoConfirm,
        [string]$RunningCommand
    )

    $global:Configuration = $null
    $global:Offline = $Offline
    $global:AutoConfirm = $AutoConfirm
    $global:RunningCommand = $RunningCommand
    $dir = Get-Location
    $global:RunningDirectory = $dir

    $global:Pipelines = Get-Content -Raw -Path "$PSScriptRoot\common\Pipelines.json" | ConvertFrom-Json
}

function LoadCofigurations {
    [CmdletBinding()]
    Param
    (
        [string]$ConfigName
    )

    $dir = Get-Location
    $configFile = "$dir\$ConfigName"

    if (!(Test-Path $configFile)) {
        throw "Please, provide Configuration JSON file."
    }

    $global:Configuration = Get-Content -Raw -Path $configFile | ConvertFrom-Json
}

function EnsureRunAsAdmin {
    ## Verify elevated
    ## https://superuser.com/questions/749243/detect-if-powershell-is-running-as-administrator
    $elevated = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")
    if ($elevated -eq $false) {
        throw "In order to use SAF, please run this script elevated. If you are using Visual Studio Plug-in, please run Visual Studio as Administrator."
    }
}

function Initialize {

    Write-Output "`n`n------------------------------------------------------"
    Write-Output "--- Welcome to Sitecore Automation Framework (SAF) ---"
    Write-Output "------------------------------------------------------`n`n"
    
    EnsureRunAsAdmin

    Write-Warning "SAF initialization will start after 3 seconds..."
    Start-Sleep -s 3

    if ($global:Offline -eq $false) {
        ConfigurePSGallery
        ConfigureChoco
        RefreshEnvironment
    }
    else {
        Write-Warning "SAF is running in offline mode. It won't use PSGallery and Chocolatey. That means you'll have more to do by yourself ;)"
        Start-Sleep -s 5
    }

    Write-Warning "SAF initialization is done."
}

function Install-Sitecore {
    [CmdletBinding()]
    Param
    (
        [switch]$Force,
        [switch]$Offline,
        [switch]$AutoConfirm
    )

    SetGlobals -Offline $Offline.IsPresent -AutoConfirm $AutoConfirm.IsPresent -RunningCommand "Install-Sitecore -Force:`$$Force -Offline:`$$Offline -AutoConfirm:`$$AutoConfirm"
    
    Initialize
    Import-Module "$PSScriptRoot\Install\OnPrem\Install-Module.psm1" -Force

    $dir = Get-Location
    $configFile = "$dir\SitecoreInstallConfiguration.json"
    if ((Test-Path $configFile)) {
        LoadCofigurations -ConfigName "SitecoreInstallConfiguration.json"
        StartInstall -Force:$Force
    }
    else {
        LoadCofigurations -ConfigName "SitecoreQuickInstallConfiguration.json"
        StartInstall -Force:$Force -Quickly
    }

    Write-Warning "SAF is DONE!"
}

function Uninstall-Sitecore {
    [CmdletBinding()]
    Param
    (
        [switch]$Force,
        [switch]$Offline,
        [switch]$AutoConfirm
    )

    SetGlobals -Offline $Offline.IsPresent -AutoConfirm $AutoConfirm.IsPresent -RunningCommand "Uninstall-Sitecore -Force:`$$Force -Offline:`$$Offline -AutoConfirm:`$$AutoConfirm"
    
    $dir = Get-Location
    $configFile = "$dir\SitecoreInstallConfiguration.json"
    if ((Test-Path $configFile)) {
        LoadCofigurations -ConfigName "SitecoreInstallConfiguration.json"
    }
    else {
        LoadCofigurations -ConfigName "SitecoreQuickInstallConfiguration.json"
    }
    
    Initialize

    Import-Module "$PSScriptRoot\Install\OnPrem\Uninstall-Module.psm1" -Force
    StartUninstallSitecore -Force:$Force

    Write-Warning "SAF is DONE!"
}

function Install-Solr {
    [CmdletBinding()]
    Param
    (
        [switch]$Force,
        [switch]$Offline,
        [switch]$AutoConfirm
    )

    SetGlobals -Offline $Offline.IsPresent -AutoConfirm $AutoConfirm.IsPresent -RunningCommand "Install-Solr -Force:`$$Force -Offline:`$$Offline -AutoConfirm:`$$AutoConfirm"

    $dir = Get-Location
    $configFile = "$dir\SitecoreSolrConfiguration.json"
    if ((Test-Path $configFile)) {
        LoadCofigurations -ConfigName "SitecoreSolrConfiguration.json"
    }
    else {
        LoadCofigurations -ConfigName "SitecoreQuickInstallConfiguration.json"
    }

    Initialize

    Import-Module "$PSScriptRoot\Install\OnPrem\Solr\Solr-Module.psm1" -Force
    StartInstall -Force:$Force

    Write-Warning "SAF is DONE!"
}

function New-SolrConfigSets {
    [CmdletBinding()]
    Param
    (
        [switch]$Force,
        [switch]$Offline,
        [switch]$AutoConfirm
    )

    SetGlobals -Offline $Offline.IsPresent -AutoConfirm $AutoConfirm.IsPresent -RunningCommand "New-SolrConfigSets -Force:`$$Force -Offline:`$$Offline -AutoConfirm:`$$AutoConfirm"
    LoadCofigurations -ConfigName "SitecoreSolrConfiguration.json"
    Initialize

    Import-Module "$PSScriptRoot\Install\OnPrem\Solr\Solr-Module.psm1" -Force
    GenerateNewSolrConfigSets -Force:$Force

    Write-Warning "SAF is DONE!"
}

function New-SolrCoresViaHTTP {
    [CmdletBinding()]
    Param
    (
        [switch]$Force,
        [switch]$Offline,
        [switch]$AutoConfirm
    )

    SetGlobals -Offline $Offline.IsPresent -AutoConfirm $AutoConfirm.IsPresent -RunningCommand "New-SolrCoresViaHTTP -Force:`$$Force -Offline:`$$Offline -AutoConfirm:`$$AutoConfirm"
    LoadCofigurations -ConfigName "SitecoreSolrConfiguration.json"
    Initialize

    Import-Module "$PSScriptRoot\Install\OnPrem\Solr\Solr-Module.psm1" -Force
    CreateNewSolrCoresViaHTTP -Force:$Force

    Write-Warning "SAF is DONE!"
}

function Uninstall-Solr {
    [CmdletBinding()]
    Param
    (
        [switch]$Force,
        [switch]$Offline,
        [switch]$AutoConfirm
    )

    SetGlobals -Offline $Offline.IsPresent -AutoConfirm $AutoConfirm.IsPresent -RunningCommand "Uninstall-Solr -Force:`$$Force -Offline:`$$Offline -AutoConfirm:`$$AutoConfirm"

    $dir = Get-Location
    $configFile = "$dir\SitecoreSolrConfiguration.json"
    if ((Test-Path $configFile)) {
        LoadCofigurations -ConfigName "SitecoreSolrConfiguration.json"
    }
    else {
        LoadCofigurations -ConfigName "SitecoreQuickInstallConfiguration.json"
    }

    Initialize

    Import-Module "$PSScriptRoot\Install\OnPrem\Uninstall-Module.psm1" -Force
    StartUninstallSolr -Force:$Force

    Write-Warning "SAF is DONE!"
}
 
function New-SSLCerts {
    [CmdletBinding()]
    Param
    (
        [switch]$Force,
        [switch]$Offline,
        [switch]$AutoConfirm
    )

    SetGlobals -Offline $Offline.IsPresent -AutoConfirm $AutoConfirm.IsPresent -RunningCommand "New-SSLCerts -Force:`$$Force -Offline:`$$Offline -AutoConfirm:`$$AutoConfirm"

    LoadCofigurations -ConfigName "SAFSSLConfiguration.json"
    Initialize

    Import-Module "$PSScriptRoot\Common\SSL\SSL-Module.psm1" -Force
    StartSSLCertsPipeline -Pipeline "newSSLCerts" -Force:$Force

    Write-Warning "SAF is DONE!"
}

function Clear-SAFCache {
    [CmdletBinding()]
    Param
    (
        [switch]$Offline,
        [switch]$AutoConfirm
    )
    
    SetGlobals -Offline $Offline.IsPresent -AutoConfirm $AutoConfirm.IsPresent -RunningCommand "Clear-Cache -Offline:`$$Offline -AutoConfirm:`$$AutoConfirm"
    Initialize

    Write-Output "Clear cache started..."
    Import-Module "$PSScriptRoot\Common\Run-Pipelines.psm1" -Force
    RemoveTemp
    Write-Output "Cache is cleared."
    Write-Warning "SAF is DONE!"
}

# function Set-SSLCertsAppPoolsAccess {
# [CmdletBinding()]
# Param
# (
# [switch]$Offline,
# [switch]$AutoConfirm
# )

# SetGlobals -Offline $Offline.IsPresent -AutoConfirm $AutoConfirm.IsPresent -RunningCommand "Set-SSLCertsAppPoolsAccess -Offline:`$$Offline"
# LoadCofigurations -ConfigName "SAFSSLConfiguration.json"
# Initialize

# Import-Module "$PSScriptRoot\Common\SSL\SSL-Module.psm1" -Force
# $clientCertName = BuildSitecoreClientCertName -Prefix $global:Configuration.prefix
# SetSSLCertsAppPoolsAccess -ClientCertName $clientCertName -Hostnames $global:Configuration.hostNames

# Write-Warning "SAF is DONE!"
# }

Export-ModuleMember -Function "Install-Sitecore"
Export-ModuleMember -Function "Uninstall-Sitecore"
Export-ModuleMember -Function "Install-Solr"
Export-ModuleMember -Function "New-SolrConfigSets"
Export-ModuleMember -Function "New-SolrCoresViaHTTP"
Export-ModuleMember -Function "Uninstall-Solr"
Export-ModuleMember -Function "New-SSLCerts"
Export-ModuleMember -Function "Clear-SAFCache"
# Export-ModuleMember -Function "Set-SSLCertsAppPoolsAccess"