Install-WiFiAnalyzer.ps1

# WiFiAnalyzer Automated Installation Script
# This script will install the fixed WiFiAnalyzer module

param(
    [ValidateSet('AllUsers', 'CurrentUser')]
    [string]$Scope = 'CurrentUser'
)

Write-Host "=== WiFiAnalyzer Installation Script ===" -ForegroundColor Cyan
Write-Host "Installation Scope: $Scope" -ForegroundColor Yellow

# Check if running as admin for AllUsers
if ($Scope -eq 'AllUsers') {
    $isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
    if (-not $isAdmin) {
        Write-Host "ERROR: AllUsers scope requires Administrator privileges" -ForegroundColor Red
        Write-Host "Please run PowerShell as Administrator or use -Scope CurrentUser" -ForegroundColor Yellow
        pause
        exit 1
    }
}

# Determine paths
if ($Scope -eq 'AllUsers') {
    $basePath = "C:\Program Files\WindowsPowerShell\Modules"
} else {
    $basePath = "$HOME\Documents\WindowsPowerShell\Modules"
}

$modulePath = Join-Path $basePath "WiFiAnalyzer\1.0.7"

# Step 1: Remove old versions
Write-Host "`n[1] Removing old versions..." -ForegroundColor Yellow
$oldPaths = @(
    "C:\Program Files\WindowsPowerShell\Modules\WiFiAnalyzer",
    "$HOME\Documents\WindowsPowerShell\Modules\WiFiAnalyzer"
)

foreach ($path in $oldPaths) {
    if (Test-Path $path) {
        Remove-Item $path -Recurse -Force -ErrorAction SilentlyContinue
        Write-Host " Removed: $path" -ForegroundColor Gray
    }
}

# Clear from memory
Get-Module WiFiAnalyzer | Remove-Module -Force -ErrorAction SilentlyContinue
Write-Host " Cleared from memory" -ForegroundColor Green

# Step 2: Create module directory
Write-Host "`n[2] Creating module directory..." -ForegroundColor Yellow
if (-not (Test-Path $modulePath)) {
    New-Item -ItemType Directory -Path $modulePath -Force | Out-Null
    Write-Host " Created: $modulePath" -ForegroundColor Green
} else {
    Write-Host " Directory exists: $modulePath" -ForegroundColor Gray
}

# Step 3: Copy files
Write-Host "`n[3] Copying module files..." -ForegroundColor Yellow

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$psd1Source = Join-Path $scriptDir "WiFiAnalyzer.psd1"
$psm1Source = Join-Path $scriptDir "WiFiAnalyzer.psm1"

if (-not (Test-Path $psd1Source)) {
    Write-Host " ERROR: WiFiAnalyzer.psd1 not found in script directory" -ForegroundColor Red
    Write-Host " Looking for: $psd1Source" -ForegroundColor Gray
    pause
    exit 1
}

if (-not (Test-Path $psm1Source)) {
    Write-Host " ERROR: WiFiAnalyzer.psm1 not found in script directory" -ForegroundColor Red
    Write-Host " Looking for: $psm1Source" -ForegroundColor Gray
    pause
    exit 1
}

Copy-Item $psd1Source (Join-Path $modulePath "WiFiAnalyzer.psd1") -Force
Copy-Item $psm1Source (Join-Path $modulePath "WiFiAnalyzer.psm1") -Force
Write-Host " Files copied successfully" -ForegroundColor Green

# Step 4: Import module
Write-Host "`n[4] Importing module..." -ForegroundColor Yellow
try {
    Import-Module WiFiAnalyzer -Force -ErrorAction Stop
    Write-Host " Module imported successfully" -ForegroundColor Green
}
catch {
    Write-Host " ERROR: Failed to import module" -ForegroundColor Red
    Write-Host " $($_.Exception.Message)" -ForegroundColor Red
    pause
    exit 1
}

# Step 5: Verify
Write-Host "`n[5] Verifying installation..." -ForegroundColor Yellow
$commands = Get-Command -Module WiFiAnalyzer -ErrorAction SilentlyContinue

if ($commands) {
    Write-Host " SUCCESS! Module installed and working" -ForegroundColor Green
    Write-Host "`n Available commands:" -ForegroundColor Cyan
    $commands | ForEach-Object { 
        Write-Host " - $($_.Name)" -ForegroundColor White 
    }
    
    Write-Host "`n=== Installation Complete ===" -ForegroundColor Green
    Write-Host "`nTo launch WiFi Analyzer, run any of these commands:" -ForegroundColor Yellow
    Write-Host " Start-WiFiAnalyzer" -ForegroundColor White
    Write-Host " Show-WiFiAnalyzerGUI" -ForegroundColor White
    Write-Host " WiFi-Analyzer" -ForegroundColor White
} else {
    Write-Host " ERROR: No commands found in module" -ForegroundColor Red
    pause
    exit 1
}

Write-Host ""
pause