Install-HyperShell.ps1

#Requires -Version 5.1

<#
.SYNOPSIS
    Installs the HyperShell PowerShell module
.DESCRIPTION
    Copies the HyperShell module to the user's PowerShell modules directory
    Works on Windows, macOS, and Linux
#>


[CmdletBinding()]
param()

$ModuleName = "HyperShell"

# Determine source path - handles being run from repo root or module directory
if (Test-Path (Join-Path $PSScriptRoot $ModuleName)) {
    # Script is in repo root
    $SourcePath = Join-Path $PSScriptRoot $ModuleName
} elseif ((Split-Path $PSScriptRoot -Leaf) -eq $ModuleName) {
    # Script is inside the module directory
    $SourcePath = $PSScriptRoot
} else {
    throw "Cannot locate HyperShell module. Please run from repo root or HyperShell directory."
}

# Determine the correct module path based on OS and PowerShell version
if ($PSVersionTable.PSVersion.Major -ge 6) {
    # PowerShell Core (cross-platform)
    if ($IsWindows) {
        $ModulesPath = Join-Path ([Environment]::GetFolderPath('MyDocuments')) 'PowerShell' 'Modules'
    } else {
        # macOS and Linux use ~/.local/share/powershell/Modules
        $ModulesPath = Join-Path $HOME '.local' 'share' 'powershell' 'Modules'
    }
} else {
    # Windows PowerShell 5.1
    $ModulesPath = Join-Path ([Environment]::GetFolderPath('MyDocuments')) 'WindowsPowerShell' 'Modules'
}

$DestinationPath = Join-Path $ModulesPath $ModuleName

try {
    # Create modules directory if it doesn't exist
    if (-not (Test-Path $ModulesPath)) {
        New-Item -Path $ModulesPath -ItemType Directory -Force | Out-Null
        Write-Host "Created modules directory: $ModulesPath" -ForegroundColor Green
    }

    # Remove existing module if present
    if (Test-Path $DestinationPath) {
        Write-Host "Removing existing module..." -ForegroundColor Yellow
        Remove-Item $DestinationPath -Recurse -Force
    }

    # Copy module to destination
    Write-Host "Installing HyperShell module to: $DestinationPath" -ForegroundColor Cyan
    Copy-Item -Path $SourcePath -Destination $DestinationPath -Recurse -Force

    # Ensure the modules path is in PSModulePath for current session
    if ($env:PSModulePath -notlike "*$ModulesPath*") {
        Write-Host "Adding module path to current session..." -ForegroundColor Yellow
        $env:PSModulePath += [IO.Path]::PathSeparator + $ModulesPath
    }

    # Verify installation
    $installedModule = Get-Module -ListAvailable -Name $ModuleName
    if ($installedModule) {
        Write-Host "`n✓ HyperShell module installed successfully!" -ForegroundColor Green
        Write-Host " Version: $($installedModule.Version)" -ForegroundColor Gray
        Write-Host " Location: $($installedModule.ModuleBase)" -ForegroundColor Gray
        Write-Host "`nTo use the module, run:" -ForegroundColor Cyan
        Write-Host " Import-Module HyperShell" -ForegroundColor White
        Write-Host "`nTo verify installation:" -ForegroundColor Cyan
        Write-Host " Get-Command -Module HyperShell" -ForegroundColor White
    } else {
        Write-Error "Installation completed but module not found. Try restarting PowerShell."
        exit 1
    }
    
} catch {
    Write-Error "Failed to install module: $_"
    exit 1
}