# 09-Install-PrinterInstallerMap.ps1 - imaging-time placement of the
# PrinterInstallerMap.exe tool plus a Public Desktop shortcut.
# Runs for every shopfloor PC type (Standard, CMM, Keyence, Display,
# etc.) so a tech can launch the printer installer at any bay without
# going through Edge. Numbered 09 to run after 06-OrganizeDesktop
# (which creates Shopfloor Tools folder) and 08-EdgeDefaultBrowser.
#
# Why this exists: the legacy printer-deploy flow was a web map
# (printerinstallermap.asp) that handed users a per-selection .bat to
# download and run. New Edge policy blocks both .bat downloads and
# unsigned .exe runs. PrinterInstallerMap is a forked installer with the
# map UX baked into a custom Inno wizard page; baking the EXE onto the
# image at PXE time means it never goes through a browser (no MOTW, no
# SmartScreen prompt). UAC still fires because PrivilegesRequired=admin
# and supportuser creds satisfy it - that is the only remaining gate.
#
# Layout this script produces on a Standard PC:
#   C:\Tools\PrinterInstallerMap\PrinterInstallerMap.exe
#   C:\Users\Public\Desktop\Shopfloor Tools\Printer Installer.lnk
#
# The Shopfloor Tools desktop folder is also created/maintained by
# 06-OrganizeDesktop.ps1; this script tolerates either order (it creates
# the folder if absent so it can run before 06).
#
# Idempotent: re-imaging or re-running the script overwrites the EXE and
# rewrites the shortcut. Drop-in newer PrinterInstallerMap.exe alongside
# this script in the imaging payload, re-image the PC, the desktop link
# now points to the new build.
#
# Source EXE expected at $PSScriptRoot\PrinterInstallerMap.exe (placed
# next to this script when the imaging payload lands). Until the Inno
# compile + sign+ship step is wired in (see ../../docs/ + the inno repo
# feature/printer-map branch), this script no-ops with a warning if the
# EXE is missing - re-image will pick it up once the build artifact is
# dropped in.

$ErrorActionPreference = 'Continue'

$src             = Join-Path $PSScriptRoot 'PrinterInstallerMap.exe'
$installDir      = 'C:\Tools\PrinterInstallerMap'
$installExe      = Join-Path $installDir 'PrinterInstallerMap.exe'
$publicDesktop   = 'C:\Users\Public\Desktop'
$shopfloorTools  = Join-Path $publicDesktop 'Shopfloor Tools'
$shortcutPath    = Join-Path $shopfloorTools 'Printer Installer.lnk'

if (-not (Test-Path -LiteralPath $src)) {
    Write-Warning "Install-PrinterInstallerMap: source EXE not found at $src - skipping. Re-image after the build artifact is dropped alongside this script."
    exit 0
}

# Stage EXE
if (-not (Test-Path -LiteralPath $installDir)) {
    New-Item -Path $installDir -ItemType Directory -Force | Out-Null
}
try {
    Copy-Item -LiteralPath $src -Destination $installExe -Force -ErrorAction Stop
    Write-Host "Install-PrinterInstallerMap: staged EXE -> $installExe"
} catch {
    Write-Error "Install-PrinterInstallerMap: failed to copy EXE: $_"
    exit 1
}

# Ensure Shopfloor Tools folder exists. 06-OrganizeDesktop.ps1 also
# creates this; we create it here so this script does not depend on
# 06 having run first.
if (-not (Test-Path -LiteralPath $shopfloorTools)) {
    try {
        New-Item -Path $shopfloorTools -ItemType Directory -Force | Out-Null
    } catch {
        Write-Warning "Install-PrinterInstallerMap: could not create $shopfloorTools : $_"
    }
}

# Create / overwrite shortcut
try {
    $wsh = New-Object -ComObject WScript.Shell
    $lnk = $wsh.CreateShortcut($shortcutPath)
    $lnk.TargetPath        = $installExe
    $lnk.IconLocation      = "$installExe,0"
    $lnk.WorkingDirectory  = $installDir
    $lnk.Description       = 'Install network printers from the WJ site map'
    $lnk.Save()
    Write-Host "Install-PrinterInstallerMap: shortcut -> $shortcutPath"
} catch {
    Write-Error "Install-PrinterInstallerMap: failed to create shortcut: $_"
    exit 1
}

exit 0
