[CmdletBinding()]
param(
[string]$Version = $(if ($env:AGBURN_VERSION) { $env:AGBURN_VERSION } else { "0.2.0" }),
[string]$InstallDir = $(if ($env:AGBURN_INSTALL_DIR) { $env:AGBURN_INSTALL_DIR } else { "" }),
[string]$BaseUrl = $(if ($env:AGBURN_BASE_URL) { $env:AGBURN_BASE_URL } else { "https://github.com/edudar/agburn/releases/download" })
)
$ErrorActionPreference = "Stop"
$GitHubLatestApi = "https://api.github.com/repos/edudar/agburn/releases/latest"
$Target = "windows-x64"
if ($env:OS -ne "Windows_NT") {
throw "This installer is for Windows ($Target). Use install.sh on macOS and Linux."
}
$arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString()
if ($arch -ne "X64" -and $env:PROCESSOR_ARCHITECTURE -ne "AMD64") {
throw "Unsupported CPU architecture: $arch (supported: x64)."
}
if ([string]::IsNullOrWhiteSpace($InstallDir)) {
if ([string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) {
throw "LOCALAPPDATA is not set; pass -InstallDir
or set AGBURN_INSTALL_DIR."
}
$InstallDir = Join-Path $env:LOCALAPPDATA "agburn\bin"
}
if ($Version -eq "latest") {
$release = Invoke-RestMethod -Uri $GitHubLatestApi -UseBasicParsing
if (-not $release.tag_name) {
throw "Failed to resolve latest agburn version from $GitHubLatestApi."
}
$Version = $release.tag_name.TrimStart("v")
} else {
$Version = $Version.TrimStart("v")
}
if ([string]::IsNullOrWhiteSpace($Version)) {
throw "Resolved version is empty."
}
$trimmedBaseUrl = $BaseUrl.TrimEnd("/")
if ($trimmedBaseUrl -like "*/releases/download") {
$releaseUrl = "$trimmedBaseUrl/v$Version"
} else {
$releaseUrl = $trimmedBaseUrl
}
$archiveName = "agburn-$Version-$Target.zip"
$workDir = Join-Path ([System.IO.Path]::GetTempPath()) ("agburn-install-" + [System.Guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Force -Path $workDir | Out-Null
try {
$archivePath = Join-Path $workDir $archiveName
$sumsPath = Join-Path $workDir "SHA256SUMS.txt"
Invoke-WebRequest -Uri "$releaseUrl/$archiveName" -OutFile $archivePath -UseBasicParsing
Invoke-WebRequest -Uri "$releaseUrl/SHA256SUMS.txt" -OutFile $sumsPath -UseBasicParsing
$expectedHash = $null
foreach ($line in Get-Content -Path $sumsPath) {
if ($line -match '^\s*([0-9a-fA-F]{64})\s+\*?(.+?)\s*$') {
if ($Matches[2] -eq $archiveName) {
$expectedHash = $Matches[1].ToLowerInvariant()
break
}
}
}
if (-not $expectedHash) {
throw "Checksum entry for $archiveName not found in SHA256SUMS.txt."
}
$actualHash = (Get-FileHash -Path $archivePath -Algorithm SHA256).Hash.ToLowerInvariant()
if ($actualHash -ne $expectedHash) {
throw "SHA-256 mismatch for $archiveName (expected $expectedHash, got $actualHash)."
}
$stageDir = Join-Path $workDir "stage"
New-Item -ItemType Directory -Force -Path $stageDir | Out-Null
Expand-Archive -Path $archivePath -DestinationPath $stageDir -Force
$stagedAgburn = Join-Path $stageDir "agburn.exe"
$stagedCcusage = Join-Path $stageDir "ccusage.exe"
if (-not (Test-Path -Path $stagedAgburn -PathType Leaf)) {
throw "Archive is missing agburn.exe."
}
if (-not (Test-Path -Path $stagedCcusage -PathType Leaf)) {
throw "Archive is missing ccusage.exe."
}
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
$destAgburn = Join-Path $InstallDir "agburn.exe"
$destCcusage = Join-Path $InstallDir "ccusage.exe"
Move-Item -Path $stagedAgburn -Destination $destAgburn -Force
Move-Item -Path $stagedCcusage -Destination $destCcusage -Force
$agburnVersionOut = ""
try {
$agburnVersionOut = (& $destAgburn --version 2>$null | Select-Object -First 1)
} catch {
$agburnVersionOut = ""
}
$agburnVersion = if ($agburnVersionOut) { ($agburnVersionOut.ToString().Trim() -replace '^agburn\s+', '') } else { $Version }
$ccusageVersionOut = ""
try {
$ccusageVersionOut = (& $destCcusage --version 2>$null | Select-Object -First 1)
} catch {
$ccusageVersionOut = ""
}
$ccusageVersion = if ($ccusageVersionOut) { ($ccusageVersionOut.ToString().Trim() -replace '^ccusage\s+', '') } else { "" }
Write-Output "Installed agburn $agburnVersion to $destAgburn"
if ($ccusageVersion) {
Write-Output "Installed ccusage $ccusageVersion to $destCcusage"
} else {
Write-Output "Installed ccusage to $destCcusage"
}
Write-Output "To remove:"
Write-Output " Remove-Item -Force `"$destAgburn`", `"$destCcusage`""
}
finally {
if (Test-Path -Path $workDir) {
Remove-Item -Path $workDir -Recurse -Force -ErrorAction SilentlyContinue
}
}