. "$PSScriptRoot/Constants.ps1";
. "$PSScriptRoot/../powershell/Profile.ps1";
. "$PSScriptRoot/../../../lib/Software.ps1";

Start-SoftwareInstaller @args `
    -Configurator {
        param([string] $Name)
        . "$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 $Name `
            -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 {
        param(
            [hashtable] $Arguments
        )

        sudo -u $Arguments.Name pwsh -c Copy-Item -Force "$PSScriptRoot/aliae.yml" "~/.aliae.yaml";
    };