From a7b40d2376d2760cb3b915b8c0c52369e5d0a70d Mon Sep 17 00:00:00 2001
From: Manuel Thalmann <m@nuth.ch>
Date: Tue, 3 Sep 2024 12:08:14 +0200
Subject: [PATCH] Install Ubuntu automatically

---
 scripts/Windows/Scripts/WSL.ps1 | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/scripts/Windows/Scripts/WSL.ps1 b/scripts/Windows/Scripts/WSL.ps1
index 96350f6e..3039fc66 100644
--- a/scripts/Windows/Scripts/WSL.ps1
+++ b/scripts/Windows/Scripts/WSL.ps1
@@ -63,8 +63,14 @@ function Install-Wsl {
 function Install-WslDistribution {
     $dir = Get-WslDistributionPath;
     $root = Split-Path -Parent $dir;
+    $ubuntuPattern = "*Ubuntu*";
     $registryPath = "HKCU:/Software/Microsoft/Windows/CurrentVersion/Lxss";
     $key = Get-Item $registryPath;
+
+    if (-not (Get-AppxPackage $ubuntuPattern)) {
+        Install-Wsl;
+    }
+
     wsl --shutdown;
 
     if ($key) {
@@ -75,7 +81,7 @@ function Install-WslDistribution {
         $null = New-Item -ItemType Directory $root;
     }
 
-    Copy-Item -Recurse -Force (Get-AppxPackage "*Ubuntu*").InstallLocation $dir;
+    Copy-Item -Recurse -Force (Get-AppxPackage $ubuntuPattern).InstallLocation $dir;
     Set-UserPermissions $dir;
     & "$dir\ubuntu.exe" install --root;
     wsl --shutdown;