From 467ad0caabe72050cfe744c8e2b5af6edca1e709 Mon Sep 17 00:00:00 2001
From: Manuel Thalmann <m@nuth.ch>
Date: Wed, 31 Jul 2024 16:25:32 +0200
Subject: [PATCH] Refactor the installation loop architecture

---
 scripts/Windows/OS/Install.ps1          | 56 ++++++++++++++-----------
 scripts/Windows/Scripts/SetupConfig.ps1 |  5 +++
 2 files changed, 36 insertions(+), 25 deletions(-)

diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1
index 5ace9e81..6db0b682 100644
--- a/scripts/Windows/OS/Install.ps1
+++ b/scripts/Windows/OS/Install.ps1
@@ -29,32 +29,38 @@ function Start-InstallationLoop {
     . "$PSScriptRoot/../Scripts/SetupConfig.ps1";
 
     while (-not (Get-IsFinished)) {
-        switch (Get-Stage) {
-            $null {
-                if (-not ((Get-Command "choco") -and (Get-Command "refreshenv"))) {
-                    Invoke-Hook "Install-Chocolatey" -Fallback {
-                        # Install chocolatey
-                        [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
-                        Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'));
-                        Import-Module $env:ChocolateyInstall/helpers/chocolateyProfile.psm1;
-                        refreshenv;
-                    };
-                }
-
-                if (-not (Test-ChocoSoftware "powershell-core")) {
-                    Invoke-Hook "Install-PowerShellCore" -Fallback {
-                        choco install -y powershell-core --install-arguments='"ADD_FILE_CONTEXT_MENU_RUNPOWERSHELL=1 ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 REGISTER_MANIFEST=1 USER_MU=1 ENABLE_MU=1"';
-                    };
-
-                    Restart-Intermediate;
-                    return;
-                }
-
-                if (Test-Path $env:PWSH_PATH) {
-                    Remove-Item -Recurse $env:PWSH_PATH;
-                }
+        if (-not (Get-Stage)) {
+            Set-Stage [SetupStage]::Initialize;
+            break;
+        } elseif (Get-Stage -eq [SetupStage]::Initialize) {
+            if (-not ((Get-Command "choco") -and (Get-Command "refreshenv"))) {
+                Invoke-Hook "Install-Chocolatey" -Fallback {
+                    # Install chocolatey
+                    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
+                    Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'));
+                    Import-Module $env:ChocolateyInstall/helpers/chocolateyProfile.psm1;
+                    refreshenv;
+                };
+            }
+
+            if (-not (Test-ChocoSoftware "powershell-core")) {
+                Invoke-Hook "Install-PowerShellCore" -Fallback {
+                    choco install -y powershell-core --install-arguments='"ADD_FILE_CONTEXT_MENU_RUNPOWERSHELL=1 ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 REGISTER_MANIFEST=1 USER_MU=1 ENABLE_MU=1"';
+                };
+
+                Restart-Intermediate;
+                return;
+            }
+
+            if (Test-Path $env:PWSH_PATH) {
+                Remove-Item -Recurse $env:PWSH_PATH;
+            }
+        } else {
+            switch (Get-Stage) {
+                $null {
+                }
+                Default {}
             }
-            Default {}
         }
     }
 }
diff --git a/scripts/Windows/Scripts/SetupConfig.ps1 b/scripts/Windows/Scripts/SetupConfig.ps1
index 439e7305..4b0cf50f 100644
--- a/scripts/Windows/Scripts/SetupConfig.ps1
+++ b/scripts/Windows/Scripts/SetupConfig.ps1
@@ -2,6 +2,11 @@ using namespace Microsoft.Win32;
 using namespace System.Security.AccessControl;
 using namespace System.Security.Principal;
 
+enum SetupStage {
+    Initialize = "init"
+    Install = "install"
+}
+
 $null = New-Module {
     [string] $configRoot = "HKLM:\Software\PortValhalla";
     [string] $stageOption = "Stage";