param(
    [string]$GamePath = "",
    [Parameter(ValueFromRemainingArguments = $true)]
    [string[]]$GameArguments
)

$ErrorActionPreference = "Stop"
if (-not $GamePath) { $GamePath = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) }
$Synchronizer = Join-Path $GamePath ".stardew-modsync\windows\Sync-StardewMods.ps1"
if (-not (Test-Path -LiteralPath $Synchronizer -PathType Leaf)) {
    throw "The Thor's Fjord synchronizer is missing. Re-run the installer."
}
$ErrorLog = Join-Path $GamePath ".stardew-modsync\last-error.log"
try {
    & $Synchronizer -GamePath $GamePath
    exit $LASTEXITCODE
} catch {
    @(
        "Thor's Fjord launch sync failed at $(Get-Date -Format o)",
        $_.Exception.ToString(),
        $_.ScriptStackTrace
    ) | Set-Content -Encoding UTF8 -LiteralPath $ErrorLog
    Write-Host "Thor's Fjord could not update or launch: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "Details were saved to: $ErrorLog" -ForegroundColor Yellow
    exit 1
}
