$ROOT_DATA_PATH = "$execFolder\data\"

. $("$libFolder\sharepoint\CommonConstants.ps1")
. $("$libFolder\sharepoint\SolutionService.ps1")
. $("$libFolder\sharepoint\FeatureService.ps1")
. $("$libFolder\sharepoint\ExportImportService.ps1")

function Deploy($settings)
{
	Write-Host 
	
	Write-Host "$MSG_LINE"
	#Write-Host $settings
	#exit

	$webUrl = $settings.webUrl
	$mode = $settings.mode	

	$web = Get-SPWeb ($webUrl)-ErrorAction Stop
	$site = $web.Site
	$siteUrl = $site.Url

	if ($mode -eq $MODE_INSTALL_AND_ACTIVATE)
	{
		InstallSolutionList $siteUrl $webUrl $settings
		ActivateSolutionFeatures $siteUrl $webUrl $settings
		#ImportAllListDataWithNotification $webUrl $settings
	}
	else
	{
		if ($mode -eq $MODE_ACTIVATE)
		{
			ActivateSolutionFeatures $siteUrl $webUrl $settings
		}
		else
		{
			if ($mode -eq $MODE_INSTALL)
			{
				InstallSolutionList $siteUrl $webUrl $settings
			}
			else
			{
				if ($mode -eq $MODE_DATA_IMPORT)
				{
					ImportAllListDataWithNotification $webUrl $settings
				}
				else
				{
					if ($mode -eq $MODE_UNINSTALL)
					{
						UninstallSolutionList $siteUrl $webUrl $settings
					}
					else
					{
						Write-Host ("Error! Unknown mode selected: '{0}'. Operation terminated." -f $mode) -ForegroundColor Magenta
					}
				}
			}
		}
	}
}

function ActivateSolutionFeatures($siteUrl, $webUrl, $settings)
{
	if ($settings.siteFeatureNameCollection -ne $null -and $settings.siteFeatureNameCollection.Count -ne 0)
	{
		foreach ($feature in $settings.siteFeatureNameCollection)
		{
			ActivateSiteFeatureBySiteUrlAndFeatureId $siteUrl $feature
		}
	}
	
	if ($settings.webFeatureNameCollection -ne $null -and $settings.webFeatureNameCollection.Count -ne 0)
	{
		foreach ($feature in $settings.webFeatureNameCollection)
		{
			ActivateWebFeatureByWebUrlAndFeatureId $webUrl $feature
		}
	}
	
	if ($settings.webApplicationFeatureNameCollection -ne $null -and $settings.webApplicationFeatureNameCollection.Count -ne 0)
	{
		foreach ($feature in $settings.webApplicationFeatureNameCollection)
		{
			ActivateWebApplicationFeatureByWebUrlAndFeatureId $webUrl $feature
		}
	}
}

function InstallSolutionList($siteUrl, $webUrl, $settings)
{
	if ($settings.solutionNameCollection -ne $null -and $settings.solutionNameCollection.Count -ne 0)
	{
		foreach ($solutionName in $settings.solutionNameCollection)
		{
			InstallSolution $solutionName $siteUrl $webUrl
		}
	}

	if ($settings.sandboxedSolutionNameCollection -ne $null -and $settings.sandboxedSolutionNameCollection.Count -ne 0)
	{
		foreach ($solutionName in $settings.sandboxedSolutionNameCollection)
		{
			InstallSandboxedSolution $solutionName $siteUrl $webUrl
		}
	}
}

function UninstallSolutionList($siteUrl, $webUrl, $settings)
{
	if ($settings.solutionNameCollection -ne $null -and $settings.solutionNameCollection.Count -ne 0)
	{
		foreach ($solutionName in $settings.solutionNameCollection)
		{
			UninstallSolution $solutionName $siteUrl $webUrl
		}
	}	
	
	if ($settings.sandboxedSolutionNameCollection -ne $null -and $settings.sandboxedSolutionNameCollection.Count -ne 0)
	{
		foreach ($solutionName in $settings.sandboxedSolutionNameCollection)
		{
			UninstallSandboxedSolution $solutionName $siteUrl $webUrl
		}
	}
}

function ImportAllListDataWithNotification($webUrl, $settings)
{
	foreach ($ListData in $settings.listDataCollection)
	{
		$ListDataFullPath = $ROOT_DATA_PATH+$ListData
		ImportListDataWithNotification $webUrl $ListDataFullPath
	}
}

function UninstallSolution($solutionName, $siteUrl, $webUrl)
{
	Write-Host "Removing old solution..."
	UninstallAndRemoveSolutionByName $solutionName
	sleep -s 3
}

function InstallSolution($solutionName, $siteUrl, $webUrl)
{
	UninstallSolution $solutionName $siteUrl $webUrl

	Write-Host "Adding new solution..."
	AddAndInstallSolutionByName $solutionName $execFolder\bin $siteUrl
	sleep -s 3
}

function UninstallSandboxedSolution($solutionName, $siteUrl, $webUrl)
{
	Write-Host "Removing old sandboxed solution..."
	UninstallAndRemoveSandboxedSolutionByName $solutionName $webUrl
	sleep -s 3
}

function InstallSandboxedSolution($solutionName, $siteUrl, $webUrl)
{
	UninstallSandboxedSolution $solutionName $siteUrl $webUrl
	
	Write-Host "Adding new sandboxed solution..."
	AddAndInstallSPUserSolutionByName $solutionName  $execFolder\bin  $siteUrl
	sleep -s 3
}
