Install-TerminalTracker.ps1

#Requires -Version 5.1
<#
.SYNOPSIS
    Install TerminalTracker - set up profile hook, auto-start, and verify the environment.
.DESCRIPTION
    Interactive installer that walks through:
    1. Module import verification
    2. Profile hook installation (tracks CWD automatically)
    3. Auto-start with Windows (optional)
    4. Auto-reload suspended sessions on startup (optional)
    5. Sync path configuration (optional)
.PARAMETER Uninstall
    Remove all TerminalTracker hooks, auto-start entries, and optionally data.
.PARAMETER Quiet
    Skip interactive prompts and use defaults.
#>

[CmdletBinding()]
param(
    [switch]$Uninstall,
    [switch]$Quiet
)

$ErrorActionPreference = 'Stop'
$modulePath = Join-Path $PSScriptRoot 'TerminalTracker.psm1'

if (-not (Test-Path $modulePath)) {
    Write-Error "TerminalTracker.psm1 not found in $PSScriptRoot. Run this script from the terminal-tracker directory."
    return
}

Import-Module $modulePath -Force -DisableNameChecking

# ---------------------------------------------------------------------------
# Uninstall
# ---------------------------------------------------------------------------
if ($Uninstall) {
    Write-Host "`n Uninstalling TerminalTracker..." -ForegroundColor Yellow
    Write-Host ""

    Remove-TTProfileHook
    Remove-TTAutoStart
    Stop-TTMonitor

    if (-not $Quiet) {
        $removeData = Read-Host " Remove all session data? (y/N)"
        if ($removeData -eq 'y') {
            $dataRoot = Join-Path $env:APPDATA 'TerminalTracker'
            if (Test-Path $dataRoot) {
                Remove-Item $dataRoot -Recurse -Force
                Write-Host " Data directory removed: $dataRoot" -ForegroundColor Yellow
            }
        }
    }

    Write-Host "`n TerminalTracker uninstalled." -ForegroundColor Green
    Write-Host " Restart your PowerShell sessions to complete removal.`n" -ForegroundColor Cyan
    return
}

# ---------------------------------------------------------------------------
# Install
# ---------------------------------------------------------------------------
Write-Host ""
Write-Host " ===============================" -ForegroundColor Cyan
Write-Host " TerminalTracker Setup" -ForegroundColor Cyan
Write-Host " ===============================" -ForegroundColor Cyan
Write-Host ""

# Step 1: Verify module loads
Write-Host " [1/5] Module loaded successfully." -ForegroundColor Green
$cfg = Get-TTConfig
Write-Host " Data directory: $(Join-Path $env:APPDATA 'TerminalTracker')" -ForegroundColor Gray
Write-Host ""

# Step 2: Profile hook
$installHook = $true
if (-not $Quiet) {
    Write-Host " [2/5] Profile Hook" -ForegroundColor White
    Write-Host " This adds a prompt hook to your PowerShell profile that" -ForegroundColor Gray
    Write-Host " automatically tracks your working directory in every session." -ForegroundColor Gray
    $answer = Read-Host " Install profile hook? (Y/n)"
    if ($answer -eq 'n') { $installHook = $false }
}

if ($installHook) {
    Install-TTProfileHook -Force
    Write-Host ""
}

# Step 3: Auto-start
$enableAutoStart = $false
if (-not $Quiet) {
    Write-Host " [3/5] Auto-Start with Windows" -ForegroundColor White
    Write-Host " Starts the session monitor when you log in." -ForegroundColor Gray
    $answer = Read-Host " Enable auto-start? (y/N)"
    if ($answer -eq 'y') { $enableAutoStart = $true }
}

if ($enableAutoStart) {
    Install-TTAutoStart
    Write-Host ""
}

# Step 4: Auto-reload
$enableAutoReload = $false
if (-not $Quiet) {
    Write-Host " [4/5] Auto-Reload Suspended Sessions" -ForegroundColor White
    Write-Host " Automatically reopens suspended sessions when Windows starts." -ForegroundColor Gray
    $answer = Read-Host " Enable auto-reload? (y/N)"
    if ($answer -eq 'y') { $enableAutoReload = $true }
}

if ($enableAutoReload) {
    Set-TTConfig -AutoReload $true | Out-Null
    Write-Host " Auto-reload enabled." -ForegroundColor Green
    Write-Host ""
}

# Step 5: Sync path
if (-not $Quiet) {
    Write-Host " [5/5] Sync Path (optional)" -ForegroundColor White
    Write-Host " Set a folder (e.g. OneDrive, Dropbox) to sync sessions across machines." -ForegroundColor Gray
    $syncPath = Read-Host " Sync path (Enter to skip)"
    if ($syncPath -and (Test-Path $syncPath)) {
        Set-TTConfig -SyncPath $syncPath | Out-Null
        Write-Host " Sync path set: $syncPath" -ForegroundColor Green
    }
    elseif ($syncPath) {
        Write-Warning " Path does not exist: $syncPath (skipped)"
    }
    Write-Host ""
}

# Summary
Write-Host " ===============================" -ForegroundColor Cyan
Write-Host " Setup Complete!" -ForegroundColor Green
Write-Host " ===============================" -ForegroundColor Cyan
Write-Host ""
Write-Host " Quick start:" -ForegroundColor White
Write-Host " Show-TTDashboard # View all sessions" -ForegroundColor Gray
Write-Host " Get-TTSession # List active sessions" -ForegroundColor Gray
Write-Host " Set-TTNote -Id abc -Note 'working on X' # Add notes" -ForegroundColor Gray
Write-Host " Suspend-TTSession # Save & close current window" -ForegroundColor Gray
Write-Host " Resume-TTSession -All # Restore all suspended" -ForegroundColor Gray
Write-Host " Hide-TTWindow -Id abc # Hide a window" -ForegroundColor Gray
Write-Host " Show-TTWindow -Id abc # Show it again" -ForegroundColor Gray
Write-Host " tt # Quick CLI (if tt.ps1 is in PATH)" -ForegroundColor Gray
Write-Host ""
Write-Host " Restart PowerShell sessions for the profile hook to activate." -ForegroundColor Cyan
Write-Host ""