@echo off
REM Test script for Xerox driver installation
REM Run as Administrator
REM Tests the same commands used by XeroxOfflineInstaller.exe

setlocal enabledelayedexpansion

echo.
echo ============================================
echo   Xerox Driver Installation Test Script
echo   Run as Administrator
echo ============================================
echo.

REM Set path to your driver folder (adjust as needed)
set "DRIVER_DIR=C:\temp\xerox_drivers"
set "CAT_FILE=%DRIVER_DIR%\x3UNIVx.cat"
set "INF_FILE=%DRIVER_DIR%\x3UNIVX.inf"
set "DRIVER_NAME=Xerox Global Print Driver PCL6"

echo Driver directory: %DRIVER_DIR%
echo.

REM ============================================
echo TEST 1: Check if files exist
echo ============================================
echo.

if exist "%CAT_FILE%" (
    echo   [OK] Catalog file: %CAT_FILE%
) else (
    echo   [FAIL] Catalog file NOT found: %CAT_FILE%
    echo.
    echo   Please copy Xerox driver files to %DRIVER_DIR%
    echo   Required files: x3UNIVx.cat, x3UNIVX.inf, and all driver files
    echo.
    pause
    exit /b 1
)

if exist "%INF_FILE%" (
    echo   [OK] INF file: %INF_FILE%
) else (
    echo   [FAIL] INF file NOT found: %INF_FILE%
    pause
    exit /b 1
)

echo.

REM ============================================
echo TEST 2: Add catalog to TrustedPublisher store
echo ============================================
echo.
echo Running: certutil -addstore TrustedPublisher "%CAT_FILE%"
echo.
certutil -addstore TrustedPublisher "%CAT_FILE%"
set CERT_RESULT=!errorlevel!
echo.
echo Exit code: %CERT_RESULT%

if %CERT_RESULT% equ 0 (
    echo [OK] Certificate catalog added successfully
) else (
    echo [INFO] certutil returned %CERT_RESULT% - may already be installed
)

echo.

REM ============================================
echo TEST 2b: Disable driver signature enforcement
echo ============================================
echo.

REM Method 1: Set policy to Ignore (0) via registry
echo Setting driver signing policy to Ignore...
reg add "HKLM\SOFTWARE\Microsoft\Driver Signing" /v Policy /t REG_BINARY /d 00 /f
echo Exit code: %errorlevel%

REM Method 2: Disable via WMIC (Windows 7)
echo.
echo Disabling driver signing via Group Policy registry...
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows NT\Driver Signing" /v BehaviorOnFailedVerify /t REG_DWORD /d 0 /f 2>nul
echo Exit code: %errorlevel%

REM Method 3: Set Code Integrity policy
echo.
echo Setting Code Integrity policy...
reg add "HKLM\SYSTEM\CurrentControlSet\Control\CI" /v UMCIDisabled /t REG_DWORD /d 1 /f 2>nul

echo.

REM ============================================
echo TEST 3: Install driver with pnputil
echo ============================================
echo.

REM Try Windows 7 syntax first (works on all versions)
echo Running: pnputil -i -a "%INF_FILE%"
echo   (-i = install, -a = add driver package)
echo.
pnputil -i -a "%INF_FILE%"
set PNPRESULT=!errorlevel!
echo.
echo Exit code: %PNPRESULT%

REM If Windows 7 syntax fails, try Windows 10+ syntax
if !PNPRESULT! neq 0 (
    echo.
    echo Trying Windows 10+ syntax...
    echo Running: pnputil /add-driver "%INF_FILE%" /install
    echo.
    pnputil /add-driver "%INF_FILE%" /install
    set PNPRESULT=!errorlevel!
    echo.
    echo Exit code: !PNPRESULT!
)

echo.
if !PNPRESULT! equ 0 (
    echo [OK] Driver added to store successfully
) else if !PNPRESULT! equ 259 (
    echo [OK] Driver already in store
) else (
    echo [WARN] pnputil returned !PNPRESULT! - check output above for details
)

echo.

REM ============================================
echo TEST 4: Verify driver installation
echo ============================================
echo.

echo Checking if driver is registered with print spooler...
echo.

REM Try PowerShell method (Windows 10+)
powershell -NoProfile -Command "Get-PrinterDriver -Name '%DRIVER_NAME%' -ErrorAction SilentlyContinue | Select-Object Name, PrinterEnvironment" 2>nul
if %errorlevel% equ 0 (
    echo.
    echo [OK] Driver found via PowerShell
) else (
    echo PowerShell check failed - trying wmic...
    wmic printer driver where "name like '%%Xerox%%'" get name 2>nul
)

echo.

REM ============================================
echo TEST 5: Check TrustedPublisher store
echo ============================================
echo.

echo Certificates/CTLs containing "Xerox" in TrustedPublisher:
certutil -store TrustedPublisher | findstr /i "xerox"
if %errorlevel% neq 0 (
    echo   (No Xerox entries found - checking for Microsoft HW Publisher...)
    certutil -store TrustedPublisher | findstr /i "Microsoft Windows Hardware"
)

echo.

REM ============================================
echo SUMMARY
echo ============================================
echo.
echo Results:
echo   - Certificate install (TEST 2): Exit code %CERT_RESULT%
echo   - Driver install (TEST 3): Exit code %PNPRESULT%
echo.
echo If both exit codes are 0 or 1, installation succeeded!
echo.
echo Next: Connect your Xerox printer via USB.
echo Windows should automatically detect and configure it.
echo.
echo ============================================
echo.
pause
