# Deploy STARTNET.BAT path fix to AD2
# Fixes all references from C:\NET\STARTNET.BAT to C:\STARTNET.BAT
# Date: 2026-01-20

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Deploying STARTNET Path Fix" -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)

# Files to deploy
$Files = @(
    @{Name="AUTOEXEC.BAT"; Dest="\\$AD2Host\C$\Shares\test\COMMON\ProdSW\AUTOEXEC.BAT"},
    @{Name="DEPLOY.BAT"; Dest="\\$AD2Host\C$\Shares\test\COMMON\ProdSW\DEPLOY.BAT"},
    @{Name="UPDATE.BAT"; Dest="\\$AD2Host\C$\Shares\test\COMMON\ProdSW\UPDATE.BAT"},
    @{Name="CTONW.BAT"; Dest="\\$AD2Host\C$\Shares\test\COMMON\ProdSW\CTONW.BAT"},
    @{Name="CHECKUPD.BAT"; Dest="\\$AD2Host\C$\Shares\test\COMMON\ProdSW\CHECKUPD.BAT"},
    @{Name="NWTOC.BAT"; Dest="\\$AD2Host\C$\Shares\test\COMMON\ProdSW\NWTOC.BAT"},
    @{Name="DOSTEST.BAT"; Dest="\\$AD2Host\C$\Shares\test\COMMON\ProdSW\DOSTEST.BAT"}
)

# Also deploy to _COMMON\ProdSW
$FilesAlt = @(
    @{Name="AUTOEXEC.BAT"; Dest="\\$AD2Host\C$\Shares\test\_COMMON\ProdSW\AUTOEXEC.BAT"},
    @{Name="DEPLOY.BAT"; Dest="\\$AD2Host\C$\Shares\test\_COMMON\ProdSW\DEPLOY.BAT"},
    @{Name="UPDATE.BAT"; Dest="\\$AD2Host\C$\Shares\test\_COMMON\ProdSW\UPDATE.BAT"},
    @{Name="CTONW.BAT"; Dest="\\$AD2Host\C$\Shares\test\_COMMON\ProdSW\CTONW.BAT"},
    @{Name="CHECKUPD.BAT"; Dest="\\$AD2Host\C$\Shares\test\_COMMON\ProdSW\CHECKUPD.BAT"},
    @{Name="NWTOC.BAT"; Dest="\\$AD2Host\C$\Shares\test\_COMMON\ProdSW\NWTOC.BAT"},
    @{Name="DOSTEST.BAT"; Dest="\\$AD2Host\C$\Shares\test\_COMMON\ProdSW\DOSTEST.BAT"}
)

# 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
}

# Deploy to COMMON\ProdSW
Write-Host "Deploying to COMMON\ProdSW..." -ForegroundColor Cyan
$SuccessCount = 0
foreach ($File in $Files) {
    $SourceFile = "D:\ClaudeTools\$($File.Name)"

    if (-not (Test-Path $SourceFile)) {
        Write-Host "  [ERROR] Source not found: $($File.Name)" -ForegroundColor Red
        continue
    }

    try {
        Copy-Item -Path $SourceFile -Destination $File.Dest -Force -ErrorAction Stop
        Write-Host "  [OK] $($File.Name)" -ForegroundColor Green
        $SuccessCount++
    } catch {
        Write-Host "  [ERROR] $($File.Name): $_" -ForegroundColor Red
    }
}
Write-Host ""

# Deploy to _COMMON\ProdSW
Write-Host "Deploying to _COMMON\ProdSW..." -ForegroundColor Cyan
foreach ($File in $FilesAlt) {
    $SourceFile = "D:\ClaudeTools\$($File.Name)"

    if (-not (Test-Path $SourceFile)) {
        Write-Host "  [ERROR] Source not found: $($File.Name)" -ForegroundColor Red
        continue
    }

    try {
        Copy-Item -Path $SourceFile -Destination $File.Dest -Force -ErrorAction Stop
        Write-Host "  [OK] $($File.Name)" -ForegroundColor Green
    } catch {
        Write-Host "  [ERROR] $($File.Name): $_" -ForegroundColor Red
    }
}
Write-Host ""

# Cleanup
Remove-PSDrive -Name TEMP_AD2 -ErrorAction SilentlyContinue

# Summary
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Deployment Complete" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Fixed Files Deployed: 7" -ForegroundColor White
Write-Host "  - AUTOEXEC.BAT (C:\STARTNET.BAT reference)" -ForegroundColor Gray
Write-Host "  - DEPLOY.BAT (C:\STARTNET.BAT reference)" -ForegroundColor Gray
Write-Host "  - UPDATE.BAT (C:\STARTNET.BAT + XCOPY fix)" -ForegroundColor Gray
Write-Host "  - CTONW.BAT (C:\STARTNET.BAT reference)" -ForegroundColor Gray
Write-Host "  - CHECKUPD.BAT (C:\STARTNET.BAT reference)" -ForegroundColor Gray
Write-Host "  - NWTOC.BAT (C:\STARTNET.BAT reference)" -ForegroundColor Gray
Write-Host "  - DOSTEST.BAT (C:\STARTNET.BAT reference)" -ForegroundColor Gray
Write-Host ""
Write-Host "Changes:" -ForegroundColor Yellow
Write-Host "  C:\NET\STARTNET.BAT -> C:\STARTNET.BAT" -ForegroundColor White
Write-Host ""
Write-Host "Next Steps:" -ForegroundColor Yellow
Write-Host "1. AD2 will sync to NAS within 15 minutes" -ForegroundColor White
Write-Host "2. Machines will get updates on next reboot or NWTOC run" -ForegroundColor White
Write-Host "3. Verify STARTNET.BAT exists at C:\STARTNET.BAT on machines" -ForegroundColor White
Write-Host ""
