TMConsole.Client.psm1

## Add required Class Type Libraries
# Add-Type -AssemblyName Microsoft.PowerShell.Commands.Utility

## Setup an apropriate quiet environment
$global:InformationPreference = 'Continue'
$global:ProgressPreference = 'Continue'
$global:VerbosePreference = 'SilentlyContinue'
$global:DebugPreference = 'SilentlyContinue'
$global:WarningPreference = 'SilentlyContinue'
$global:ErrorActionPreference = 'Continue' # Using 'Stop' will break execution and not throw errors to the next handler.
$global:ConfirmPreference = 'None'  ## Allows functions that would typically require confirmation to be overridden

## Dot-source Private and Public ps1 files.
$Public = @( Get-ChildItem -Path "$PSScriptRoot\Public\*.ps1" )
$Private = @( Get-ChildItem -Path "$PSScriptRoot\Private\*.ps1" )

@($Public + $Private) | ForEach-Object {
    Try {
        . $_.FullName
    } Catch {
        Write-Error -Message "Failed to import function $($_.FullName): $_"
    }
}

## Store the User's preferred path in the registry
if ($IsWindows) {
    $regKey = Get-ChildItem -Path "HKCU:\Software\" | Where-Object { $_.Name -like '*\TransitionManager' }

    if (-not $regKey) {
        ## Create a user path
        $global:userFilesRoot = Join-Path -Path $HOME -ChildPath 'TMD_Files'
        $DebugFolder = Join-Path -Path $HOME -ChildPath 'TMD_Files' -AdditionalChildPath 'Debug'

        New-Item -Path "HKCU:\Software\TransitionManager"
        Set-ItemProperty -Path "HKCU:\Software\TransitionManager" -Name "UserFilesRoot" -Value $userFilesRoot
        Set-ItemProperty -Path "HKCU:\Software\TransitionManager" -Name "SessionManagerInstanceId" -Value '4bcc190b-2c25-4ed9-83db-d026c3d172fc'
        Set-ItemProperty -Path "HKCU:\Software\TransitionManager" -Name "DebugFolderPath" -Value $DebugFolder
    }
    else {
        $global:userFilesRoot = (Get-ItemProperty -Path "HKCU:\Software\TransitionManager" -Name "UserFilesRoot").UserFilesRoot
    }



    ## Confirm Existence of
}

## Store the User's preferred path in the registry
if ($IsMacOS) {

    ## Create a user path
    $global:userFilesRoot = Join-Path $HOME 'TMD_Files'
    $TmdUserConfigFolder = Join-Path $global:userFilesRoot 'config'
    $TmdUserConfigFile = Join-Path $TmdUserConfigFolder 'config.json'

    Test-FolderPath -FolderPath $TmdUserConfigFolder

    ## Read or Create the User config file
    if (Test-Path -Path $TmdUserConfigFile) {

        ## Read the Config File
        $UserConfig = Get-Content $TmdUserConfigFile | ConvertFrom-Json

    }
    else {

        ## Create a Default Config file
        $UserConfig = [PSCustomObject]@{
            UserFilesRoot   = $userFilesRoot
            # TmdPsRoot = $TmdPsRoot
            DebugFolderPath = (Join-Path -Path $userFilesRoot 'debug').FullName
        }
    }
}

## User Paths
$global:userPaths = @{
    root             = $global:userFilesRoot
    debug            = Join-Path $global:userFilesRoot "Debug"
    queue            = Join-Path $global:userFilesRoot "Queue"
    logs             = Join-Path $global:userFilesRoot "Logs"
    config           = Join-Path $global:userFilesRoot "Config"
    input            = Join-Path $global:userFilesRoot "Input"
    output           = Join-Path $global:userFilesRoot "Output"
    credentials      = Join-Path $global:userFilesRoot "Credentials"
    git              = Join-Path $global:userFilesRoot "Git"
    referencedesigns = Join-Path $global:userFilesRoot "Reference Designs"
}

## Confirm each user folder exists
$global:userPaths.Values | ForEach-Object {
    Test-FolderPath -FolderPath $_
}

## Write the Configuration to disk
if ($IsMacOS) {
    $UserConfig | ConvertTo-Json | Set-Content -Path $TmdUserConfigFile
}

## Declare a variable used within the functions in this module
##TM-23332 Note: As CRLF is not in use within this file, we had to supress the rule below just for it.
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
$CRLF = "`r`n";

## Check for/Create the powershellsettings file
if ($IsWindows) {
    $PowerShellSettingsFilePath = Join-Path -Path $env:APPDATA -ChildPath 'tmconsole' -AdditionalChildPath 'powershellsettings'
}
if ($isMacOS) {
    $PowerShellSettingsFilePath = Join-Path -Path '/' -ChildPath 'Users' -AdditionalChildPath $env:USER, 'Library', 'Application Support', 'tmconsole', 'powershellsettings'
}
if (-not (Test-Path $PowerShellSettingsFilePath -PathType Leaf)) {
    New-Item -Path $PowerShellSettingsFilePath -ItemType File -Force -Value '{}'
}

## Gather a list of installed provider modules and store them in the PowerShell settings
New-TMConsoleSetting -Name 'InstalledProviderModules' -Value (@((Get-Module -Name 'TMConsole.Provider.*' -ListAvailable).Name))