From 554f3c9a63039a112bdeb734092d0af49e3c55fe Mon Sep 17 00:00:00 2001
From: Manuel Thalmann <m@nuth.ch>
Date: Wed, 13 Mar 2024 09:06:48 +0100
Subject: [PATCH] Add a script for forcibly installing `winget`

---
 scripts/Windows/Software/winget/Install.ps1 | 39 +++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 scripts/Windows/Software/winget/Install.ps1

diff --git a/scripts/Windows/Software/winget/Install.ps1 b/scripts/Windows/Software/winget/Install.ps1
new file mode 100644
index 00000000..ced855b8
--- /dev/null
+++ b/scripts/Windows/Software/winget/Install.ps1
@@ -0,0 +1,39 @@
+#!/bin/pwsh
+. "$PSScriptRoot/../../Scripts/Context.ps1";
+
+function Install-Winget {
+    param([Context] $context)
+    Write-Host "Installing winget";
+    $namePattern = "*Desktop*Installer*";
+    [System.Collections.Generic.List[string]] $packages = @();
+    $versions = Get-AppxPackage -AllUsers "$namePattern";
+
+    if ($versions.Count -gt 0) {
+        if (-not $(Get-Command sqlite3)) {
+            $packages.Add("sqlite");
+        }
+
+        if ($packages.Count -gt 0) {
+            Write-Host "Installing prequisites for uninstalling winget";
+
+            foreach ($package in $packages) {
+                choco install -y "$package";
+            }
+        }
+
+        foreach ($version in $versions) {
+            sqlite3 `
+                "$env:ProgramData\Microsoft\Windows\AppRepository\StateRepository-Machine.srd" `
+                "UPDATE Package SET IsInbox = 0 WHERE PackageFullName = '$($version.PackageFullName)'";
+        }
+
+        Get-AppxPackage -AllUsers "$namePattern" | Remove-AppxPackage -AllUsers;
+
+        Get-AppxProvisionedPackage -Online | `
+            Where-Object { $_.DisplayName -like "$namePattern" } | `
+            Remove-AppxProvisionedPackage -Online;
+    }
+
+    choco install -y winget;
+}
+