param(
    [string]$PCType    = 'Shopfloor',
    [string]$PCSubType = ''
)

$ErrorActionPreference = 'Continue'

Write-Host '================================================================'
Write-Host "=== VM test harness: pcType=$PCType subType=$PCSubType ==="
Write-Host '================================================================'

& net use '\\192.168.122.1\pxe-images' /user:camp vos313 2>&1 | Out-Null
Write-Host "SMB mount status: $LASTEXITCODE"

Write-Host ''
Write-Host '[setup] cleaning prior test state'
Remove-Item -Recurse -Force 'C:\Enrollment'             -ErrorAction SilentlyContinue
Remove-Item -Recurse -Force 'C:\Program Files\GE\Shopfloor' -ErrorAction SilentlyContinue
Remove-Item -Recurse -Force 'C:\Logs\Shopfloor'         -ErrorAction SilentlyContinue
Remove-Item -Path 'HKLM:\SOFTWARE\GE\SFLD\Credentials\samba' -Recurse -Force -ErrorAction SilentlyContinue

Write-Host '[setup] staging enforcer runtime'
New-Item -Path 'C:\Program Files\GE\Shopfloor\lib' -ItemType Directory -Force | Out-Null
Copy-Item '\\192.168.122.1\pxe-images\enforcer-stage\GE-Enforce.ps1'           'C:\Program Files\GE\Shopfloor\GE-Enforce.ps1' -Force
Copy-Item '\\192.168.122.1\pxe-images\enforcer-stage\lib\Install-FromManifest.ps1' 'C:\Program Files\GE\Shopfloor\lib\Install-FromManifest.ps1' -Force

Write-Host "[setup] creating enrollment stubs (pcType=$PCType subType=$PCSubType)"
New-Item -Path 'C:\Enrollment' -ItemType Directory -Force | Out-Null
$PCType    | Set-Content -Path 'C:\Enrollment\pc-type.txt'    -Encoding ascii
$PCSubType | Set-Content -Path 'C:\Enrollment\pc-subtype.txt' -Encoding ascii
$siteConfig = @{
    shopfloorShareRoot = '\\192.168.122.1\pxe-images\tsgwp00525-v2\shared\dt\shopfloor'
    siteName = 'West Jefferson (VM test)'
} | ConvertTo-Json -Depth 5
$siteConfig | Set-Content -Path 'C:\Enrollment\site-config.json' -Encoding ascii

Write-Host '[setup] injecting fake SFLD creds'
$credKey = 'HKLM:\SOFTWARE\GE\SFLD\Credentials\samba'
New-Item -Path $credKey -Force | Out-Null
New-ItemProperty -Path $credKey -Name TargetHost -Value '192.168.122.1' -PropertyType String -Force | Out-Null
New-ItemProperty -Path $credKey -Name Username   -Value 'camp'          -PropertyType String -Force | Out-Null
New-ItemProperty -Path $credKey -Name Password   -Value 'vos313'        -PropertyType String -Force | Out-Null

Write-Host ''
Write-Host '================================================================'
Write-Host '=== Running GE-Enforce.ps1 ==='
Write-Host '================================================================'

$stdoutFile = [System.IO.Path]::GetTempFileName()
$stderrFile = [System.IO.Path]::GetTempFileName()
$argString = '-NoProfile -ExecutionPolicy Bypass -File "C:\Program Files\GE\Shopfloor\GE-Enforce.ps1"'
$proc = Start-Process -FilePath 'powershell.exe' -ArgumentList $argString `
    -NoNewWindow -Wait -PassThru `
    -RedirectStandardOutput $stdoutFile -RedirectStandardError $stderrFile
$rc = $proc.ExitCode
Write-Host "--- GE-Enforce stdout ---"
Get-Content -Path $stdoutFile -ErrorAction SilentlyContinue
Write-Host "--- GE-Enforce stderr ---"
Get-Content -Path $stderrFile -ErrorAction SilentlyContinue
Remove-Item $stdoutFile, $stderrFile -Force -ErrorAction SilentlyContinue
Write-Host ''
Write-Host "GE-Enforce exited $rc"
