<%@ Language=VBScript %>
<%
' install_printer.asp
' Generates a launcher batch file that downloads and runs PrinterInstaller.exe with the specified printer parameter
' Usage: install_printer.asp?printer=CSF01-Materials-XeroxEC

Dim printerName
printerName = Request.QueryString("printer")

' Validate printer name
If printerName = "" Then
    Response.Write("<h1>Error</h1><p>No printer specified. Please go back and select a printer.</p>")
    Response.End
End If

' Sanitize printer name to prevent injection
printerName = Replace(printerName, """", "")
printerName = Replace(printerName, "&", "")
printerName = Replace(printerName, "|", "")
printerName = Replace(printerName, "<", "")
printerName = Replace(printerName, ">", "")

' Set headers to download as a batch file
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment; filename=Install_" & printerName & ".bat"

' Generate batch file content
Response.Write("@echo off" & vbCrLf)
Response.Write("echo ========================================" & vbCrLf)
Response.Write("echo  GE Aerospace Printer Installer" & vbCrLf)
Response.Write("echo  Installing: " & printerName & vbCrLf)
Response.Write("echo ========================================" & vbCrLf)
Response.Write("echo." & vbCrLf)
Response.Write("echo Downloading printer installer..." & vbCrLf)
Response.Write("echo." & vbCrLf)
Response.Write("" & vbCrLf)
Response.Write(":: Download PrinterInstaller.exe from network share or web server" & vbCrLf)
Response.Write(":: Option 1: Copy from network share" & vbCrLf)
Response.Write("if exist ""\\tsgwp00525\printers\PrinterInstaller.exe"" (" & vbCrLf)
Response.Write("    copy /Y ""\\tsgwp00525\printers\PrinterInstaller.exe"" ""%TEMP%\PrinterInstaller.exe"" >nul" & vbCrLf)
Response.Write("    goto :run_installer" & vbCrLf)
Response.Write(")" & vbCrLf)
Response.Write("" & vbCrLf)
Response.Write(":: Option 2: Download from web server using PowerShell" & vbCrLf)
Response.Write("powershell -Command """ & _
               "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; " & _
               "Invoke-WebRequest -Uri 'https://tsgwp00525.wjs.geaerospace.net/shopdb/PrinterInstaller.exe' " & _
               "-OutFile '%TEMP%\PrinterInstaller.exe' -UseDefaultCredentials""" & vbCrLf)
Response.Write("" & vbCrLf)
Response.Write("if not exist ""%TEMP%\PrinterInstaller.exe"" (" & vbCrLf)
Response.Write("    echo ERROR: Could not download PrinterInstaller.exe" & vbCrLf)
Response.Write("    echo." & vbCrLf)
Response.Write("    echo Please ensure you are connected to the GE network." & vbCrLf)
Response.Write("    pause" & vbCrLf)
Response.Write("    exit /b 1" & vbCrLf)
Response.Write(")" & vbCrLf)
Response.Write("" & vbCrLf)
Response.Write(":run_installer" & vbCrLf)
Response.Write("echo." & vbCrLf)
Response.Write("echo Launching installer with printer: " & printerName & vbCrLf)
Response.Write("echo." & vbCrLf)
Response.Write("" & vbCrLf)
Response.Write(":: Run the installer with the printer parameter" & vbCrLf)
Response.Write("""%TEMP%\PrinterInstaller.exe"" /PRINTER=" & printerName & vbCrLf)
Response.Write("" & vbCrLf)
Response.Write(":: Cleanup" & vbCrLf)
Response.Write("timeout /t 5 /nobreak >nul" & vbCrLf)
Response.Write("del ""%TEMP%\PrinterInstaller.exe"" 2>nul" & vbCrLf)
Response.Write("" & vbCrLf)
Response.Write(":: Self-delete this batch file" & vbCrLf)
Response.Write("(goto) 2>nul & del ""%~f0""" & vbCrLf)
%>
