#!/bin/pwsh
. "$PSScriptRoot/../../Scripts/KnownFolders.ps1";
. "$PSScriptRoot/../../../Common/Scripts/Context.ps1";

$null = New-Module {
    $path = "$((Get-KnownFolder -SpecialFolder ProgramFilesX86).Path)/osu!";
    $softwareName = "osu!";
    $genericConfigName = "osu!.User.cfg";

    function Backup-Osu([Context] $context) {
        Write-Host "Backing up osu!";
        $archive = $context.SoftwareArchive($softwareName);
        Write-Information "Backing up important files";
        $context.Backup($path, $archive, @("-i@`"$PSScriptRoot/include.txt`""));
        Write-Information "Renaming user configuration to $genericConfigName";
        & 7z rn "$archive" "osu!.$env:USERNAME.cfg" $genericConfigName;
    }

    function Restore-Osu([Context] $context) {
        Write-Host "Restoring osu!";
        Write-Information "Installing osu!";
        choco install -y osu;
        $context.RemoveDesktopIcon("*osu*");
        Write-Information "Restoring files";
        $context.Restore($context.SoftwareArchive($softwareName), $path, @("-aos"));
        $configName = "osu!.$env:USERNAME.cfg";
        Write-Information "Renaming user configuration to $configName";
        Push-Location $path;

        if ((Test-Path $genericConfigName)) {
            Rename-Item $genericConfigName $configName;
        }

        Pop-Location;
    }
}