From f5a1b0cb86ba4c3004b87d57d94c9e8a9a83177d Mon Sep 17 00:00:00 2001
From: Manuel Thalmann <m@nuth.ch>
Date: Sat, 28 Sep 2024 01:24:28 +0200
Subject: [PATCH] Fix `aliae` and `omp` configuration for Linux

---
 scripts/Common/Software/Oh My Posh/Manage.ps1 | 34 ++++++++++++++-----
 scripts/Common/Software/aliae/Manage.ps1      |  9 +++--
 2 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/scripts/Common/Software/Oh My Posh/Manage.ps1 b/scripts/Common/Software/Oh My Posh/Manage.ps1
index e568d600..13de366e 100644
--- a/scripts/Common/Software/Oh My Posh/Manage.ps1	
+++ b/scripts/Common/Software/Oh My Posh/Manage.ps1	
@@ -27,27 +27,36 @@ Start-SoftwareInstaller @PSBoundParameters `
             [hashtable] $Arguments
         )
 
-        $theme = Get-UserConfig "oh-my-posh.theme";
+        $user = $Arguments.Name;
+        $theme = Get-UserConfig -UserName "$user" "oh-my-posh.theme";
 
         function New-Theme {
             param($info)
-            $root = "$($IsWindows ? $env:AppData : "~/.config")/oh-my-posh";
-            $path = Join-Path $root "$($info.name).omp.json";
-            $null = New-Item -Force -ItemType Directory $root;
 
-            Set-Content $path (
+            $root = "$(& {
+                if ($IsWindows) {
+                    sudo -u "$user" pwsh -c 'Write-Host $env:AppData';
+                } else {
+                    sudo -u "$user" bash -c 'realpath ~/.config'
+                }
+            })/oh-my-posh";
+
+            $path = Join-Path $root "$($info.name).omp.json";
+            $null = sudo -u "$user" pwsh -c New-Item -Force -ItemType Directory $root;
+
+            sudo -u "$user" pwsh -CommandWithArgs 'Set-Content $args[0] -Value $args[1]' $path ([string](
                 & {
                     if ($IsWindows) {
                         wsl cat $info.source;
                     } else {
-                        cat $info.source;
+                        Get-Content -Raw $info.source;
                     }
-                });
+                }));
 
             return $path;
         }
 
-        foreach ($additionalTheme in (Get-UserConfig "oh-my-posh.additionalThemes")) {
+        foreach ($additionalTheme in (Get-UserConfig -UserName "$user" "oh-my-posh.additionalThemes")) {
             Write-Host "creating theme $additionalTheme"
             $null = New-Theme $additionalTheme;
         }
@@ -63,7 +72,14 @@ Start-SoftwareInstaller @PSBoundParameters `
                 [System.Environment]::SetEnvironmentVariable($varName, "%AppData%/$([System.IO.Path]::GetRelativePath($env:AppData, $theme))", "User");
             } else {
                 . "$PSScriptRoot/../aliae/Manage.ps1";
-                Add-EnvironmentVariable -User $Arguments.Name $varName ($theme).Replace("~", "{{ .Home }}");
+                $value = $theme;
+                $relativePath = sudo -u "$user" fish -c 'realpath --relative-base ~ $argv' $theme;
+
+                if ($relativePath -ne $theme) {
+                    $value = Join-Path "{{ .Home }}" $relativePath;
+                }
+
+                Add-EnvironmentVariable -User $Arguments.Name $varName $value;
             }
         }
     };
diff --git a/scripts/Common/Software/aliae/Manage.ps1 b/scripts/Common/Software/aliae/Manage.ps1
index 4163af39..9ce5d01e 100644
--- a/scripts/Common/Software/aliae/Manage.ps1
+++ b/scripts/Common/Software/aliae/Manage.ps1
@@ -73,8 +73,11 @@ $null = New-Module {
             [string] $User
         )
 
+        $flags = @();
+
         if ($User) {
-            $path = "$($IsWindows ? "~" : "$(sudo -u $User bash -c "realpath ~")")/.aliae.yaml";
+            $flags = @("-u", "$User")
+            $path = "$($IsWindows ? "~" : "$(sudo @flags bash -c "realpath ~")")/.aliae.yaml";
         } else {
             $path = Get-GlobalConfigPath;
         }
@@ -87,9 +90,9 @@ $null = New-Module {
                 Set-Item "Env:\$key" $variables[$key];
             }
 
-            sudo -u $using:User cp $using:path $file;
+            sudo @using:flags pwsh -CommandWithArgs 'Get-Content -Raw $args[0]' $using:path | Set-Content $file;
             yq -yi $using:Script $file;
-            sudo -u $using:User cp $file $using:path;
+            sudo @using:flags pwsh -CommandWithArgs 'Set-Content $args[0] -Value $args[1]' $using:path (Get-Content -Raw $file);
             Remove-Item $file;
         } | Receive-Job -Wait;
     }