param(
    [string]$Mode = $null,
    [string]$Executable = $null,
    [string]$Configuration = $null,
    [string]$Platform = $null
)

if ([string]::IsNullOrEmpty($Mode)) {
    throw "\$Mode parameter is required."
}
if ([string]::IsNullOrEmpty($Executable)) {
    throw "\$Executable parameter is required."
}

if (($Mode -ne "CLI") -and ($Mode -ne "UnitTest")) {
    throw "Invalid mode: $Mode. Allowed values are CLI or UnitTest."
}

if (([string]::IsNullOrEmpty($Configuration)) -ne ([string]::IsNullOrEmpty($Platform))) {
    throw "Configuration and Platform parameters should be set or unset at the same time."
}
if (-not [string]::IsNullOrEmpty($Configuration)) {
    if (($Configuration -ne "Debug") -and ($Configuration -ne "Release")) {
        throw "Invalid configuration: $Configuration. Allowed values are Debug or Release."
    }
    if (($Platform -ne "x64") -and ($Platform -ne "Win32")) {
        throw "Invalid platform: $Platform. Allowed values are x64 or Win32."
    }
}

. $PSScriptRoot\copilotShared.ps1

if ($Mode -eq "UnitTest") {
  # Remove log files
  $logFile = "$PSScriptRoot\Execute.log"
  $logFileUnfinished = "$logFile.unfinished"
  $logFileMemoryLeaks = "$logFile.memoryleaks"
  Remove-Item -Path $logFile, $logFileUnfinished, $logFileMemoryLeaks -Force -ErrorAction SilentlyContinue
}

# Ensure the executable name does not have the .exe extension
if ($Executable.EndsWith(".exe")) {
    throw "\$Executable parameter should not include the .exe extension: $Executable"
}
$executableName = $Executable + ".exe"

# Find the solution folder by looking for *.sln files
$solutionFolder = GetSolutionDir

# Find the file with the latest modification time
if ([string]::IsNullOrEmpty($Configuration)) {
  $latestFile = GetLatestModifiedExecutable $solutionFolder $executableName
} else {
  $latestFile = GetSpecifiedExecutable $solutionFolder $executableName $Configuration $Platform
}
Write-Host "Selected $executableName`: $($latestFile.Path) (Modified: $($latestFile.LastWriteTime))"

# Try to read debug arguments from the corresponding .vcxproj.user file
$debugArgs = GetDebugArgs $solutionFolder $latestFile $Executable

# Execute the selected executable with debug arguments and save output to log file
if ($Mode -eq "UnitTest") {
  $commandLine = "`"$($latestFile.Path)`" /C /DebugOutput:`"$logFileMemoryLeaks`" $debugArgs"
  & { $commandLine; & cmd.exe /S /C $commandLine 2>&1 } | Tee-Object -FilePath $logFileUnfinished
  if ((Test-Path $logFileMemoryLeaks) -and ((Get-Item $logFileMemoryLeaks).Length -gt 0)) {
    Get-Content -Path $logFileMemoryLeaks | Add-Content -Path $logFileUnfinished
  }
  Rename-Item -Path $logFileUnfinished -NewName $logFile -Force
  Remove-Item -Path $logFileUnfinished, $logFileMemoryLeaks -Force -ErrorAction SilentlyContinue
} else {
  $commandLine = "`"$($latestFile.Path)`" $debugArgs"
  & { $commandLine; & cmd.exe /S /C $commandLine 2>&1 }
}
exit $LASTEXITCODE
