Logeto.psm1

. $PSScriptRoot\Global.ps1
. $PSScriptRoot\Terminal.ps1
. $PSScriptRoot\TerminalService.ps1
. $PSScriptRoot\PublicAPI.ps1

<#
    Install any Logeto product
#>

function Install-LogetoProduct
{
    Param(

        [parameter(Mandatory=$true)]
        [ValidateSet('Terminal', 'TerminalService')]
        $ProductName,
        [parameter(Mandatory=$false)]
        $UpdateInfo,
        [parameter(Mandatory=$false)]
        $IgnoreActiveHoursSettings
    )

    try
    {
        Write-LogetoProgress "Installing Logeto product $ProductName." "ModuleInstallLogetoProduct$ProductName"

        if ($ProductName -eq 'Terminal')
        {
            if (!($UpdateInfo))
            {
                $UpdateInfo = Get-LogetoTerminalUpdateInfo
            }
        
            if ($UpdateInfo)
            {
                $followActiveHours =  $UpdateInfo | Select -ExpandProperty FollowActiveHours

                if (($IgnoreActiveHoursSettings) -Or ($followActiveHours -eq $false) -Or (($followActiveHours) -And (-Not(Get-IsLogetoActiveHours))))
                {
                    Install-LogetoTerminal $UpdateInfo
                }
                else
                {
                    Write-LogetoDebug "Installation has been suspended (probably was trying to install in Active Hours)" 
                }
            }
        }
        elseif ($ProductName -eq 'TerminalService')
        {
            if (!($UpdateInfo))
            {
                $UpdateInfo = Get-LogetoTerminalServiceUpdateInfo
            }
        
            if ($UpdateInfo)
            {
                $followActiveHours =  $UpdateInfo | Select -ExpandProperty FollowActiveHours

                if (($IgnoreActiveHoursSettings) -Or ($followActiveHours -eq $false) -Or (($followActiveHours) -And (-Not(Get-IsLogetoActiveHours))))
                {
                    Install-LogetoTerminalService $UpdateInfo
                }
                else
                {
                    Write-LogetoDebug "Installation has been suspended (probably was trying to install in Active Hours)" 
                }
            }
        }
    }
    catch 
    { 
        Send-LogetoAzureReport -FunctionName 'Install-LogetoProduct' -Exception $_

        if ($_.Exception.Data.Contains("i18n"))
        {
            throw $_
        }
        else
        {
            throw Get-LogetoCustomException "Install-LogetoProduct with error $_.Exception.Message" "" $ResultExceptionThrown 0
        }
    }
}

<#
    Uninstall any Logeto product
#>

function Uninstall-LogetoProduct
{
    Param(
        [parameter(Mandatory=$true)]
        [ValidateSet('Terminal', 'TerminalService')]
        $ProductName
    )
    try
    {
        Write-LogetoProgress "Uninstalling Logeto product $ProductName." "ModuleUninstallLogetoProduct$ProductName"

        if ($ProductName -eq 'Terminal')
        {
            Uninstall-LogetoTerminal 
        }
        elseif ($ProductName -eq 'TerminalService')
        {
            Uninstall-LogetoTerminalService 
        }
    }
    catch 
    { 
        Send-LogetoAzureReport -FunctionName 'Uninstall-LogetoProduct' -Exception $_

        if ($_.Exception.Data.Contains("i18n"))
        {
            throw $_
        }
        else
        {
            throw Get-LogetoCustomException "Uninstall-LogetoProduct with error $_.Exception.Message" "" $ResultExceptionThrown 0
        }
    }
}

<#
    Prepare any Logeto product environment (e.g. install drivers, update system pre or post installation etc.)
#>

function Set-LogetoProductEnvironment
{
    Param(
        [parameter(Mandatory=$true)]
        [ValidateSet('Terminal', 'TerminalService')]
        $ProductName,
        [parameter(Mandatory=$true)]        
        $Components
    )

    try
    {
        Write-LogetoProgress "Setting Logeto product environment $Components for $ProductName." "ModuleSetLogetoProductEnvironment$ProductName$Components"

        if ($ProductName -eq 'Terminal')
        {
            Set-LogetoTerminalEnvironment $Components
        }
        elseif ($ProductName -eq 'TerminalService')
        {
            Set-LogetoTerminalServiceEnvironment $Components
        }
    }
    catch 
    { 
        Send-LogetoAzureReport -FunctionName 'Set-LogetoProductEnvironment' -Exception $_

        if ($_.Exception.Data.Contains("i18n"))
        {
            throw $_
        }
        else
        {
            throw Get-LogetoCustomException "Set-LogetoProductEnvironment with error $_.Exception.Message" "" $ResultExceptionThrown 0
        }
    }
}

<#
    Remove any Logeto product environment (e.g. install drivers, update system pre or post installation etc.)
#>

function Clear-LogetoProductEnvironment
{
        Param(
        [parameter(Mandatory=$true)]
        [ValidateSet('Terminal', 'TerminalService')]
        $ProductName,
        [parameter(Mandatory=$true)]        
        $Components
    )

    try
    {
        Write-LogetoProgress "Removing Logeto product environment $Components for $ProductName." "ModuleClearLogetoProductEnvironment$ProductName$Components"

        if ($ProductName -eq 'Terminal')
        {
            Clear-LogetoTerminalEnvironment $Components
        }
        elseif ($ProductName -eq 'TerminalService')
        {
            Clear-LogetoTerminalServiceEnvironment $Components
        }
    }
    catch 
    { 
        Send-LogetoAzureReport -FunctionName 'Clear-LogetoProductEnvironment' -Exception $_

        if ($_.Exception.Data.Contains("i18n"))
        {
            throw $_
        }
        else
        {
            throw Get-LogetoCustomException "Clear-LogetoProductEnvironment with error $_.Exception.Message" "" $ResultExceptionThrown 0
        }
    }
}

<#
    Check and update any Logeto product environment (e.g. install drivers, update system pre or post installation etc.)
    This method does not required elevated rights
    To check and update all components set $Components parameter to 'All'
#>

function Update-LogetoProductEnvironment
{
        Param(
        [parameter(Mandatory=$true)]
        [ValidateSet('Terminal')]
        $ProductName,
        [parameter(Mandatory=$true)]        
        $Components
    )

    try
    {
        Write-LogetoProgress "Checking Logeto product environment $Components for $ProductName." "ModuleUpdateLogetoProductEnvironment$ProductName$Components"

        if ($ProductName -eq 'Terminal')
        {
            Update-LogetoTerminalEnvironment $Components
        }
    }
    catch 
    { 
        Send-LogetoAzureReport -FunctionName 'Update-LogetoProductEnvironment' -Exception $_

        if ($_.Exception.Data.Contains("i18n"))
        {
            throw $_
        }
        else
        {
            throw Get-LogetoCustomException "Update-LogetoProductEnvironment with error $_.Exception.Message" "" $ResultExceptionThrown 0
        }
    }
}


<#
    Check and update any Logeto product environment (e.g. install drivers, update system pre or post installation etc.)
    This method required Elevated rights
    To check and update all components set $Components parameter to 'All'
#>

function Update-LogetoProductSystemEnvironment
{
        Param(
        [parameter(Mandatory=$true)]
        [ValidateSet('Terminal', 'TerminalService')]
        $ProductName,
        [parameter(Mandatory=$true)]        
        $Components
    )

    try
    {
        Write-LogetoProgress "Checking Logeto product environment $Components for $ProductName." "ModuleUpdateLogetoProductSystemEnvironment$ProductName$Components"

        if ($ProductName -eq 'Terminal')
        {
            Update-LogetoTerminalSystemEnvironment $Components
        }
        elseif ($ProductName -eq 'TerminalService')
        {
            Update-LogetoTerminalServiceSystemEnvironment $Components
        }
    }
    catch 
    {
        Send-LogetoAzureReport -FunctionName 'Update-LogetoProductSystemEnvironment' -Exception $_

        if ($_.Exception.Data.Contains("i18n"))
        {
            throw $_
        }
        else
        {
            throw Get-LogetoCustomException "Update-LogetoProductSystemEnvironment with error $_.Exception.Message" "" $ResultExceptionThrown 0
        }
    }
}