Write-Host "Connecting to i9-13ks..."

try {
    $remoteCommand = @'
Write-Host "Checking current network connections..."
net use

Write-Host "`nDisconnecting from NEXUS shares..."
# First disconnect from specific NEXUS shares
net use \\NEXUS\amazon /delete /y 2>$null
net use \\NEXUS\flamenco /delete /y 2>$null
net use \\NAS\amazon /delete /y 2>$null
# Then disconnect any mapped drives
net use A: /delete /y 2>$null
net use F: /delete /y 2>$null
net use N: /delete /y 2>$null
Write-Host "Existing NEXUS connections cleared."

Write-Host "`nMapping network drives..."
Write-Host "Connecting drives to NEXUS..."

# First connect to the share without a drive letter
Write-Host "Establishing initial NEXUS connection..."
net use \\NEXUS\amazon /user:Nathan /persistent:yes
if ($LASTEXITCODE -eq 0) {
    Write-Host "Initial NEXUS connection established."
    Start-Sleep -Seconds 2

    Write-Host "`nMapping A: drive..."
    net use A: \\NEXUS\amazon /persistent:yes
    if ($LASTEXITCODE -eq 0) {
        Write-Host "A: drive connected successfully."
        
        Write-Host "`nMapping F: drive..."
        net use F: \\NEXUS\flamenco /persistent:yes
        if ($LASTEXITCODE -eq 0) {
            Write-Host "F: drive connected successfully."
        } else {
            Write-Host "Failed to connect F: drive" -ForegroundColor Red
        }
    } else {
        Write-Host "Failed to connect A: drive" -ForegroundColor Red
    }
} else {
    Write-Host "Failed to establish initial NEXUS connection" -ForegroundColor Red
}

Write-Host "`nMapping N: drive..."
net use N: \\NAS\amazon /user:Nathan /persistent:yes
if ($LASTEXITCODE -eq 0) {
    Write-Host "N: drive connected successfully."
} else {
    Write-Host "Failed to connect N: drive" -ForegroundColor Red
}

Write-Host "`nVerifying connections..."
net use

Write-Host "`nRunning command file..."
$cmdFilePath = "F:\software\Flamenco 3.6\run-flamenco-worker.cmd"
$cmdDirectory = "F:\software\Flamenco 3.6"

if (Test-Path $cmdFilePath) {
    Write-Host "Starting $cmdFilePath..."
    # Change to the directory containing the CMD file first
    Set-Location $cmdDirectory
    # Run the CMD file
    cmd /c $cmdFilePath
} else {
    Write-Host "Error: Command file not found at $cmdFilePath" -ForegroundColor Red
    $customPath = Read-Host "Enter the path to your .cmd file"
    if (Test-Path $customPath) {
        Write-Host "Starting $customPath..."
        # Get the directory of the custom path
        $customDirectory = Split-Path -Parent $customPath
        # Change to the directory containing the CMD file
        Set-Location $customDirectory
        # Run the custom path CMD file
        cmd /c $customPath
    } else {
        Write-Host "Error: Command file not found at custom path" -ForegroundColor Red
    }
}

Write-Host "`nPress Enter to continue..."
Read-Host
'@

    # Encode the command to handle special characters
    $bytes = [System.Text.Encoding]::Unicode.GetBytes($remoteCommand)
    $encodedCommand = [Convert]::ToBase64String($bytes)
    
    # Execute the encoded command on the remote machine
    ssh -t -p 22146 i9-13ks "powershell -EncodedCommand $encodedCommand"

} catch {
    Write-Host "`nAn error occurred:" -ForegroundColor Red
    Write-Host $_.Exception.Message -ForegroundColor Red
}

Write-Host "`nPress Enter to exit..."
Read-Host 