Install/OnPrem/XP/Scaled/ContentDelivery/Install-CD.ps1

. "$PSScriptRoot\..\..\..\..\InstallParams.ps1" -Force
. "$PSScriptRoot\..\..\..\Solr\SolrParams.ps1" -Force
Import-Module "$PSScriptRoot\..\..\..\..\..\Common\SSL\SSL-Module.psm1" -Force
Import-Module "$PSScriptRoot\..\..\..\..\..\Common\WebAdministration-Module.psm1" -Force
Import-Module "$PSScriptRoot\..\..\..\Install-Module.psm1" -Force
$ErrorActionPreference = "Stop"

$prefix = $global:Configuration.prefix
$license = $global:Configuration.license
$sqlServer = $global:Configuration.sql.serverName
$sqlSitecorePassword = $global:Configuration.sql.sitecorePassword
$solrURL = $SolrServiceURL
$xConnectCollectionService = $global:Configuration.xConnect.collectionServiceURL
$xConnectReferenceDataService = $global:Configuration.xDB.referenceDataServiceURL
$marketingAutomationOperationsService = $global:Configuration.xDB.automationOperationsServiceURL
$marketingAutomationReportingService = $global:Configuration.xDB.automationReportingServiceURL
$exmCryptographicKey = $global:Configuration.exm.cryptographicKey
$exmAuthenticationKey = $global:Configuration.exm.authenticationKey
$package = Get-ChildItem -Path "$SAFInstallPackageDir\*" -Include *cd.scwdp.zip*
$sifConfiguration = GetSIFConfiguration -Name "sitecore-XP1-cd.json" -PackageName $package -DefaultValue "$PSScriptRoot\sitecore-XP1-cd.json"

$clientCert = BuildSitecoreClientCertName -Prefix $prefix
$serverCert = BuildSitecoreServerCertName -Prefix $prefix

$count = 1

foreach ($cd in $global:Configuration.sitecore) {
    $hostNames = $cd.hostNames
    $siteName = $hostNames[0]
    $installDir = $cd.installDir

    Write-Output "Testing installation of Sitecore CD$count..."
    if (TestURI -Uri "https://$siteName") {
        Write-Output "Sitecore CD$count has been installed before. Going forward..."
    }
    else {
        Write-Output "Install Sitecore CD$count started..."

        $sitecoreParams = @{
            Path                                 = $sifConfiguration
            Package                              = $package.FullName
            LicenseFile                          = $license
            SqlDbPrefix                          = $prefix
            SqlServer                            = $sqlServer
            SqlCoreUser                          = "$($prefix)_coreuser"
            SqlCorePassword                      = $sqlSitecorePassword
            SqlWebUser                           = "$($prefix)_webuser"
            SqlWebPassword                       = $sqlSitecorePassword
            SqlFormsUser                         = "$($prefix)_formsuser"
            SqlFormsPassword                     = $sqlSitecorePassword
            SqlExmMasterUser                     = "$($prefix)_exmmasteruser"
            SqlExmMasterPassword                 = $sqlSitecorePassword
            SqlMessagingUser                     = "$($prefix)_messaginguser"
            SqlMessagingPassword                 = $sqlSitecorePassword
            SolrCorePrefix                       = $prefix
            SolrUrl                              = $solrURL
            Sitename                             = $siteName
            XConnectCert                         = $clientCert
            XConnectCollectionService            = $xConnectCollectionService
            XConnectReferenceDataService         = $xConnectReferenceDataService
            MarketingAutomationOperationsService = $marketingAutomationOperationsService
            MarketingAutomationReportingService  = $marketingAutomationReportingService
            EXMCryptographicKey                  = $exmCryptographicKey
            EXMAuthenticationKey                 = $exmAuthenticationKey
            InstallDirectory                     = $installDir
        }
        Install-SitecoreConfiguration @sitecoreParams
        AddWebBindings -SiteName $siteName -HostNames $hostNames
        AddWebBindings -SiteName $siteName -HostNames $hostNames -SSLCert $serverCert
        Write-Output "Install Sitecore CD$count done."
    }

    $count = $count + 1
}