. "$PSScriptRoot/../../Scripts/Context.ps1";

$null = New-Module {
    $path = "$env:LOCALAPPDATA/LGHUB";
    $softwareName = "LGHub";

    function Invoke-BackupLGHub([Context] $context) {
        $hubName = "lghub.exe";
        $hubPath = $(Get-Process | Where-Object { [System.IO.Path]::GetFileName($_.Path) -eq $hubName })[0].Path;

        $mainProcesses = Get-Process | Where-Object { @($hubName, "lghub_system_tray.exe") -contains [System.IO.Path]::GetFileName($_.Path) -and $(
            $_.Parent.ProcessName -eq "explorer" -or $null -eq $_.Parent) };

        $mainProcesses | Foreach-Object { $_.Kill($true) };
        $context.Backup($path, $context.SoftwareArchive($softwareName), @("-i!settings.db", "-i!icon_cache"));

        if ($hubPath) {
            Start-Process $hubPath;
        }
    }
}