From f04ec8be9cdf4aedadc13c16aa55a328da7956cf Mon Sep 17 00:00:00 2001
From: Manuel Thalmann <m@nuth.ch>
Date: Wed, 7 Aug 2024 23:53:12 +0200
Subject: [PATCH] Add scripts for desktop apps

---
 scripts/Windows/OS/Install.ps1                |  9 +++
 scripts/Windows/Software/Firefox/Manage.ps1   | 43 ++++++++++++++
 scripts/Windows/Software/Nextcloud/Manage.ps1 | 58 +++++++++++++++++++
 3 files changed, 110 insertions(+)
 create mode 100644 scripts/Windows/Software/Firefox/Manage.ps1
 create mode 100644 scripts/Windows/Software/Nextcloud/Manage.ps1

diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1
index f6d96156..78deed68 100644
--- a/scripts/Windows/OS/Install.ps1
+++ b/scripts/Windows/OS/Install.ps1
@@ -196,6 +196,15 @@ $null = New-Module {
                             . "$softwarePath/WinSCP/Manage.ps1" @arguments;
                             . "$softwarePath/Thunderbird/Manage.ps1" @arguments;
                         }
+
+                        if (Test-Collection "desktopExperience") {
+                            # Internet Access
+                            . "$softwarePath/Firefox/Manage.ps1" @arguments;
+
+                            if (Test-Collection "fileSync") {
+                                . "$softwarePath/Nextcloud/Manage.ps1" @arguments;
+                            }
+                        }
                     };
                 }
 
diff --git a/scripts/Windows/Software/Firefox/Manage.ps1 b/scripts/Windows/Software/Firefox/Manage.ps1
new file mode 100644
index 00000000..8f2503ae
--- /dev/null
+++ b/scripts/Windows/Software/Firefox/Manage.ps1
@@ -0,0 +1,43 @@
+param(
+    $Action,
+    [hashtable] $Arguments
+)
+
+. "$PSScriptRoot/../../Scripts/AppAssociations.ps1";
+. "$PSScriptRoot/../../../Common/Scripts/Software.ps1";
+
+Start-SoftwareInstaller @PSBoundParameters `
+    -Installer {
+        param(
+            [scriptblock] $Installer
+        )
+
+        Install-ChocoPackage firefox;
+        & $Installer -Action ([InstallerAction]::Configure)
+    } `
+    -Configurator {
+        Write-Host "Making Firefox the default browser…";
+        $progIdSuffix = "-308046B0AF4A39CB";
+        $appName = "Firefox";
+
+        $extensions = @(
+            ".htm",
+            ".html",
+            ".svg",
+            ".xht",
+            ".xhtml"
+        );
+
+        $schemes = @(
+            "http",
+            "https"
+        );
+
+        foreach ($extension in $extensions) {
+            Set-DefaultAppAssociation -Identifier $extensions -ProgId "${appName}HTML$progIdSuffix" -ApplicationName $appName;
+        }
+
+        foreach ($scheme in $schemes) {
+            Set-DefaultAppAssociation -Identifier $scheme -ProgId "${appName}URL$progIdSuffix" -ApplicationName $appName;
+        }
+    };
diff --git a/scripts/Windows/Software/Nextcloud/Manage.ps1 b/scripts/Windows/Software/Nextcloud/Manage.ps1
new file mode 100644
index 00000000..710fab67
--- /dev/null
+++ b/scripts/Windows/Software/Nextcloud/Manage.ps1
@@ -0,0 +1,58 @@
+param(
+    $Action,
+    [hashtable] $Arguments
+)
+
+. "$PSScriptRoot/../../../Common/Scripts/Software.ps1";
+
+Start-SoftwareInstaller @PSBoundParameters `
+    -Installer {
+        Install-ChocoPackage nextcloud-client -ArgumentList "-y","--params='/KeepUpdateCheck'";
+    } `
+    -Configurator {
+        Write-Host "Making Firefox the default browser…";
+        $progIdSuffix = "-308046B0AF4A39CB";
+        $appName = "Firefox";
+
+        $extensions = @(
+            ".htm",
+            ".html",
+            ".svg",
+            ".xht",
+            ".xhtml"
+        );
+
+        $schemes = @(
+            "http",
+            "https"
+        );
+
+        foreach ($extension in $extensions) {
+            Set-DefaultAppAssociation -Identifier $extensions -ProgId "${appName}HTML$progIdSuffix" -ApplicationName $appName;
+        }
+
+        foreach ($scheme in $schemes) {
+            Set-DefaultAppAssociation -Identifier $scheme -ProgId "${appName}URL$progIdSuffix" -ApplicationName $appName;
+        }
+    } `
+    -UserConfigurator {
+        if (-not (Test-Path $context.GetNextcloudConfigFile())) {
+            Write-Information "Setting up Nextcloud configuration";
+
+            Write-Information "Ensuring all Let's Encrypt certificates are cached";
+            $null = Invoke-WebRequest https://valid-isrgrootx1.letsencrypt.org/;
+
+            while (-not (Test-Path $context.GetNextcloudConfigFile())) {
+                Write-Host "Nextcloud has been installed!";
+                Read-Host "Please log in in the Nextcloud app and hit enter to continue";
+
+                if (-not (Test-Path $context.GetNextcloudConfigFile())) {
+                    Write-Error "The login seems to have failed. Please try again.";
+                }
+            }
+
+            $context.Reboot();
+            exit;
+        }
+    };
+