#
# This script is used to install the DTT UI on a remote IOT system
#
param(
[string]$ACTIVITY,
[string]$IP,
[string]$PASSWORD
)

Write-Host "Intel(R) Dynamic Tuning UI Installer"

if(!$IP -or !$PASSWORD -or !$ACTIVITY)
{
	Write-Host "To use this installer script without entering each values via prompt:"
	Write-Host "setup-iot.ps1 [Install | Uninstall] [IP address] [Password]"
	Write-Host ""
	
	$ACTIVITY = Read-Host -Prompt 'Please input "Install" or "uninstall"'
	$IP = Read-Host -Prompt 'Input the IOT SUT IP address'
	$PASSWORD = Read-Host -Prompt 'Input the IOT SUT Admin password'
}
else
{
	Set-ExecutionPolicy RemoteSigned
}

net start winrm
Set-Item WSMan:\localhost\Client\TrustedHosts -value $IP 

$InputPassword = convertto-securestring -AsPlainText -Force -String $PASSWORD
$Cred = new-object System.Management.Automation.PSCredential ("$IP\Administrator",$InputPassword)
$RemoteSessionID = New-PSSession -ComputerName $IP -Credential $Cred

$UIFolder = "\\$IP\C$\Windows\system32\drivers\DriverData\Intel\DPTF\ui"
$DVFolder = "\\$IP\C$\Windows\system32\drivers\DriverData\Intel\DPTF\dv"
$BinFolder = "\\$IP\C$\Windows\system32\Intel\DPTF"

if($ACTIVITY -like "install")
{
	#creating folder path if not present
	if(!(Test-Path -Path $DVFolder)) {New-Item $DVFolder -Type Directory}
	if(!(Test-Path -Path $UIFolder)) {New-Item $UIFolder -Type Directory}

	#copying dependencies
	Copy-Item ui\* -destination $UIFolder -Force
	Copy-Item dv\* -Destination $DVFolder -Force
	Copy-Item bin\esif_ws.dll -Destination $BinFolder -Force
	Copy-Item bin\upe_phidget.dll -Destination $BinFolder -Force
	
	Write-Host "DPTF UI Installation completed."
	Write-Host -nonewline "Please restart your system." -foregroundcolor red 
}
ElseIf($ACTIVITY -like "uninstall")
{
	#delete UI folder if present
	if(Test-Path -Path $UIFolder) {Remove-Item $UIFolder -Recurse -Force}

	#delete other file(s)
	Remove-Item $DVFolder\remoteui.dvx -Force
	Remove-Item $BinFolder\esif_ws.dll -Force
	Remove-Item $BinFolder\upe_phidget.dll -Force
	
	Write-Host "DPTF UI Uninstallation completed."
	Write-Host -nonewline "Please restart your system." -foregroundcolor red 
}
Else
{
	Write-Host "Invalid command"
}

