# Stop the three services
Get-Service -Name RSysLnkCns, RSysLogger, WksEmitter | Stop-Service -Force

# Display a dialog box for the user to select a folder
$selectedFolder = Get-Item -Path 'C:\borisconfig\*' | Out-GridView -PassThru -Title "Pick a Folder"

# Get the list of config files in the selected folder
$configs = Get-ChildItem -Path $selectedFolder.FullName -File

# Check that the user has selected at least one config file
if ($configs.Count -eq 0) {
    Write-Warning "No config files found in selected folder"
    pause
    exit
}

# Move the contents of the destination folder to a backup folder
$backupDir = "C:\Rejsekort\Conf\OldConfig"
if (!(Test-Path $backupDir)) {
    New-Item -ItemType Directory -Path $backupDir | Out-Null
}
Move-Item -Path "C:\Rejsekort\Conf\*" -Destination $backupDir -Force -ErrorAction SilentlyContinue

# Copy the config files to the destination folder
Copy-Item -Path $configs.FullName -Destination "C:\Rejsekort\Conf\"  -Force

# Get the selected choice from the user
$choices = "buc", "preprod", "prod", "testa", "testc"
$selectedChoice = $choices | Out-GridView -PassThru -Title "Pick a choice"


# Modify the RSysLnkCns.exe.config file based on the selected choice
$configFile = "C:\Rejsekort\Bin\RSysLnkCns.exe.config"
if (Test-Path $configFile) {
    $config = Get-Content $configFile
    switch ($selectedChoice) {
        "buc" {
            $config[44] = '  <endpoint address="https://sales-buc.east-west.dk/WksConfig.Service" binding="webHttpBinding" bindingConfiguration="webHttps" behaviorConfiguration="webHttps" contract="ROSA.WksConfig.Service.Interface.IWksConfigService" name="ROSA.WksConfig.Service.WksConfigService" />'
        }
        "preprod" {
            $config[44] = '  <endpoint address="https://sales-preprod.east-west.dk/WksConfig.Service" binding="webHttpBinding" bindingConfiguration="webHttps" behaviorConfiguration="webHttps" contract="ROSA.WksConfig.Service.Interface.IWksConfigService" name="ROSA.WksConfig.Service.WksConfigService" />'
        }
        "prod" {
            $config[44] = '  <endpoint address="https://sales-prod.east-west.dk/WksConfig.Service" binding="webHttpBinding" bindingConfiguration="webHttps" behaviorConfiguration="webHttps" contract="ROSA.WksConfig.Service.Interface.IWksConfigService" name="ROSA.WksConfig.Service.WksConfigService" />'
        }
        "testc" {
            $config[44] = '  <endpoint address="https://sales-tstc.east-west.dk/WksConfig.Service" binding="webHttpBinding" bindingConfiguration="webHttps" behaviorConfiguration="webHttps" contract="ROSA.WksConfig.Service.Interface.IWksConfigService" name="ROSA.WksConfig.Service.WksConfigService" />'
        }
        "testa" {
            $config[44] = '  <endpoint address="https://sales-tsta.east-west.dk/WksConfig.Service" binding="webHttpBinding" bindingConfiguration="webHttps" behaviorConfiguration="webHttps" contract="ROSA.WksConfig.Service.Interface.IWksConfigService" name="ROSA.WksConfig.Service.WksConfigService" />'
        }
    }
    Set-Content $configFile -Value $config
} else {
    Write-Warning "Could not find config file $configFile"
    pause
    exit
}

# Start the three services
Get-Service -Name RSysLnkCns, RSysLogger, WksEmitter | Start-Service

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show('Please wait 1-2 minutes before trying to connect in order for the client to sync config', 'PlatformConfigScript')

