public/Install-NordVPN.ps1

#Requires -RunAsAdministrator
#Requires -Modules @{ModuleName="msi";ModuleVersion="3.2.3.1024"}
$ErrorActionPreference = "Stop"

Import-Module -Name "$(Split-Path $PSScriptRoot -Parent)\Private\Helpers.psm1" -DisableNameChecking

function Install-NordVpn
{
    <#
    .SYNOPSIS
        Installs NordVPN app
  
    .PARAMETER version
        Version of NordVPN app to install
    .PARAMETER paths
        Folder where to install
    .PARAMETER force
        Indicates if to remove existing NordVPN installation
    .PARAMETER displayLog
        Indicates if to display installation log
    .PARAMETER dontRun
        Indicates if to don't launch NordVPN after installation
    #>

    [CmdletBinding()]
    param(
        [string]$version,
        [string]$path,
        [switch]$force,
        [switch]$displayLog,
        [switch]$dontRun
    )

    $existingInstallation = Get-MSIProductInfo -Name "NordVPN"
    if ($existingInstallation -ne $null)
    {
        Write-Host "Found existing installation of NordVPN $($existingInstallation.ProductVersion)"
        Write-Host "Product code $($existingInstallation.ProductCode)"
        Write-Host "Install location $($existingInstallation.InstallLocation)"
        if (!$force.IsPresent)
        {
            Write-Host "Exiting. Specify -force to force the installation."
            return
        }

        Write-Host "Removing existing installation"
        Uninstall-MSIProduct -ProductCode $existingInstallation.ProductCode
    }

    $source = ""
    if ($version)
    {
        $source = "http://downloads.nordvpn.com/apps/windows/NordVPN+Setup-$version.exe"
    }
    else 
    {
        $source = "http://downloads.nordvpn.com/apps/windows/NordVPN beta setup.exe"
    }
    
    $destination = $null
    $result = $null
    $logFileName = [System.IO.Path]::GetRandomFileName()

    try
    {
        $destination = [System.IO.Path]::GetTempFileName() + ".exe"
        Write-Host "Downloading $version..."
        $response = Invoke-WebRequest $source -OutFile $destination

        Write-Host "Installing..."
        $result = RunInstaller -installerFile $destination -path $path -logFileName $logFileName
    }
    finally
    {
        if ($destination -and (Test-Path $destination))
        {
            Remove-Item $destination -ErrorAction Ignore
        }
    }

    if ($displayLog.IsPresent)
    {
        DisplayLog $logFileName
    }

    if ($result.ExitCode -ne 0)
    {
        Write-Host "Failed to install NordVPN" -ForegroundColor Red
        return
    }
    
    $installation = Get-MSIProductInfo -Name "NordVPN"
    Write-Host "NordVPN $($installation.ProductVersion) has been successfully installed" -ForegroundColor Green

    if (!$dontRun.IsPresent)
    {
        Start-Process -FilePath (Join-Path $installation.InstallLocation "NordVPN.exe")
    }
}

function RunInstaller($installerFile, $path, $logFileName)
{
    $installerArgs = @("/exenoui", "/qn", "/norestart")
    if ($path)
    {
        $installerArgs += "APPDIR=""$path"""
    }

    $installerArgs += "/li ""$logFileName"""
    
    # Can't use MSI cmdlets here because Advanced Installer exe packages are not supported
    $result = Execute-Command -path $installerFile -arguments $installerArgs
    return $result
}

function DisplayLog($fileName)
{
    $logPath = Join-Path $env:TEMP $fileName
    Get-Content $logPath | Write-host 
}