## Define URL for downloading the installer
$URL = 'https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/resources/Nextcloud-3.0.3-setup.exe'

## Stop app if it's currently running
End-Process -Name 'nextcloud' -Force

## Get the install location so we can start the app again after installation
If ( Test-Path "HKLM:SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Nextcloud" )
{ $InstallPath = '{0}\nextcloud.exe' -f $(Get-ItemProperty -Path "HKLM:SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Nextcloud" -ErrorAction SilentlyContinue).InstallLocation }
ElseIf ( Test-Path "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Nextcloud" )
{ $InstallPath = '{0}\nextcloud.exe' -f $(Get-ItemProperty -Path "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Nextcloud" -ErrorAction SilentlyContinue).InstallLocation }

## Install package
Install-Program -Path $(Download-File -URL $URL) -Arguments "/S" -Delete

## Start the app after installation
If ( Test-Path $InstallPath )
{
    LogMsg "Starting Nextcloud"
    Try { Start-Process $InstallPath }
    Catch { LogErr $_.Exception.Message }
}
