[CmdletBinding()]
param(
    [string]$HostName = "panopainter-mac",
    [string]$RemoteDirectory = "~/Dev/panopainter",
    [string]$RepositoryUrl = "ssh://git@git.omar.synology.me:3022/omar/panopainter.git",
    [string]$Branch = "codex/modernization-cmake-foundation",
    [string[]]$Presets = @("macos", "ios-simulator", "ios-device")
)

$ErrorActionPreference = "Stop"

function Join-RemoteArgument {
    param([string[]]$Values)

    $expanded = @()
    foreach ($value in $Values) {
        foreach ($part in ($value -split ",")) {
            $trimmed = $part.Trim()
            if ($trimmed.Length -gt 0) {
                $expanded += $trimmed
            }
        }
    }
    return ($expanded -join " ")
}

function ConvertTo-ShellSingleQuoted {
    param([string]$Value)

    return "'" + ($Value -replace "'", "'\\''") + "'"
}

$presetArgument = Join-RemoteArgument -Values $Presets
$remoteDirectoryLiteral = ConvertTo-ShellSingleQuoted -Value $RemoteDirectory
$repositoryLiteral = ConvertTo-ShellSingleQuoted -Value $RepositoryUrl
$branchLiteral = ConvertTo-ShellSingleQuoted -Value $Branch
$presetLiteral = ConvertTo-ShellSingleQuoted -Value $presetArgument

$remoteScript = @"
set -eu
export PATH="/opt/homebrew/bin:/usr/local/bin:`$HOME/tools/bin:`$PATH"
export DEVELOPER_DIR="/Applications/Xcode.app/Contents/Developer"

remote_dir=$remoteDirectoryLiteral
repository_url=$repositoryLiteral
branch_name=$branchLiteral
presets=$presetLiteral

case "`$remote_dir" in
    "~/"*) remote_dir="`$HOME/`$(printf '%s' "`$remote_dir" | sed 's|^~/||')" ;;
esac

mkdir -p "`$(dirname "`$remote_dir")"
if [ ! -d "`$remote_dir/.git" ]; then
    git clone "`$repository_url" "`$remote_dir"
fi

cd "`$remote_dir"
git fetch origin
git checkout "`$branch_name"
git pull --ff-only origin "`$branch_name"

git submodule update --init --recursive \
    libs/tinyxml2 \
    libs/glm \
    libs/stb/stb \
    libs/yoga \
    libs/poly2tri \
    libs/base64 \
    libs/sqlite3 \
    libs/nanort \
    libs/hash-library \
    libs/fmt \
    libs/glad \
    libs/tinyfiledialogs

mkdir -p out/logs
log="out/logs/apple-platform-build-`$(date +%Y%m%d-%H%M%S).log"
set +e
sh ./scripts/automation/platform-build.sh "`$presets" > "`$log" 2>&1
exit_code=`$?
set -e

printf '{"command":"apple-remote-build","host":"%s","branch":"%s","presets":"%s","log":"%s","exitCode":%s}\n' \
    "`$(hostname)" "`$branch_name" "`$presets" "`$log" "`$exit_code"
tail -n 80 "`$log"
exit "`$exit_code"
"@

$remoteScript = $remoteScript -replace "`r`n", "`n"
$encodedRemoteScript = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($remoteScript))
& ssh -o BatchMode=yes $HostName "printf '%s' '$encodedRemoteScript' | base64 -D | sh"
exit $LASTEXITCODE
