# Install rivet CLI for Windows
# Usage: powershell -ExecutionPolicy Bypass -File install.ps1 [-System]
#   -System   Install to C:\Program Files\rivet (requires admin)
#   default   Install to %USERPROFILE%\.rivet and add to user PATH

param([switch]$System)

$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

if ($System) {
    $RivetHome  = "C:\Program Files\rivet"
    $InstallDir = "C:\Program Files\rivet"
} else {
    $RivetHome  = "$env:USERPROFILE\.rivet"
    $InstallDir = "$env:USERPROFILE\.rivet"
}

Write-Host "Installing rivet to $InstallDir..."

# Copy files
if (Test-Path $RivetHome) { Remove-Item $RivetHome -Recurse -Force }
New-Item -ItemType Directory -Path $RivetHome | Out-Null
Copy-Item (Join-Path $ScriptDir "rivet.exe") (Join-Path $RivetHome "rivet.exe")
Copy-Item (Join-Path $ScriptDir "_internal") (Join-Path $RivetHome "_internal") -Recurse

# Add to PATH (user scope, no admin needed for default install)
$CurrentPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($CurrentPath -notlike "*$RivetHome*") {
    [Environment]::SetEnvironmentVariable("PATH", "$CurrentPath;$RivetHome", "User")
    Write-Host "Added $RivetHome to your user PATH."
    Write-Host "Restart your terminal for PATH to take effect."
}

Write-Host ""
Write-Host "rivet installed successfully to $RivetHome"
Write-Host ""
& (Join-Path $RivetHome "rivet.exe") --help
