param(
    [string]$ProjectDir = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path,
    [string]$RenpySdkDir = "F:\Users\dmssi\Downloads\renpy-8.5.3-sdk",
    [string]$OutputRoot = "C:\Users\dmssi\Documents\GitHub\FlexibleSurvival-1.0-dists",
    [switch]$SkipLint,
    [switch]$SkipDesktop,
    [switch]$SkipAndroid,
    [switch]$SkipWeb,
    [switch]$AndroidBundle,
    [switch]$LaunchWeb
)

$ErrorActionPreference = "Stop"

function Resolve-ExistingPath {
    param(
        [Parameter(Mandatory = $true)]
        [string]$PathValue,
        [Parameter(Mandatory = $true)]
        [string]$Label
    )

    try {
        return (Resolve-Path -LiteralPath $PathValue).Path
    }
    catch {
        throw "$Label not found: $PathValue"
    }
}

function Invoke-Renpy {
    param(
        [Parameter(Mandatory = $true)]
        [string[]]$Arguments,
        [Parameter(Mandatory = $true)]
        [string]$Label
    )

    Write-Host ""
    Write-Host "==> $Label"

    & $script:PythonExe $script:RenpyPy @Arguments
    if ($LASTEXITCODE -ne 0) {
        throw "$Label failed with exit code $LASTEXITCODE."
    }
}

$ProjectDir = Resolve-ExistingPath -PathValue $ProjectDir -Label "Project directory"
$RenpySdkDir = Resolve-ExistingPath -PathValue $RenpySdkDir -Label "Ren'Py SDK directory"
$LauncherDir = Resolve-ExistingPath -PathValue (Join-Path $RenpySdkDir "launcher") -Label "Ren'Py launcher directory"
$PythonExe = Resolve-ExistingPath -PathValue (Join-Path $RenpySdkDir "lib\py3-windows-x86_64\python.exe") -Label "Ren'Py Python runtime"
$RenpyPy = Resolve-ExistingPath -PathValue (Join-Path $RenpySdkDir "renpy.py") -Label "Ren'Py CLI"

if ($OutputRoot) {
    $OutputRoot = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutputRoot)
    New-Item -ItemType Directory -Force -Path $OutputRoot | Out-Null
}

if (-not $SkipLint) {
    Invoke-Renpy -Label "Lint" -Arguments @(
        $ProjectDir,
        "lint"
    )
}

if (-not $SkipDesktop) {
    $desktopArgs = @(
        $LauncherDir,
        "distribute",
        $ProjectDir
    )

    if ($OutputRoot) {
        $desktopArgs += @("--destination", $OutputRoot)
    }

    Invoke-Renpy -Label "Desktop distributions" -Arguments $desktopArgs
}

if (-not $SkipAndroid) {
    $androidArgs = @(
        $LauncherDir,
        "android_build",
        $ProjectDir
    )

    if ($AndroidBundle) {
        $androidArgs += "--bundle"
    }

    if ($OutputRoot) {
        $androidArgs += @("--destination", $OutputRoot)
    }

    Invoke-Renpy -Label "Android build" -Arguments $androidArgs
}

if (-not $SkipWeb) {
    $webArgs = @(
        $LauncherDir,
        "web_build",
        $ProjectDir
    )

    if ($LaunchWeb) {
        $webArgs += "--launch"
    }

    if ($OutputRoot) {
        $webDir = Join-Path $OutputRoot "Web"
        New-Item -ItemType Directory -Force -Path $webDir | Out-Null
        $webArgs += @("--destination", $webDir)
    }

    Invoke-Renpy -Label "Web build" -Arguments $webArgs
}

Write-Host ""
Write-Host "Release pipeline complete."
