Private/Profile.ps1

# Cool Profile Initialization Script
# This script checks if the current user's PowerShell profile is configured to import the Cool module.

function script:Initialize-CoolProfile {
    # Get the path to the current user's PowerShell profile
    $path = $PROFILE.CurrentUserAllHosts
    $name = if ($path -match "WindowsPowerShell") { "WindowsPowerShell" } else { "PowerShell" }
    # 1. Ensure the parent directory of the profile file exists, if not, create it
    $parentDir = Split-Path $path
    if (-not [System.IO.Directory]::Exists($parentDir)) {
        New-Item -Path $parentDir -Type Directory -Force | Out-Null
    }
    # 2. Check if the profile file exists, if not, create an empty one
    if (-not [System.IO.File]::Exists($path)) {
        New-Item -Path $path -Type File -Force | Out-Null
        Write-Host (Get-LocalizedString 'CoolProfileCreated' $name) -ForegroundColor Cyan
    }

    # 3. Check if the profile already contains the import statement for Cool, if not, append it
    $content = [string][System.IO.File]::ReadAllText($path, [System.Text.Encoding]::UTF8)
    if ($content -notlike "*Import-Module Cool*" -or $content -match "#.*Import-Module Cool") {
        [System.IO.File]::AppendAllText($path, "`r`nImport-Module Cool`r`n", [System.Text.Encoding]::UTF8)
        $msg = Get-LocalizedString 'CoolProfileUpdated' $name
        Write-Host $msg -ForegroundColor Green
    }
    else {
        $msg = Get-LocalizedString 'CoolProfileAlreadyConfigured' $name
        Write-Host $msg -ForegroundColor Gray
    }
}