#!/bin/pwsh
. "$PSScriptRoot/Manage.ps1";
. "$PSScriptRoot/Upgrade.ps1";
. "$PSScriptRoot/User.ps1";
. "$PSScriptRoot/../Scripts/Context.ps1";

function Invoke-WindowsInstallation([Context] $context)
{
    Write-Host "Starting Installation and Restoration of Windows";
    . "$PSScriptRoot/../Scripts/Prerequisites.ps1";
    Update-WindowsInstallation $context;

    if (-not $context.Get("SoftwarePrerequisitesInstalled")) {
        Write-Host "Installing prerequisites for installing software";

        Install-Module -AcceptLicense -Force "NuGet";
        Import-Module NuGet;

        choco install -y firefox selenium-gecko-driver;
        Install-Package -Force Selenium.WebDriver -SkipDependencies;

        $context.Set("SoftwarePrerequisitesInstalled", 1, "DWord");
    }

    if (-not $context.Get("DriversInstalled")) {
        Write-Host "Installing Drivers";
        Write-Information "Looking for driver installation function";

        if ((Get-Command Install-PortValhallaDrivers -ErrorAction SilentlyContinue)) {
            Write-Information "Driver installation function was found. Starting installation";
            Install-PortValhallaDrivers $context;
        }

        Write-Information "Finished installing drivers";
        $context.Set("DriversInstalled", 1, "DWord");
        $context.Reboot();
        exit;
    }

    New-PersonalUser $context;
    Restore-WindowsInstallation $context;
}