
@echo off
setlocal enabledelayedexpansion

set var=%1

set OsArch=%PROCESSOR_ARCHITECTURE%

rem If the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\PackagesPending" exists,
rem windows updates are pending.
rem on the following line ">nul 2>&1" redirects STDOUT to nul and STDERR to STDOUT, so no "output" or "error" info is displayed to the user.
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\PackagesPending" >nul 2>&1
if %ERRORLEVEL% == 0 (
    echo Windows updates are pending. Restart the computer to complete the updates before continuing.
    GOTO SyntaxEnd
)

if /i "%var%" == "uninstall" (
    echo Begin !var!. 
	echo Make sure your USB printer cable is still plugged in.
    echo No activity will be reported for about 30 seconds while the driver files are deleted.
	echo.
    start /wait autoplay\networkInstall\%OsArch%\dxp01install.exe uninstall
) else if /i "%var%" == "update" (
    echo Begin %var% 
    echo No activity will be reported for about 30 seconds while the driver files are updated.
    start /wait autoplay\networkInstall\%OsArch%\dxp01install.exe upgrade
) else (
    goto Syntax
)

if %ERRORLEVEL% == 0 (
    if /i "%var%" == "uninstall" (
        ECHO If this is a USB connection, when the uninstall finishes, disconnect the USB cable from the printer.
        ECHO Driver has been uninstalled. Restart the computer.
    ) else if /i "%var%" == "update" (
        ECHO Printer Driver update successful
        ECHO Please restart the computer
    ) 
    GOTO SyntaxEnd
) 
if %ERRORLEVEL% == 1 (
    if /i "%var%" == "uninstall" (
        ECHO Printer Driver uninstall failed
    ) else if /i "%var%" == "update" (
        ECHO Printer Driver update failed
    ) 
    GOTO SyntaxEnd
)
if %ERRORLEVEL% == 2 (
    ECHO !var! Printer Driver update needed
    GOTO SyntaxEnd
)
if %ERRORLEVEL% == 3 (
    ECHO Newer driver already installed
    GOTO SyntaxEnd
)
if %ERRORLEVEL% == 4 (
    ECHO Driver cannot be uninstalled until printers are removed
    ECHO The driver has not been uninstalled.
    GOTO SyntaxEnd
)
if %ERRORLEVEL% == 5 (
    ECHO !var! Failed.
    ECHO Printer name is invalid.
    GOTO SyntaxEnd
)
if %ERRORLEVEL% == 6 (
    ECHO !var! Failed.
    ECHO Windows updates are pending.
    GOTO SyntaxEnd
)
if %ERRORLEVEL% == 100 (
    ECHO If this is a USB connection, when the uninstall finishes, disconnect the USB cable from the printer.
    ECHO Driver has been uninstalled. Please restart the computer.
    GOTO SyntaxEnd
)

ECHO "%var%" error = %ERRORLEVEL%
GOTO SyntaxEnd

:Syntax
ECHO.
ECHO DriverUtility.cmd must be invoked from a command prompt using 'run as administrator'.
ECHO. 
ECHO The DriverUtility.cmd allows unattended update or uninstall of the XPS Card Printer Driver. 
ECHO. 
ECHO Example:
ECHO. 
ECHO DriverUtility.cmd uninstall
ECHO Uninstall the Card Printer Driver from the computer.
ECHO Remove all XPS card printers before starting the uninstall process.
ECHO Uninstall may require that the computer be restarted when the uninstall process is complete.
ECHO.
ECHO DriverUtility.cmd update
ECHO Updates the Card Printer Driver to the latest version.
ECHO Make sure that all print jobs have completed before starting the update.
ECHO The computer will need to be restarted when the update process is complete.
ECHO.
:SyntaxEnd
