#
# This script is used to install the DTT UI to a remote WCOS system using TShell
#
param(
[string]$IP,
[string]$ACTIVITY
)

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

if(!$IP -or !$ACTIVITY)
{
	Write-Host "To use this installer script without entering each values via prompt:"
	Write-Host "setup-ts.ps1 [IP address] [install | uninstall]"
	Write-Host ""
	
	if(!$IP) 
	{
		$IP = Read-Host -Prompt 'Input the SUT IP address'	
	}
	if(!$ACTIVITY) 
	{
		$ACTIVITY = Read-Host -Prompt 'Please input "install" or "uninstall"'
	}
}

echo "Connecting to device $IP..."

#Connect to device if already connection is present it close previous connection and establish new connection
Open-Device $IP

if($ACTIVITY -like "install")
{
	echo "Add firewall exception for target"
	cmdd netsh advfirewall firewall add rule name="xdIn" protocol=TCP localport=8888 action=allow dir=IN
	cmdd netsh advfirewall firewall add rule name="dOut" protocol=TCP localport=8888 action=allow dir=OUT  

	echo "Deleting Legacy startup.dv Files"
	cmdd "del M:\Windows\ServiceProfiles\LocalService\AppData\Local\Intel\DPTF\startup.dv"
	cmdd "del O:\DriverData\Intel\DPTF\dv\startup.dv"

	echo "Copying esif_ws.dll"
	Putd ".\bin\esif_ws.dll" 		"M:\Windows\System32\Intel\DPTF\" -recurse
	
	echo "Copying upe_phidget.dll"
	Putd ".\bin\upe_phidget.dll" 	"M:\Windows\System32\Intel\DPTF\" -recurse

	echo "Copying remoteui.dvx"
	Putd ".\dv\remoteui.dvx" 		"O:\DriverData\Intel\DPTF\dv\" -recurse

	echo "Copying index.html"
	Putd ".\ui\index.html"			"M:\Windows\System32\drivers\DriverData\Intel\DPTF\ui\" -recurse

	echo "Copying combined.xsl"
	Putd ".\ui\combined.xsl" 		"M:\Windows\System32\drivers\DriverData\Intel\DPTF\ui\" -recurse
	
	echo "Copying CaptureConfig.txt"
	Putd ".\ui\CaptureConfig.txt" 		"M:\Windows\System32\drivers\DriverData\Intel\DPTF\ui\" -recurse
	
	Write-Host "DPTF UI Installation completed."
	Write-Host -nonewline "Please restart your system." -foregroundcolor red 
}
ElseIf($ACTIVITY -like "uninstall")
{
	echo "Remove firewall exception for target"
	cmdd netsh advfirewall firewall delete rule name='DPTF'
	
	echo "Deleting esif_ws.dll"
	cmdd "del M:\Windows\System32\Intel\DPTF\esif_ws.dll"
	
	echo "Deleting upe_phidget.dll"
	cmdd "del M:\Windows\System32\Intel\DPTF\upe_phidget.dll"

	echo "Deleting remoteui.dvx"
	cmdd "del O:\DriverData\Intel\DPTF\dv\remoteui.dvx"

	echo "Deleting index.html"
	cmdd "del M:\Windows\System32\drivers\DriverData\Intel\DPTF\ui\index.html"

	echo "Deleting combined.xsl"
	cmdd "del M:\Windows\System32\drivers\DriverData\Intel\DPTF\ui\combined.xsl"
	
	echo "Deleting CaptureConfig.txt"
	cmdd "del M:\Windows\System32\drivers\DriverData\Intel\DPTF\ui\CaptureConfig.txt"
	
	echo "Deleting ui folder"
	cmdd "rd /s /q M:\Windows\System32\drivers\DriverData\Intel\DPTF\ui\"
	
	Write-Host "DPTF UI Uninstallation completed."
}
Else
{
	Write-Host "Invalid command"
}