Functions/Install-EverixControl.ps1

function Install-EverixControl {
    [CmdletBinding()]
    param(
        [ValidatePattern("^(latest|v\d+\.\d+\.\d+)$")]
        [string] $version = "latest",
        [string] $owner = "everix-io",    
        [string] $repo = "everix-install",
        [string] $outputDir = "$HOME/everix-control",
        [string] $githubToken
    )

    $apiUrlBase = "https://api.github.com/repos/$owner/$repo"

    $getReleasesUrl = "$apiUrlBase/releases"

    try {
    
        $outputDir = [IO.Path]::GetFullPath($outputDir)
        if (-not (Test-Path $outputDir)) {
            Write-Host "⌛ Creating output directory '$outputDir'..." -ForegroundColor Green
            New-Item -ItemType Directory -Path $outputDir | Out-Null
        }

        $headers = @{}

        if ($null -ne $githubToken) {
            Write-Host "🔑 Will authenticate with GitHub using the provided token..." -ForegroundColor Green
            $headers.Add("Authorization", "token $githubToken")
        }

        Write-Host "⌛ Getting everix-control version '$version' from GitHub..." -ForegroundColor Green

        # Determine the URL based on the version requested
        if ($version -eq "latest") {
            $getReleaseUrl = "$apiUrlBase/releases/latest"
        } else {
            $getReleaseUrl = "$apiUrlBase/releases/tags/$version"
        }

        Write-Verbose "Requesting '$getReleaseUrl'..."
        $response = Invoke-RestMethod -Uri $getReleasesUrl -Method Get
        Write-Verbose "Response: $($response | ConvertTo-Json -Depth 5)"
    
        if ($null -eq $response) {
            throw "Failed to get everix-control version '$version' from $apiUrlBase."        
        }

        $tag = $response.tag_name
        Write-Host "✅ Found everix-control release with tag '$tag':" -ForegroundColor Green
        Write-Host " Release name: $($response.name)" -ForegroundColor DarkBlue
        Write-Host " Published at: $($response.published_at)" -ForegroundColor DarkBlue
        Write-Host " GitHub Link: $($response.html_url)" -ForegroundColor DarkBlue

        $assetsUrl = $response.assets_url

        Write-Verbose "Requesting '$assetsUrl'..."
        $assetsResponse = Invoke-RestMethod -Uri $assetsUrl -Method Get
        Write-Verbose "Response: $($assetsResponse | ConvertTo-Json -Depth 5)"

        if ($null -eq $assetsResponse) {
            throw "Failed to get everix-control assets from $assetsUrl."
        }       

        $everixControlZipAsset = $assetsResponse | Where-Object {
            $_.name -eq "everix-control.zip"
        }

        if ($null -eq $everixControlZipAsset) {
            throw "Failed to find 'everix-control.zip' asset in the release."
        }

        $everixControlZipAssetDownloadUrl = $everixControlZipAsset.browser_download_url

        Write-Host "❓ Do you want to download this version? [Y/N] " -ForegroundColor Yellow -NoNewline
        $answer = Read-Host

        if ($answer -ne "Y" -and $answer -ne "y") {
            Write-Host "🚫 Aborted by the user" -ForegroundColor Red
            exit 0
        }
    
        Write-Host "⬇️ Downloading everix-control version '$tag'..." -ForegroundColor Green
    
        $outputDirWithVersion = "$outputDir\$tag"
        # create output directory if doesn't exist
        if (Test-Path $outputDirWithVersion) {
            Write-Host "⌛ Removing existing directory '$outputDirWithVersion'..." -ForegroundColor Yellow
            Remove-Item -Recurse -Force $outputDirWithVersion | Out-Null
        } else {
            New-Item -ItemType Directory -Path $outputDirWithVersion | Out-Null
        }

        $downloadPath = "$outputDirWithVersion\everix-control.zip"
        Write-Verbose "Downloading '$everixControlZipAssetDownloadUrl' to '$downloadPath'..."
        Invoke-WebRequest -Uri $everixControlZipAssetDownloadUrl -OutFile $downloadPath
    
        Write-Host "📦 Uncompressing everix-control.zip..." -ForegroundColor Green
        Expand-Archive -Path $downloadPath -DestinationPath $outputDirWithVersion -Force

        # check `everix-control.ps1` exists
        $everixControlScriptPath = "$outputDirWithVersion\everix-control.ps1"
        if (-not (Test-Path $everixControlScriptPath)) {
            throw "Failed to find 'everix-control.ps1' in the unzipped release."
        }

        # remove zip file
        Remove-Item -Path $downloadPath

        Write-Host "✅ everix-control version '$tag' unzipped to '$outputDirWithVersion'." -ForegroundColor Green

    } catch {
        Write-Error "Failed to get everix-control version '$version' from $apiUrlBase."
        Write-Error $_
    
        exit 1
    }
}