From 67907e3a53be9be874bc09e317d147ec6d0db270 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 24 Aug 2024 04:07:35 +0200 Subject: [PATCH] Allow configuring nextcloud syncs --- scripts/Windows/Software/Nextcloud/Main.ps1 | 125 +++++++++++++++++--- 1 file changed, 109 insertions(+), 16 deletions(-) diff --git a/scripts/Windows/Software/Nextcloud/Main.ps1 b/scripts/Windows/Software/Nextcloud/Main.ps1 index 26a9cdf7..e76eeb50 100644 --- a/scripts/Windows/Software/Nextcloud/Main.ps1 +++ b/scripts/Windows/Software/Nextcloud/Main.ps1 @@ -3,30 +3,123 @@ param( [hashtable] $Arguments ) +. "$PSScriptRoot/../../Scripts/PowerManagement.ps1"; . "$PSScriptRoot/../../../Common/Scripts/Software.ps1"; -Start-SoftwareInstaller @PSBoundParameters ` +& { + param($Parameters) + + <# + .SYNOPSIS + Gets the path to the Nextcloud configuration file. + #> + function Get-ConfigFile { + return "$env:APPDATA/Nextcloud/nextcloud.cfg"; + } + + <# + .SYNOPSIS + Adds a new nextcloud sync to the current user. + #> + function Add-NextcloudSync { + param( + [string] $RemotePath, + [string] $LocalPath, + [switch] $VirtualFiles + ) + + Write-Host "Adding a Nextcloud sync"; + Write-Host "$RemotePath <=> $LocalPath"; + $configName = "Folders"; + $virtualName = "WithPlaceholders"; + $LocalPath = $LocalPath.Replace("\", "/"); + $RemotePath = $RemotePath.Replace("\", "/"); + $oldContent = Get-Content (Get-ConfigFile); + $additionalSettings = @(); + $pattern = "^\d+\\$configName(?:$virtualName)?\\(\d+)"; + + $folderID = ( + $oldContent | ` + Where-Object { $_ -match "$pattern" } | ` + ForEach-Object { $_ -replace "$pattern.*$","`$1" } | ` + Sort-Object -Unique | ` + Measure-Object -Maximum).Maximum + 1; + + if ($VirtualFiles.IsPresent) { + $configName += $virtualName; + $additionalSettings = @("0\$configName\$folderID\virtualFilesMode=wincfapi"); + } + + $newSettings = ( + @( + "0\$configName\$folderID\localPath=$LocalPath", + "0\$configName\$folderID\targetPath=$RemotePath" + ) + $additionalSettings + ) -join "`n"; + + & { + $accountSectionEntered = $false; + $accountSectionLeft = $false; + + for ($i = 0; $i -lt $oldContent.Count; $i++) { + $line = $oldContent[$i]; + + if ($line -eq "[Accounts]") { + $accountSectionEntered = $true; + } + + if ($line -eq "" -and $accountSectionEntered) { + $accountSectionLeft = $true; + $newSettings; + } + + $line; + + if ( + (-not $accountSectionLeft) -and + ($i -eq ($oldContent.Count - 1))) { + $newSettings; + } + } + } | Set-Content (Get-ConfigFile); + } + + + Start-SoftwareInstaller @Parameters ` -Installer { Install-ChocoPackage nextcloud-client -ArgumentList "-y","--params='/KeepUpdateCheck'"; } ` -UserConfigurator { - if (-not (Test-Path $context.GetNextcloudConfigFile())) { - Write-Information "Setting up Nextcloud configuration"; + param($Arguments) + $user = $Arguments.Name; - Write-Information "Ensuring all Let's Encrypt certificates are cached"; - $null = Invoke-WebRequest https://valid-isrgrootx1.letsencrypt.org/; + & { + $syncs = Get-UserConfig -UserName $user "nextcloud.folderSyncs"; + $configExists = { (Test-Path (Get-ConfigFile) ) }; - 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."; + if ($syncs.Count -gt 0) { + if (-not (& $configExists)) { + while (-not (& $configExists)) { + Read-Host "Please log in to the Nextcloud app and hit enter to continue"; + + if (-not (& $configExists)) { + Write-Error -ErrorAction Continue "The login seems to have failed. Please try again."; + } + } } + + Write-Host "Stopping Nextcloud process"; + $nextcloudProcess = Get-Process nextcloud; + $nextcloudPath = [string]$nextcloudProcess[0].Path; + $nextcloudProcess | Stop-Process -Force; + + foreach ($sync in $syncs) { + Add-NextcloudSync -LocalPath $sync.localPath -RemotePath $sync.remotePath -VirtualFiles:$sync.virtualFiles; + } + + Write-Host "Restarting Nextcloud"; + Start-Process $nextcloudPath; } - - $context.Reboot(); - exit; - } + }; }; - +} $PSBoundParameters;