#! powershell
param(
    [string]$BaseUrl = "https://scripts.rever.com.br",
    [string]$WorkDir = "$env:TEMP\\rever-menu",
    [string]$Profile = "default",
    [switch]$DryRun
)

Set-StrictMode -Version Latest
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls11

function Download-File {
    param([string]$Uri, [string]$Destination)
    Write-Host ("Baixando {0} -> {1}" -f $Uri, $Destination) -ForegroundColor Cyan
    Invoke-WebRequest -Uri $Uri -OutFile $Destination -UseBasicParsing
}

New-Item -ItemType Directory -Path $WorkDir -Force | Out-Null

$menuPath     = Join-Path $WorkDir "menu.ps1"
$catalogPath  = Join-Path $WorkDir "comandos.json"

Download-File -Uri ("{0}/menu.ps1" -f $BaseUrl) -Destination $menuPath
Download-File -Uri ("{0}/comandos.json" -f $BaseUrl) -Destination $catalogPath

try { Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force -ErrorAction Stop } catch { }

if (-not (Test-Path $menuPath)) {
    Write-Host "menu.ps1 nao encontrado apos download." -ForegroundColor Red
    exit 1
}

$menuArgs = @("-Profile", $Profile)
if ($DryRun) { $menuArgs += "-DryRun" }

& $menuPath @menuArgs
