$butlerPath = "F:\Apps\butler\butler.exe"
$zipOutputDir = ".\release"
$fileName = "Cirno_No_Reason"
$gitVersionPath = "dotnet-gitversion.exe"
$buildDir = ".\build"

$gitVersionOutput = & $gitVersionPath | ConvertFrom-Json
$assemblySemFileVer = $gitVersionOutput.AssemblySemFileVer
$assemblySemVer = $gitVersionOutput.AssemblySemVer
$fullSemVer = $gitVersionOutput.FullSemVer
$PreReleaseLabel = $gitVersionOutput.PreReleaseLabelWithDash

#$zipFileName = "$fileName.$fullSemVer.win.zip"
#$zipFileNameLinux = "$fileName.$fullSemVer.linux.zip"
#$zipFilePath = "$zipOutputDir\$zipFileName"
#$zipFilePathLinux = "$zipOutputDir\$zipFileNameLinux"

# Ensure output directory exists
if (!(Test-Path $zipOutputDir)) {
    New-Item -ItemType Directory -Path $zipOutputDir | Out-Null
}

function Handle-BuildArtifact {
    param (
        [string]$platform,
        [string]$label
    )

    $sourcePath = Join-Path $buildDir $platform
    $zipFileName = "$fileName.$fullSemVer.$platform.zip"
    $zipFilePath = Join-Path $zipOutputDir $zipFileName
    $butlerTarget = "maddoscientisto/cirno-no-reason:$platform$label"

    $files = Get-ChildItem -Path $sourcePath -Recurse -File -ErrorAction SilentlyContinue
    if ($files) {
        Compress-Archive -Path "$sourcePath\*" -DestinationPath $zipFilePath -Force
        Write-Host "$platform build completed and packaged as $zipFilePath"
        & $butlerPath push $zipFilePath $butlerTarget --userversion $fullSemVer
    } else {
        Write-Warning "$platform build folder is empty or missing: $sourcePath"
    }
}

Handle-BuildArtifact -platform "win" -label $PreReleaseLabel
Handle-BuildArtifact -platform "linux" -label $PreReleaseLabel