#Import .NET Framework into Powershell session if needed -
If (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type) {
	$certCallback = @"
		using System;
		using System.Net;
		using System.Net.Security;
		using System.Security.Cryptography.X509Certificates;
		public class ServerCertificateValidationCallback
		{
			public static void Ignore()
			{
				if(ServicePointManager.ServerCertificateValidationCallback ==null)
				{
					ServicePointManager.ServerCertificateValidationCallback +=
						delegate
						(
							Object obj,
							X509Certificate certificate,
							X509Chain chain,
							SslPolicyErrors errors
						)
						{
							return true;
						};
				}
			}
		}
"@
	Add-Type $certCallback
}
[ServerCertificateValidationCallback]::Ignore()

#Enable SSL/TLS
Try {
	# Set TLS 1.2 (3072), then TLS 1.1 (768), then TLS 1.0 (192)
	# Use integers because the enumeration values for TLS 1.2 and TLS 1.1 won't
	# exist in .NET 4.0, even though they are addressable if .NET 4.5+ is
	# installed (.NET 4.5 is an in-place upgrade).
	[System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor 192
} Catch {
	Write-Output 'Unable to set PowerShell to use TLS 1.2 and TLS 1.1 due to old .NET Framework installed. If you see underlying connection closed or trust errors, you may need to upgrade to .NET Framework 4.5+ and PowerShell v3+.'
}
$FunctionsFolder = $ENV:SystemDrive + '\Ambitions\Functions'
New-Item -ItemType Directory -Force -Path $FunctionsFolder
#Imports functions from Github
$FunctionURLs = (Invoke-WebRequest https://github.com/AmbitionsTechnologyGroup/ATG-PS-Functions/raw/master/Functions/URL-List.csv -UseBasicParsing).Content | ConvertFrom-Csv -Delimiter ","
$Global:DestinationFiles = @()
$FunctionURLs | ForEach-Object {
	$DestinationFiles += $FunctionsFolder + $_.Verb + ".psm1"
}



If ($PSVersionTable.PSEdition -like "Desktop") {
	$FunctionURLs | ForEach-Object {
		$Dest = $Env:temp + "\" + $_.Verb + ".psm1"
		$Final = $FunctionsFolder + "\" + $_.Verb + ".psm1"
		(Invoke-WebRequest $($_.URL) -UseBasicParsing).Content | Out-File -FilePath $Dest
		$New = (Get-FileHash $Dest -Algorithm MD5).Hash
		$Existing = (Get-FileHash $Final -Algorithm MD5 -ErrorAction SilentlyContinue).Hash
		If ($New -ne $Existing) {
			Write-Output "Updating $Final"
			Move-Item -Path $Dest -Destination $Final -Force
		} Else {
			Remove-Item -Path $Dest -Force -ErrorAction SilentlyContinue
		}
	}
} Else {
	$FunctionURLs | ForEach-Object -Parallel {
		$Dest = $Env:temp + "\" + $_.Verb + ".psm1"
		(Invoke-WebRequest $($_.URL) -UseBasicParsing).Content | Out-File -FilePath $Dest
	}
	$FunctionURLs | ForEach-Object {
		$Dest = $Env:temp + "\" + $_.Verb + ".psm1"
		$Final = $FunctionsFolder + "\" + $_.Verb + ".psm1"
		$New = (Get-FileHash $Dest -Algorithm MD5).Hash
		$Existing = (Get-FileHash $Final -Algorithm MD5 -ErrorAction SilentlyContinue).Hash
		If ($New -ne $Existing) {
			Write-Output "Updating $Final"
			Move-Item -Path $Dest -Destination $Final -Force
		} Else {
			Remove-Item -Path $Dest -Force -ErrorAction SilentlyContinue
		}
	}
}