$ErrorActionPreference = "Stop"





$RootPath = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent


$ignoreRestartPowerShell = $false





. $RootPath\AnywhereNow\Scripts\Shared.ps1


. $RootPath\AnywhereNow\Scripts\SharedPnpFunctions.ps1


. $RootPath\AnywhereNow\Scripts\Updates\UpdateHelpers.ps1


. $RootPath\AnywhereNow\Scripts\Updates\Site\SiteUpdates_6.0.ps1


. $RootPath\AnywhereNow\Scripts\Updates\Site\SiteUpdates_6.1.ps1


. $RootPath\AnywhereNow\Scripts\Updates\Site\SiteUpdates_6.2.ps1


. $RootPath\AnywhereNow\Scripts\Updates\Site\SiteUpdates_6.3.ps1


. $RootPath\AnywhereNow\Scripts\Updates\Site\SiteUpdates_8.0.ps1


. $RootPath\AnywhereNow\Scripts\Updates\Site\SiteUpdates_8.1.ps1


. $RootPath\AnywhereNow\Scripts\Updates\Site\SiteUpdates_8.2.ps1


. $RootPath\AnywhereNow\Scripts\Updates\Site\SiteUpdates_8.3.ps1


. $RootPath\AnywhereNow\Scripts\Updates\SiteCollection\SiteCollectionUpdates_6.2.ps1


. $RootPath\AnywhereNow\Scripts\Updates\SiteCollection\SiteCollectionUpdates_6.3.ps1


. $RootPath\AnywhereNow\Scripts\Updates\SiteCollection\SiteCollectionUpdates_8.0.ps1


. $RootPath\AnywhereNow\Scripts\Updates\SiteCollection\SiteCollectionUpdates_8.1.ps1


. $RootPath\AnywhereNow\Scripts\Updates\SiteCollection\SiteCollectionUpdates_8.2.ps1


. $RootPath\AnywhereNow\Scripts\Updates\SiteCollection\SiteCollectionUpdates_8.3.ps1





# Update info


$templateUpdateVersion = "8.3.0.2"


$assetsUpdateVersion = "8.3.0.1"





$firstPossibleReleasePatchLevel = 18162


$baselinePatchLevel = 18243


$templateUpdateMajorVersion = 8


$templateUpdateMinorVersion = 3


$templateUpdatePatchLevel = 0


$templateUpdateRevLevel = 1


$assetsUpdateMajorVersion = 8


$assetsUpdateMinorVersion = 3


$assetsUpdatePatchLevel = 0


$assetsUpdateRevLevel = 1





function SetTemplateVersion($updateVersion, [switch]$assets, [switch]$ignoreWhenHigherVersion) 


{


	$versionListTitle = "Version"


	if ($assets) 


	{


		$versionListTitle = "AssetsVersion"


	}


	


	$versionItems = Get-PnPListItem -List $versionListTitle


	$versionItem = $versionItems | Select-Object -first 1


	


	if ($versionItem -eq $null)


	{


		# No version item exists, create one


		if (![string]::IsNullOrEmpty($updateVersion))


		{


			AddPnPListItem $versionListTitle @{"Title" = $updateVersion}


		}


		return


	}


	


	if ($ignoreWhenHigherVersion)


	{


		$version = ConvertVersionStringToNumber $versionItem["Title"]


		$versionToCheck = ConvertVersionStringToNumber $updateVersion


		if ($version -gt $versionToCheck)


		{


			$updateVersion = [string]::Empty


		}


	}





	if (![string]::IsNullOrEmpty($updateVersion))


	{


		SetPnPListItem $versionListTitle $versionItem @{"Title" = $updateVersion}


	}


}





function ApplyTemplatePatch($siteTitle, $patchCheck, $languageInfo, $newSite)


{


	if ($siteTitle -ne $null) 


	{


		$siteTitle = "$siteTitle - "


	}


	else 


	{


		$siteTitle = ""


	}


	


	Info $siteTitle"Apply patch $templateUpdateVersion"


	try


	{


		Execute6.0SiteUpdates


		Execute6.1SiteUpdates


		Execute6.2SiteUpdates


		Execute6.3SiteUpdates


		Execute8.0SiteUpdates


		Execute8.1SiteUpdates


		Execute8.2SiteUpdates


		Execute8.3SiteUpdates


	}


	catch 


	{


		Error "Cannot apply patch $templateUpdateVersion :: $($_.Exception.Message)"


		return


	}





	Info $siteTitle"Apply patch $templateUpdateVersion done"


}





function ApplySiteCollectionPatch($patchCheck, $newSiteCollection)


{


	Info "Apply site collection patch $assetsUpdateVersion"


	try


	{


		Execute6.2SiteCollectionUpdates


		Execute6.3SiteCollectionUpdates


		Execute8.0SiteCollectionUpdates


		Execute8.1SiteCollectionUpdates


		Execute8.2SiteCollectionUpdates


		Execute8.3SiteCollectionUpdates


	}


	catch 


	{


		Error "Cannot apply site collection patch $assetsUpdateVersion :: $($_.Exception.Message)"


		return


	}





	Info "Apply site collection patch $assetsUpdateVersion done"


}





function ExecuteUpdate


{


	# Check vars


	$checkVars = CheckEnvironmentalVariables


	if ($checkVars -eq $false) 


	{


		Warn "Required variables not set"


		return


	}





	if ($SPversion -ne "2013" -and $SPversion -ne "2016" -and $SPversion -ne "2019" -and $SPversion -ne "online" -and $SPversion -ne "foundation") 


	{


		Warn "SP version '$SPversion' not supported. Use online, 2013, 2016, 2019 or foundation"


		return


    }


	


	$connected = Connect -Url $scurl -ShowError


	if ($connected -eq $false) 


	{


		Warn "Cannot connect to sitecollection $scurl"


		return


	}





	Info "Site updater started"


	Info "Connected to site collection $scurl"


	if ($SPversion -eq "online")


	{


		if (!$(CheckDenyAddCustomisePagesFeature)) 


		{


			Warn "Custom Script is disabled. Classic portal.aspx will not work - use SPFx Portal Web Part instead."


		}


	}





	$ctx = Get-PnPContext





	Info "Assets update version $assetsUpdateVersion"


	Info "Template update version $templateUpdateVersion"


	Info "Checking assets to update..."


	$assetsVersionList = GetPnPList "AssetsVersion"


	if ($assetsVersionList -eq $null) 


	{


		AssetsVersionList


	}


	


	$assetsVersionItems = Get-PnPListItem -List "AssetsVersion"


	$assetsVersionItem = $assetsVersionItems | Select-Object -first 1


	$assetsVersion = $assetsVersionItem["Title"]


	$assetsVersionInfo = $assetsVersion.Split(".")


	$assetsVersionNumber = ConvertVersionStringToNumber $assetsVersion


	if ([int]$assetsVersionInfo[0] -lt $assetsUpdateMajorVersion -or


		([int]$assetsVersionInfo[0] -eq $assetsUpdateMajorVersion -and [int]$assetsVersionInfo[1] -lt $assetsUpdateMinorVersion) -or 


		([int]$assetsVersionInfo[0] -eq $assetsUpdateMajorVersion -and [int]$assetsVersionInfo[1] -eq $assetsUpdateMinorVersion -and [int]$assetsVersionInfo[2] -lt $assetsUpdatePatchLevel) -or 


		([int]$assetsVersionInfo[0] -eq $assetsUpdateMajorVersion -and [int]$assetsVersionInfo[1] -eq $assetsUpdateMinorVersion -and [int]$assetsVersionInfo[2] -eq $assetsUpdatePatchLevel -and [int]$assetsVersionInfo[3] -lt $assetsUpdateRevLevel))


	{


		Info "$assetsVersion - Update available"


		$ctx_updateSite = Get-PnPContext


		$updateSite = Get-PnPWeb


		$updateSite_RelativeURL = $updateSite.ServerRelativeUrl


		ApplySiteCollectionPatch $assetsVersionNumber


	}


	else


	{


		Info "$assetsVersion - Up to date"


	}





	# NOTE: Subsites are deprecated per MC1117115 and no longer supported


	# UCC sites are now deployed to the site collection root only


	$sites = Get-PnPWeb





	Info "Checking sites to update..."


	$hasTemplateSites = $false


	foreach ($site in $sites) 


	{


		Connect -Url $site.Url | Out-Null


		$ctx_updateSite = Get-PnPContext


		$updateSite = Get-PnPWeb


		$updateSite_Id = $updateSite.Id


		$updateSite_Title = $updateSite.Title


		$updateSite_RelativeURL = $updateSite.ServerRelativeUrl


		$lcid = Get-PnPProperty -ClientObject $updateSite -property "Language"


		$languageInfo = GetLanguage($lcid)


		$siteTitle = $site.Title


		$versionList = GetPnPList "Version"


		if ($versionList -eq $null -or $versionList.ItemCount -eq 0) 


		{


			continue


		}





		$hasTemplateSites = $true


		$items = Get-PnPListItem -List "Version"


		$item = $items | Select-Object -first 1


		$version = $item["Title"]


		$versionInfo = $version.Split(".")


		$versionNumber = ConvertVersionStringToNumber $version


		if ([int]$versioninfo[0] -lt 6) 


		{


			Warn "$siteTitle - $version - Cannot apply patch"


			continue


		}





		if ([int]$versioninfo[0] -lt $templateUpdateMajorVersion -or


			([int]$versioninfo[0] -eq $templateUpdateMajorVersion -and [int]$versioninfo[1] -lt $templateUpdateMinorVersion) -or 


			(([int]$versioninfo[0] -eq $templateUpdateMajorVersion -and [int]$versioninfo[1] -eq $templateUpdateMinorVersion -and [int]$versioninfo[2] -lt $templateUpdatePatchLevel) -or 


			([int]$versioninfo[2] -ge $firstPossibleReleasePatchLevel -and [int]$versioninfo[2] -le $baselinePatchLevel)) -or 


			([int]$versioninfo[0] -eq $templateUpdateMajorVersion -and [int]$versioninfo[1] -eq $templateUpdateMinorVersion -and [int]$versioninfo[2] -eq $templateUpdatePatchLevel -and [int]$versioninfo[3] -lt $templateUpdateRevLevel))


		{


			Info "$siteTitle - $version - Update available"


			ApplyTemplatePatch $siteTitle $versionNumber $languageInfo


		}


		else 


		{


			Info "$siteTitle - $version - Up to date"


		}


		


		FixLookups $site.Id $siteTitle


	}





	if ($hasTemplateSite -eq $false) 


	{


		Warn "No sites with UCC Config Template"


	}





	Disconnect-PnPOnline


	Info "Done"


}





function ExecuteNewSiteUpdate


{


	$ctx_updateSite = Get-PnPContext


	$updateSite = Get-PnPWeb


	$updateSite_Id = $updateSite.Id


	$updateSite_Title = $updateSite.Title


	$updateSite_RelativeURL = $updateSite.ServerRelativeUrl


	$lcid = Get-PnPProperty -ClientObject $updateSite -property "Language"


	$languageInfo = GetLanguage($lcid)


	$versionItems = Get-PnPListItem -List "Version"


	$versionItem = $versionItems | Select-Object -first 1


	$versionNumber = 0


	if ($versionItem -ne $null)


	{


		$versionNumber = ConvertVersionStringToNumber $versionItem["Title"]


	}


	ApplyTemplatePatch $null $versionNumber $languageInfo $true


}





function ExecuteNewSiteCollectionUpdate


{


	$ctx_updateSite = Get-PnPContext


	$updateSite = Get-PnPWeb


	$updateSite_RelativeURL = $updateSite.ServerRelativeUrl


	$assetsVersionItems = Get-PnPListItem -List "AssetsVersion"


	$assetsVersionItem = $assetsVersionItems | Select-Object -first 1


	$versionNumber = ConvertVersionStringToNumber $assetsVersionItem["Title"]


	ApplySiteCollectionPatch $versionNumber $true


}


