param($dest,$PGHome,$prodName)

Set-StrictMode -Off

if ($PGHome -eq $null)
{
	'Please run the script from Script Editor.'
	return
}

while ([string]::IsNullOrEmpty($dest) -or [System.IO.Directory]::CreateDirectory($dest) -eq $null)
{
	if ([string]::IsNullOrEmpty($dest))
	{
		$dest = Get-WmiObject Win32_LogicalDisk `
			| where {$_.DriveType -eq 2 `
				-and @(1,2,3,4,5,6,7,8,9,10,13,14,15,16,17,18,19,20,21,22) -notcontains $_.MediaType} `
			| select -ExpandProperty DeviceID -First 1
	}

	if ($dest -ne $null -and $dest.LastIndexOf([system.IO.Path]::DirectorySeparatorChar) -lt 0)
	{
		$dest = "$dest$([system.IO.Path]::DirectorySeparatorChar)"
	}

	$fieldDescription = [System.Management.Automation.Host.FieldDescription]'Folder path:'
	$fieldDescription.SetParameterType([string])
	$fieldDescription.DefaultValue = $dest
	$fieldDescriptions = [System.Management.Automation.Host.FieldDescription[]]@($fieldDescription)
	
	try
	{
		$result = $host.UI.Prompt('Parameters', 'Please supply a valid destination folder.', $fieldDescriptions)
	}
	catch 
	{
		return
	}

	$dest = $result[$fieldDescription.Name]
}

#$prodName = $Host.PrivateData.ProductTitle
$desttop = $dest
$dest = [System.IO.Path]::Combine($dest, $prodName)

if ([System.IO.Directory]::Exists($dest))
{
	"Destination folder already contains $prodName folder."
	return
}

$destBin = [System.IO.Path]::Combine($dest, 'Bin')
$cfgDest = [System.IO.Path]::Combine($dest, 'Configuration')
$snapshotsDest = [System.IO.Path]::Combine($dest, 'Snapshots')
$destCache = [System.IO.Path]::Combine($dest, 'Cache')

function CopyAppData($specFolder, $folderToCopy, $destDir, $exclude, $canSkip)
{
	$prodWithCompany = [System.IO.Path]::Combine('Quest Software', $folderToCopy)
	$src = [System.IO.Path]::Combine([System.Environment]::GetFolderPath($specFolder), $prodWithCompany)
	if (-not $canSkip -or [System.IO.Directory]::Exists($src))
	{
		mkdir $destDir > $null
		copy $([System.IO.Path]::Combine($src, '*')) $destDir -Recurse -Exclude $exclude
	}
}

CopyAppData $([System.Environment+SpecialFolder]::LocalApplicationData) $prodName $destCache
$appData = [System.Environment+SpecialFolder]::ApplicationData
CopyAppData $appData $prodName $cfgDest 'AutoSaves'
CopyAppData $appData "$prodName Snapshots" $snapshotsDest $null 'can skip'
copy $PGHome $destBin -Recurse -Exclude @($MyInvocation.MyCommand.Name, '*.portable')
$files = copy "$PGHome\*.portable" -Destination $desttop -PassThru #| ForEach-Object { $_ | ren -NewName "$($_.Name -replace '\.portable$', '')"}

foreach($var in $files)
{
	$var | ren -NewName "$($var.Name -replace '\.portable$', '')"
}

[System.IO.File]::Create([System.IO.Path]::Combine($destBin, 'PortableMode.xml')) > $null

# SIG # Begin signature block
# MIIPIgYJKoZIhvcNAQcCoIIPEzCCDw8CAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUet9BFUypzf5brHs8gElGaR+V
# 9dWgggz6MIIEDTCCAvWgAwIBAgILBAAAAAABI54PrLMwDQYJKoZIhvcNAQEFBQAw
# VzELMAkGA1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNV
# BAsTB1Jvb3QgQ0ExGzAZBgNVBAMTEkdsb2JhbFNpZ24gUm9vdCBDQTAeFw05OTAx
# MjgxMzAwMDBaFw0xNzAxMjcxMjAwMDBaMIGBMQswCQYDVQQGEwJCRTEZMBcGA1UE
# ChMQR2xvYmFsU2lnbiBudi1zYTElMCMGA1UECxMcUHJpbWFyeSBPYmplY3QgUHVi
# bGlzaGluZyBDQTEwMC4GA1UEAxMnR2xvYmFsU2lnbiBQcmltYXJ5IE9iamVjdCBQ
# dWJsaXNoaW5nIENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAopt1
# KqcTuglxJBjfoQZiKRKe3J51c+j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