From 7fc0a0235c78679dd90527178b6ff61926d3f9f0 Mon Sep 17 00:00:00 2001
From: Manuel Thalmann <m@nuth.ch>
Date: Fri, 23 Aug 2024 01:52:04 +0200
Subject: [PATCH] Make scripts platform independent

---
 scripts/Common/Scripts/Config.ps1   | 42 +++++++++++++++++------------
 scripts/Common/Scripts/Software.ps1 |  2 +-
 2 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/scripts/Common/Scripts/Config.ps1 b/scripts/Common/Scripts/Config.ps1
index e7c56140..5bd1eb66 100644
--- a/scripts/Common/Scripts/Config.ps1
+++ b/scripts/Common/Scripts/Config.ps1
@@ -109,25 +109,33 @@ $null = New-Module {
 
         $scriptPath = "$PSScriptRoot/../../Common/Scripts/config.fish";
 
-        if (-not $IsWindows) {
-            $escapedPath = (fish -c 'string escape $argv' "$scriptPath");
-            fish -c ". $escapedPath; $Script";
-        } else {
-            function fish {
-                wsl --shell-type login -- nix --extra-experimental-features "nix-command flakes" run nixpkgs`#fish -- $args
-            }
-
-            $output = fish -c ". $(ConvertTo-LinuxPath $scriptPath); $Script";
-
-            if (-not $?) {
-                Write-Error "The configuration could not be retrieved!";
-            } else {
-                if (-not ($output -and ($output | Test-Json))) {
-                    Write-Error "The value ``$output`` is not valid JSON.";
+        if ($env:CONFIG_MODULE) {
+            $output = & {
+                if (-not $IsWindows) {
+                    $escapedPath = (fish -c 'string escape $argv' "$scriptPath");
+                    fish -c ". $escapedPath; $Script";
                 } else {
-                    $output | ConvertFrom-Json;
+                    function fish {
+                        wsl --shell-type login -- nix --extra-experimental-features "nix-command flakes" run nixpkgs`#fish -- $args
+                    }
+
+                    $output = fish -c ". $(ConvertTo-LinuxPath $scriptPath); $Script";
+
+                    if (-not $?) {
+                        Write-Error "The configuration could not be retrieved!";
+                    } else {
+                        $output;
+                    }
                 }
             }
+
+            if (-not ($output -and ($output | Test-Json))) {
+                Write-Error "The value ``$output`` is not valid JSON.";
+            } else {
+                $output | ConvertFrom-Json;
+            }
+        } else {
+            $null;
         }
     }
 
@@ -408,7 +416,7 @@ $null = New-Module {
         Checks whether the current user is the setup user.
     #>
     function Test-SetupUser {
-        $env:UserName -eq (Get-Config "valhalla.windows.setupUser");
+        ($IsWindows ? $env:UserName : $env:USER) -eq (Get-Config "valhalla.windows.setupUser");
     }
 
     <#
diff --git a/scripts/Common/Scripts/Software.ps1 b/scripts/Common/Scripts/Software.ps1
index f92cdeb0..420b0bd9 100644
--- a/scripts/Common/Scripts/Software.ps1
+++ b/scripts/Common/Scripts/Software.ps1
@@ -255,7 +255,7 @@ $null = New-Module {
                     }
                 } elseif ($Action -eq ([InstallerAction]::ConfigureUser)) {
                     if ((-not $Arguments.ContainsKey($userArgument)) -or (-not $Arguments[$userArgument])) {
-                        $Arguments.Add($userArgument, ($env:UserName));
+                        $Arguments.Add($userArgument, ($IsWindows ? $env:UserName : $env:USER));
                     }
 
                     if ($UserConfigurator) {