$ErrorActionPreference = 'Stop'
$release = '__IMAGE_VERSION__'
$isV2 = [System.Convert]::ToBoolean('__IS_V2__')

New-Item -Path 'C:\ProgramData\Workshopz' -ItemType Directory -Force | Out-Null
Set-Content -Path 'C:\ProgramData\Workshopz\image-version.txt' -Value $release -Encoding ascii
New-Item -Path 'HKLM:\SOFTWARE\Workshopz\GoldenImage' -Force | Out-Null
New-ItemProperty -Path 'HKLM:\SOFTWARE\Workshopz\GoldenImage' -Name Release -Value $release -PropertyType String -Force | Out-Null
New-ItemProperty -Path 'HKLM:\SOFTWARE\Workshopz\GoldenImage' -Name Channel -Value 'stable' -PropertyType String -Force | Out-Null

Install-WindowsFeature -Name Web-Server -IncludeManagementTools | Out-Null
$page = @"
<!doctype html><html><head><title>Workshopz Golden Image</title></head>
<body><h1>Azure Golden Image $release</h1><p>Windows Server 2025 • Trusted Launch ready</p></body></html>
"@
Set-Content -Path 'C:\inetpub\wwwroot\index.html' -Value $page -Encoding utf8
Set-Service -Name W3SVC -StartupType Automatic

if ($isV2) {
    Set-Content -Path 'C:\ProgramData\Workshopz\baseline-v2.marker' -Value 'Image baseline 2 enabled' -Encoding ascii
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Workshopz\GoldenImage' -Name BaselineRevision -Value 2 -PropertyType DWord -Force | Out-Null
}
else {
    Remove-Item -LiteralPath 'C:\ProgramData\Workshopz\baseline-v2.marker' -Force -ErrorAction SilentlyContinue
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Workshopz\GoldenImage' -Name BaselineRevision -Value 1 -PropertyType DWord -Force | Out-Null
}
