# Generated by ImpTune — Printer: {{ printer_name }}
# Install command: powershell.exe -NoProfile -ExecutionPolicy Bypass -File "install.ps1"
#
# This script installs the printer "{{ printer_name }}" via Intune Win32 app deployment.
# It must be launched with -File (not -Command) so $PSScriptRoot is populated.

# ---------------------------------------------------------------------------
# WOW64 Guard — relaunch in 64-bit PowerShell if running under WOW64 (Intune
# runs Win32 app scripts in a 32-bit process; pnputil is 64-bit only).
# This block MUST be the first executable code in the script.
# ---------------------------------------------------------------------------
if ($env:PROCESSOR_ARCHITECTURE -eq "x86" -and $env:PROCESSOR_ARCHITEW6432) {
    $ps64 = "$env:WINDIR\SysNative\WindowsPowerShell\v1.0\powershell.exe"
    & $ps64 -NoProfile -ExecutionPolicy Bypass -File "$PSCommandPath" @args
    exit $LASTEXITCODE
}

# ---------------------------------------------------------------------------
# SYSTEM vs User context detection + UAC self-elevation
# Skip elevation when already running as SYSTEM (Intune MDM context).
# ---------------------------------------------------------------------------
$id       = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$isSystem = $id.IsSystem
$isAdmin  = ([System.Security.Principal.WindowsPrincipal]$id).IsInRole(
                [System.Security.Principal.WindowsBuiltInRole]::Administrator)

if (-not $isSystem -and -not $isAdmin) {
    Start-Process powershell.exe `
        -Verb Runas `
        -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" `
        -Wait
    exit $LASTEXITCODE
}

# ---------------------------------------------------------------------------
# pnputil two-step driver staging
# Step 1: Stage INF + all associated files into Windows Driver Store
# Step 2: Install the named driver from the Driver Store
# ---------------------------------------------------------------------------
pnputil.exe /add-driver "$PSScriptRoot\drivers\{{ inf_filename }}" /install

Add-PrinterDriver -Name "{{ driver_name }}"

# ---------------------------------------------------------------------------
# Idempotent port creation
# ---------------------------------------------------------------------------
if (-not (Get-PrinterPort -Name "{{ port_name }}" -ErrorAction SilentlyContinue)) {
    Add-PrinterPort -Name "{{ port_name }}" -PrinterHostAddress "{{ ip_address }}"
}

# ---------------------------------------------------------------------------
# Idempotent printer creation
# ---------------------------------------------------------------------------
if (-not (Get-Printer -Name "{{ printer_name }}" -ErrorAction SilentlyContinue)) {
    Add-Printer -Name "{{ printer_name }}" `
                -PortName "{{ port_name }}" `
                -DriverName "{{ driver_name }}"
}

# ---------------------------------------------------------------------------
# Configure print settings
# ---------------------------------------------------------------------------
Set-PrintConfiguration -PrinterName "{{ printer_name }}" `
    -DuplexingMode {{ duplex_mode }} `
    -Color ${{ color }} `
    -PaperSize {{ paper_size }} `
    -Collate ${{ collate }}
