Install.ps1

<#
.SYNOPSIS
    Load ccs from every new PowerShell 7 session by adding one line to your profile.
.DESCRIPTION
    Nothing is copied and nothing is set permanently - the module keeps running
    from this folder, and CLAUDE_CONFIG_DIR is still set per shell by `ccs use`.

    The installer itself runs on Windows PowerShell 5.1 as well as PowerShell 7,
    but it always targets the PowerShell 7 profile: the two hosts read different
    profile files, and the ccs module requires 7.
.PARAMETER Uninstall
    Remove the ccs block from the PowerShell profile again.
.PARAMETER ProfilePath
    Write to this profile file instead of the PowerShell 7 one. Useful when your
    Documents folder is redirected somewhere the default lookup misses.
.EXAMPLE
    .\Install.ps1
.EXAMPLE
    .\Install.ps1 -Uninstall
#>

[CmdletBinding(SupportsShouldProcess)]
param(
    [switch]$Uninstall,
    [string]$ProfilePath
)

$ErrorActionPreference = 'Stop'

function Write-TextFile {
    <# UTF-8 without BOM on both 5.1 and 7 - Set-Content's utf8NoBOM is 7-only. #>
    param([string]$Path, [string]$Text)
    [IO.File]::WriteAllText($Path, $Text, (New-Object Text.UTF8Encoding($false)))
}

$moduleRoot = $PSScriptRoot
$manifest = Join-Path $moduleRoot 'ClaudeSwitcher.psd1'
if (-not (Test-Path -LiteralPath $manifest)) { throw "ClaudeSwitcher.psd1 not found next to Install.ps1" }

# PowerShell 7's CurrentUserAllHosts profile, resolved without relying on $PROFILE
# (which would point at the Windows PowerShell tree when run from 5.1).
if (-not $ProfilePath) {
    $documents = [Environment]::GetFolderPath('MyDocuments')
    $ProfilePath = Join-Path $documents 'PowerShell\profile.ps1'
}
$profilePath = $ProfilePath

if ($PSVersionTable.PSVersion.Major -lt 7) {
    Write-Warning "Running under PowerShell $($PSVersionTable.PSVersion). ccs itself needs PowerShell 7 - installing into the pwsh profile at $profilePath."
    if (-not (Get-Command pwsh -ErrorAction SilentlyContinue)) {
        Write-Warning "pwsh was not found on PATH. Install PowerShell 7 (winget install Microsoft.PowerShell) before using ccs."
    }
}

$beginMarker = '# >>> ccs (claude-switcher) >>>'
$endMarker   = '# <<< ccs (claude-switcher) <<<'
$block = @"
$beginMarker
Import-Module '$manifest'
$endMarker
"@


$existing = if (Test-Path -LiteralPath $profilePath) { Get-Content -LiteralPath $profilePath -Raw } else { '' }
if ($null -eq $existing) { $existing = '' }

# Strip any previous block first, so install is idempotent and uninstall is exact.
$pattern = [regex]::Escape($beginMarker) + '.*?' + [regex]::Escape($endMarker) + '\r?\n?'
$cleaned = [regex]::Replace($existing, $pattern, '', 'Singleline')

if ($Uninstall) {
    if ($cleaned -eq $existing) {
        Write-Host "ccs was not present in $profilePath - nothing to do."
        return
    }
    if ($PSCmdlet.ShouldProcess($profilePath, 'Remove ccs block')) {
        Write-TextFile -Path $profilePath -Text $cleaned.TrimEnd()
        Write-Host "Removed ccs from $profilePath" -ForegroundColor Green
    }
    return
}

if ($PSCmdlet.ShouldProcess($profilePath, 'Add ccs block')) {
    $dir = Split-Path -Parent $profilePath
    if (-not (Test-Path -LiteralPath $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null }
    $content = if ([string]::IsNullOrWhiteSpace($cleaned)) { $block } else { $cleaned.TrimEnd() + "`r`n`r`n" + $block }
    Write-TextFile -Path $profilePath -Text $content
    Write-Host "Added ccs to $profilePath" -ForegroundColor Green
    Write-Host "Open a new pwsh session, or run: pwsh -c ""Import-Module '$manifest'; ccs list""" -ForegroundColor DarkGray
}