param(
    [Parameter(Mandatory=$true)][string]$URL,
    [Parameter(Mandatory=$false)][string]$AGENT_FILENAME="SyncroInstaller.msi",
    [Parameter(Mandatory=$false)][switch]$FORCE=$false
)

# Delete this script
Remove-Item $MyInvocation.MyCommand.Path -Force

# Exit early if we don't need to install the agent
If ( ($FORCE -eq $false) -and ((Test-Path "${Env:ProgramFiles}\RepairTech\Syncro\Syncro.App.Runner.exe") -or (Test-Path "${Env:ProgramFiles(x86)}\RepairTech\Syncro\Syncro.App.Runner.exe")) ) { Exit }

Function SourceTools
{ . $([Scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/Tools.ps1'))) }
. SourceTools

# Build the path for $AGENT_INSTALLER
$AGENT_INSTALLER = '{0}{1}' -f (Get-TempPath), $AGENT_FILENAME

# Download agent installer
Download-File -URL $URL -File "${AGENT_INSTALLER}"

 # Install the agent
 Write-Output "Installing Syncro agent from ""${AGENT_INSTALLER}"""
 Try { Start-Process "msiexec.exe" -ArgumentList "/i ""${AGENT_INSTALLER}"" /qn /norestart" -Wait }
 Catch { Write-Error $_.Exception.Message }

 # Delete the agent installer
 Write-Output "Deleting ""${AGENT_INSTALLER}"""
 Try { Remove-Item $AGENT_INSTALLER -Force }
 Catch { Write-Error $_.Exception.Message }