param(
    $Action,
    [hashtable] $Arguments
)

. "$PSScriptRoot/Constants.ps1";
. "$PSScriptRoot/../PowerShell/Profile.ps1";
. "$PSScriptRoot/../../Scripts/Software.ps1";

Start-SoftwareInstaller @PSBoundParameters `
    -Configurator {
        . "$PSScriptRoot/Constants.ps1";
        $pathExpression = Get-GlobalConfigExpression;
        $path = Get-GlobalConfigPath;
        $null = New-Item -Force -ItemType Directory (Split-Path -Parent $path);
        Copy-Item -Force "$PSScriptRoot/aliae.yml" $path;

        Add-PowerShellProfileStatement `
            -System `
            -Category "aliae" `
            -Script (
                @(
                    {
                        #aliae
                    },
                    "`$globalPath = $pathExpression",
                    {
                        $userPath = & {
                            if ($env:ALIAE_CONFIG) {
                                $env:ALIAE_CONFIG;
                            } else {
                                "~/.aliae.yaml";
                            }
                        };

                        if (Test-Path $globalPath) {
                            $null = & ([scriptblock]::Create((aliae init pwsh --config $globalPath))) *>&1;
                        }

                        if (Test-Path $userPath) {
                            $null = & ([scriptblock]::Create((aliae init pwsh))) *>&1;
                        }

                        aliae completion powershell | Out-String | Invoke-Expression;
                    }
                ) -join [System.Environment]::NewLine)
    } `
    -UserConfigurator {
        Copy-Item -Force "$PSScriptRoot/aliae.yml" ~/.aliae.yaml;
    };