Logeto.psm1

$ScriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent

. ($ScriptDir + "./Global.ps1")
. ($ScriptDir + "./Terminal.ps1")
. ($ScriptDir + "./TerminalService.ps1")

<#
    Return Json file from the URL for set product and scope. If version parameter is set it returns Json file only if higher version was found, otherwise returns null.
#>

function Get-LogetoUpdateInfo 
{
    Param(
        [parameter(Mandatory=$true)]
        [String] $Product,

        [parameter(Mandatory=$false)]
        [String] $Scope,

        [parameter(Mandatory=$false)]
        [String] $Version
    )

    Write-LogetoProgress "Searching for new version." "Get-LogetoUpdateInfo"

    $fileName = $Product

    if (![String]::IsNullOrEmpty($Scope))
    {
        $fileName += "-"
        $fileName += $Scope
    }

    $fileName += ".json"

    $file = [System.IO.Path]::GetTempFileName();
    try
    {
        try
        {
            Write-LogetoDebug "URL of the UpdateInfo file is $UpdateInfoURL$fileName" 

            (New-Object System.Net.WebClient).DownloadFile($UpdateInfoURL + $fileName, $file)
        
            $json = (Get-Content -Raw -Path $file) | ConvertFrom-Json

            $downloadedVersion =  $json | Select -ExpandProperty Version
        
            if (![String]::IsNullOrEmpty($Version))
            {
                if ([System.Version]$downloadedVersion -gt [System.Version]$Version)
                {
                    Write-LogetoDebug "UpdateInfo has been found!" 
                    return $json 
                }
                else
                {
                    Write-LogetoDebug "No newer UpdateInfo has been found!" 
                    return $null
                }
            }

            if (!$json)
            {
                Write-LogetoDebug "Last UpdateInfo has been found!" 
                return $json 
            }
        }
        finally
        {
            if (Test-Path $file)
            {
                Remove-Item $file -Force
            }
        }
    }
    catch [Exception]
    {
        Write-Error $_.Exception.Message
        return $null;
    }

    Write-LogetoDebug "No UpdateInfo has been found!" 
    return $null;
}

<#
    Install any Logeto product
#>

function Install-LogetoProduct
{
    Param(
        [parameter(Mandatory=$true)]
        [ValidateSet('logeto-terminal', 'logeto-terminal-service')]
        $ProductName
    )

    Write-LogetoProgress "Installing Logeto product $ProductName." "Install-LogetoProduct"

    if ($ProductName -eq 'logeto-terminal')
    {
        Install-LogetoTerminal (Get-LogetoTerminalUpdateInfo) $ProductName
    }
    elseif ($ProductName -eq 'logeto-terminal-service')
    {
        Install-LogetoTerminalService (Get-LogetoTerminalServiceUpdateInfo) $ProductName
    }
}

<#
    Uninstall any Logeto product
#>

function Uninstall-LogetoProduct
{
    Param(
        [parameter(Mandatory=$true)]
        [ValidateSet('logeto-terminal', 'logeto-terminal-service')]
        $ProductName
    )

    Write-LogetoProgress "Uninstalling Logeto product $ProductName." "Uninstall-LogetoProduct"

    if ($ProductName -eq 'logeto-terminal')
    {
        Uninstall-LogetoTerminal $ProductName
    }
    elseif ($ProductName -eq 'logeto-terminal-service')
    {
        Uninstall-LogetoTerminalService $ProductName
    }
}

<#
    Install any Logeto product
#>

function Set-LogetoProductEnvironment
{
    Param(
        [parameter(Mandatory=$true)]
        [ValidateSet('logeto-terminal', 'logeto-terminal-service')]
        $ProductName
    )

    Write-LogetoProgress "Setting Logeto product environment for $ProductName." "Set-LogetoProductEnvironment"

    if ($ProductName -eq 'logeto-terminal')
    {
        Set-LogetoTerminalPrepareEnvironment $ProductName
    }
    elseif ($ProductName -eq 'logeto-terminal-service')
    {
        Set-LogetoTerminalServicePrepareEnvironment
    }
}

<#
    Extract zip file to dstination directory
#>

Function Invoke-LogetoExtract($sourceFile, $destionationDir)
{
    Write-LogetoProgress "Extract files to $destionationDir." "Invoke-LogetoExtract"

    if (Test-Path $destionationDir)
    {
        Remove-Item $destionationDir -Recurse -Force
    }

    if (!(Test-Path $destionationDir))
    {
        New-Item -ItemType Directory -Force -Path $destionationDir | Out-Null
    }

    Add-Type -Assembly System.IO.Compression.FileSystem
    [System.IO.Compression.ZipFile]::ExtractToDirectory($sourceFile, $destionationDir)
}