; HP Universal Print Driver - Offline USB Installer
; Installs HP Universal Printing PCL 6 for USB-connected printers
; Supports Windows 7+ (includes certificate trust bypass for older systems)

[Setup]
AppId={{9D5E6F73-7G4C-5F0D-BG3E-0C8F6G5F4D3C}}
AppName=HP Universal Print Driver (USB)
AppVersion=7.2.0
AppPublisher=WJDT
AppPublisherURL=http://tsgwp00524.logon.ds.ge.com
AppSupportURL=http://tsgwp00524.logon.ds.ge.com
AppUpdatesURL=http://tsgwp00524.logon.ds.ge.com
CreateAppDir=no
ChangesAssociations=no
PrivilegesRequired=admin
ArchitecturesInstallIn64BitMode=x64compatible
OutputDir=.\Output
OutputBaseFilename=HPOfflineInstaller
SolidCompression=yes
WizardStyle=modern
SetupIconFile=gea-logo.ico
WizardImageFile=patrick.bmp
WizardSmallImageFile=patrick-sm.bmp
CreateUninstallRegKey=no
DisableWelcomePage=no

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Messages]
WelcomeLabel2=This utility will install the HP Universal Print Driver for USB-connected printers.%n%nDriver: HP Universal Printing PCL 6%n%nAfter installation:%n1. Connect your HP printer via USB%n2. Windows will automatically detect and configure it%n%nClick Next to install the driver.

[Files]
; HP Universal Print Driver x64
Source: "drivers\hp_x64\*"; DestDir: "{tmp}\hp_drivers_x64"; Flags: ignoreversion recursesubdirs; Check: Is64BitInstallMode

; HP Universal Print Driver x32
Source: "drivers\hp_x32\*"; DestDir: "{tmp}\hp_drivers_x32"; Flags: ignoreversion recursesubdirs; Check: not Is64BitInstallMode

[Code]
procedure CurStepChanged(CurStep: TSetupStep);
var
  BatchScript: String;
  TempScriptPath: String;
  ResultCode: Integer;
  IsX64: Boolean;
  HPDriverDir, HPInfFile, HPCatFile: String;
begin
  if CurStep = ssPostInstall then
  begin
    // Determine architecture and driver paths
    IsX64 := Is64BitInstallMode;

    if IsX64 then
    begin
      HPDriverDir := ExpandConstant('{tmp}\hp_drivers_x64');
      HPInfFile := ExpandConstant('{tmp}\hp_drivers_x64\hpcu355u.inf');
      HPCatFile := ExpandConstant('{tmp}\hp_drivers_x64\hpcu355u.cat');
    end
    else
    begin
      HPDriverDir := ExpandConstant('{tmp}\hp_drivers_x32');
      HPInfFile := ExpandConstant('{tmp}\hp_drivers_x32\hpcu355c.inf');
      HPCatFile := ExpandConstant('{tmp}\hp_drivers_x32\hpcu355c.cat');
    end;

    // Build installation batch script
    BatchScript :=
      '@echo off' + #13#10 +
      'setlocal enabledelayedexpansion' + #13#10 +
      '' + #13#10 +
      'echo.' + #13#10 +
      'echo ========================================' + #13#10 +
      'echo   HP Universal Print Driver Installer' + #13#10 +
      'echo ========================================' + #13#10 +
      'echo.' + #13#10 +
      '' + #13#10 +
      'set "DRIVER_NAME=HP Universal Printing PCL 6"' + #13#10 +
      'set "INF_FILE=' + HPInfFile + '"' + #13#10 +
      'set "CAT_FILE=' + HPCatFile + '"' + #13#10 +
      'set "DRIVER_DIR=' + HPDriverDir + '"' + #13#10 +
      '' + #13#10 +
      'echo Step 1: Installing driver certificate to trusted store...' + #13#10 +
      'echo.' + #13#10 +
      '' + #13#10 +
      'REM Find the catalog file' + #13#10 +
      'if not exist "%CAT_FILE%" (' + #13#10 +
      '    for %%f in ("%DRIVER_DIR%\*.cat") do (' + #13#10 +
      '        set "CAT_FILE=%%f"' + #13#10 +
      '    )' + #13#10 +
      ')' + #13#10 +
      '' + #13#10 +
      'if exist "%CAT_FILE%" (' + #13#10 +
      '    echo   Catalog file: %CAT_FILE%' + #13#10 +
      '    echo.' + #13#10 +
      '' + #13#10 +
      '    REM Add catalog directly to TrustedPublisher store' + #13#10 +
      '    certutil -addstore TrustedPublisher "%CAT_FILE%" >nul 2>&1' + #13#10 +
      '    if !errorlevel! equ 0 (' + #13#10 +
      '        echo   Certificate catalog added to TrustedPublisher store' + #13#10 +
      '    ) else (' + #13#10 +
      '        echo   Note: certutil returned !errorlevel! - may already be installed' + #13#10 +
      '    )' + #13#10 +
      ') else (' + #13#10 +
      '    echo   WARNING: No catalog file found - continuing anyway...' + #13#10 +
      ')' + #13#10 +
      '' + #13#10 +
      'echo.' + #13#10 +
      'echo Step 2: Disabling driver signature enforcement...' + #13#10 +
      'echo.' + #13#10 +
      '' + #13#10 +
      'REM Set driver signing policy to Ignore (0) - prevents "untrusted" dialogs' + #13#10 +
      'reg add "HKLM\SOFTWARE\Microsoft\Driver Signing" /v Policy /t REG_BINARY /d 00 /f >nul 2>&1' + #13#10 +
      'if !errorlevel! equ 0 (' + #13#10 +
      '    echo   Driver signing policy set to Ignore' + #13#10 +
      ') else (' + #13#10 +
      '    echo   Note: Could not set driver signing policy' + #13#10 +
      ')' + #13#10 +
      '' + #13#10 +
      'REM Also set via Group Policy registry key' + #13#10 +
      'reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows NT\Driver Signing" /v BehaviorOnFailedVerify /t REG_DWORD /d 0 /f >nul 2>&1' + #13#10 +
      '' + #13#10 +
      'echo.' + #13#10 +
      'echo Step 3: Installing printer driver...' + #13#10 +
      'echo.' + #13#10 +
      '' + #13#10 +
      'if not exist "%INF_FILE%" (' + #13#10 +
      '    echo   ERROR: INF file not found: %INF_FILE%' + #13#10 +
      '    echo.' + #13#10 +
      '    pause' + #13#10 +
      '    exit /b 1' + #13#10 +
      ')' + #13#10 +
      'echo   INF file: %INF_FILE%' + #13#10 +
      'echo.' + #13#10 +
      '' + #13#10 +
      'echo   Adding driver to Windows driver store...' + #13#10 +
      'echo.' + #13#10 +
      '' + #13#10 +
      'REM Try Windows 7 syntax first (works on all Windows versions)' + #13#10 +
      'REM -i = install on matching devices, -a = add driver package' + #13#10 +
      'pnputil -i -a "%INF_FILE%"' + #13#10 +
      'set PNPRESULT=!errorlevel!' + #13#10 +
      '' + #13#10 +
      'REM If Windows 7 syntax fails, try Windows 10+ syntax' + #13#10 +
      'if !PNPRESULT! neq 0 (' + #13#10 +
      '    echo.' + #13#10 +
      '    echo   Trying alternative syntax...' + #13#10 +
      '    pnputil /add-driver "%INF_FILE%" /install' + #13#10 +
      '    set PNPRESULT=!errorlevel!' + #13#10 +
      ')' + #13#10 +
      '' + #13#10 +
      'echo.' + #13#10 +
      '' + #13#10 +
      'if !PNPRESULT! equ 0 (' + #13#10 +
      '    echo   Driver added to store successfully' + #13#10 +
      ') else if !PNPRESULT! equ 259 (' + #13#10 +
      '    echo   Driver already in store' + #13#10 +
      ') else (' + #13#10 +
      '    echo   pnputil returned: !PNPRESULT!' + #13#10 +
      '    echo   Trying PrintUI method...' + #13#10 +
      '    echo.' + #13#10 +
      '    rundll32 printui.dll,PrintUIEntry /ia /m "%DRIVER_NAME%" /f "%INF_FILE%"' + #13#10 +
      ')' + #13#10 +
      '' + #13#10 +
      'echo.' + #13#10 +
      'echo ========================================' + #13#10 +
      'echo.' + #13#10 +
      'echo   Installation complete!' + #13#10 +
      'echo.' + #13#10 +
      'echo   Driver: %DRIVER_NAME%' + #13#10 +
      'echo.' + #13#10 +
      'echo   Next Steps:' + #13#10 +
      'echo   1. Connect your HP printer via USB' + #13#10 +
      'echo   2. Windows will automatically detect it' + #13#10 +
      'echo   3. Printer will appear in Devices and Printers' + #13#10 +
      'echo.' + #13#10 +
      'echo ========================================' + #13#10 +
      'echo.' + #13#10 +
      'pause';

    // Save and execute batch script
    TempScriptPath := ExpandConstant('{tmp}\install_hp_driver.bat');
    SaveStringToFile(TempScriptPath, BatchScript, False);

    Exec('cmd.exe',
         '/c "' + TempScriptPath + '"',
         '', SW_SHOW, ewWaitUntilTerminated, ResultCode);

    DeleteFile(TempScriptPath);
  end;
end;

// Skip directory selection page
function ShouldSkipPage(PageID: Integer): Boolean;
begin
  Result := False;
  if PageID = wpSelectDir then
    Result := True;
end;
