# Fix root UPDATE.BAT - Deploy correct redirect script
# Date: 2026-01-20

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Fixing Root UPDATE.BAT" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# AD2 Connection Details
$AD2Host = "192.168.0.6"
$Username = "INTRANET\sysadmin"
$Password = ConvertTo-SecureString "Paper123!@#" -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential($Username, $Password)

# Source file - the redirect script
$SourceFile = "D:\ClaudeTools\UPDATE-ROOT.BAT"
$DestFile = "\\$AD2Host\C$\Shares\test\UPDATE.BAT"

# Verify source file exists
if (-not (Test-Path $SourceFile)) {
    Write-Host "[ERROR] Source file not found: $SourceFile" -ForegroundColor Red
    exit 1
}

Write-Host "[OK] Source file found: $SourceFile" -ForegroundColor Green
Write-Host ""

# Map network drive
Write-Host "Connecting to AD2..." -ForegroundColor Yellow
try {
    New-PSDrive -Name TEMP_AD2 -PSProvider FileSystem -Root "\\$AD2Host\C$" -Credential $Cred -ErrorAction Stop | Out-Null
    Write-Host "[OK] Connected to AD2" -ForegroundColor Green
    Write-Host ""
} catch {
    Write-Host "[ERROR] Failed to connect to AD2: $_" -ForegroundColor Red
    exit 1
}

# Check if DEPLOY.BAT exists in root (should NOT exist)
Write-Host "Checking for DEPLOY.BAT in root..." -ForegroundColor Yellow
$DeployInRoot = "\\$AD2Host\C$\Shares\test\DEPLOY.BAT"
if (Test-Path $DeployInRoot) {
    Write-Host "[WARNING] DEPLOY.BAT found in root - will delete" -ForegroundColor Yellow
    Remove-Item $DeployInRoot -Force
    Write-Host "[OK] DEPLOY.BAT deleted from root" -ForegroundColor Green
} else {
    Write-Host "[OK] DEPLOY.BAT not in root (correct)" -ForegroundColor Green
}
Write-Host ""

# Deploy correct UPDATE.BAT (redirect script) to root
Write-Host "Deploying UPDATE.BAT redirect script to root..." -ForegroundColor Yellow
try {
    Copy-Item -Path $SourceFile -Destination $DestFile -Force -ErrorAction Stop
    Write-Host "[OK] UPDATE.BAT redirect deployed to root" -ForegroundColor Green
    Write-Host ""
} catch {
    Write-Host "[ERROR] Failed to deploy: $_" -ForegroundColor Red
    Remove-PSDrive -Name TEMP_AD2 -ErrorAction SilentlyContinue
    exit 1
}

# Verify content
Write-Host "Verifying UPDATE.BAT content..." -ForegroundColor Yellow
$Content = Get-Content $DestFile -Raw
if ($Content -like "*CALL T:\COMMON\ProdSW\DEPLOY.BAT*") {
    Write-Host "[OK] UPDATE.BAT correctly calls DEPLOY.BAT" -ForegroundColor Green
} else {
    Write-Host "[ERROR] UPDATE.BAT does not contain correct redirect!" -ForegroundColor Red
}
Write-Host ""

# Show root BAT files
Write-Host "Root BAT files in C:\Shares\test\:" -ForegroundColor Cyan
Get-ChildItem "\\$AD2Host\C$\Shares\test\*.BAT" -ErrorAction SilentlyContinue |
    Select-Object Name, Length, LastWriteTime |
    Format-Table -AutoSize

# Cleanup
Remove-PSDrive -Name TEMP_AD2 -ErrorAction SilentlyContinue

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Fix Complete" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Correct structure:" -ForegroundColor Yellow
Write-Host "  T:\UPDATE.BAT           -> Redirect to DEPLOY.BAT" -ForegroundColor White
Write-Host "  T:\COMMON\ProdSW\DEPLOY.BAT  -> Deployment installer" -ForegroundColor White
Write-Host "  T:\COMMON\ProdSW\UPDATE.BAT  -> Backup utility (v2.1)" -ForegroundColor White
Write-Host ""
Write-Host "Usage on DOS machine:" -ForegroundColor Yellow
Write-Host "  T:\UPDATE TS-4R         -> Runs deployment for TS-4R" -ForegroundColor White
Write-Host ""
Write-Host "After deployment, backup utility is at:" -ForegroundColor Yellow
Write-Host "  C:\BAT\UPDATE.BAT       -> Full backup utility" -ForegroundColor White
