TMD.Common.psm1

## 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

## Load all files in the lib directory
$LibraryFiles = Get-ChildItem -Path $PSScriptRoot -Directory -Exclude 'Classes' | Get-ChildItem -Recurse -Force -File -Exclude '.gitkeep'

# For Each $Library File, load it
foreach ($LibraryFile in $LibraryFiles) {
    . ($LibraryFile.FullName)
}

## Create a user path
$global:userFilesRoot = Join-Path -Path $HOME -ChildPath 'TMD_Files'

if ($IsMacOS) {

    $TmdUserConfigFile = Join-Path -Path $userFilesRoot -ChildPath 'config' -AdditionalChildPath 'config.json'

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

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

    } else {

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

## User Paths
$global:userPaths = @{
    root             = $global:userFilesRoot
    debug            = Join-Path -Path $global:userFilesRoot -ChildPath "Debug"
    logs             = Join-Path -Path $global:userFilesRoot -ChildPath "Logs"
    queue            = Join-Path -Path $global:userFilesRoot -ChildPath "Queue"
    config           = Join-Path -Path $global:userFilesRoot -ChildPath "Config"
    input            = Join-Path -Path $global:userFilesRoot -ChildPath "Input"
    output           = Join-Path -Path $global:userFilesRoot -ChildPath "Output"
    credentials      = Join-Path -Path $global:userFilesRoot -ChildPath "Credentials"
    git              = Join-Path -Path $global:userFilesRoot -ChildPath "Git"
    referencedesigns = Join-Path -Path $global:userFilesRoot -ChildPath "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
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'CRLF is a global variable used across the module.')]
$CRLF = "`r`n";