@echo off
cd /d %~d0%~p0
@rem.
@rem =======================================================================================
@rem Name: nulldrvinstall.bat 
@rem Version: %NULLDRVINSTALL_VER%
@rem Description: NULL driver install for Honeywell HID barcode scanner
@rem Usage:
@rem
@rem nulldrvinstall.bat /install ==> Install NULL driver. Scanner will use HID class driver
@rem                                 for all Windows OS
@rem nulldrvinstall.bat /uninstall ==> Uninstall NULL driver. Scanner will use the
@rem                                   a/ new POS HID barcode driver for Windows 8.1, and later.
@rem                                   b/ HID class driver for Windows 8.0 and earlier
@rem 
@rem Revision History:
@rem  Version       Author      Description
@rem   1.0          KL          Initial Release
@rem                            Return Code
@rem                             0 ==> success
@rem                             1 ==> fail
@rem   1.1          KL          1/ Add NULL driver version support
@rem   1.2          KL          Adding 32/64 bits stuffs
@rem   1.3          KL          A lots of changes
@rem   1.4			KL          Fix an issue with path contains space
@rem =======================================================================================
@rem.
setlocal enableextensions enabledelayedexpansion
set NUL="%TEMP%\nul"
set RETURNCODE=0
set DEVCON=
set PNPUTIL=
set DEVMAN=
set DEVMAN-INSTALLOPT=
set DEVMAN-REMOVEOPT=
set DEVMAN-UNINSTALLOPT=
set DriverVer=
@rem checking for admin right here. If not, print out a warning for the user
@rem and exit out.
net session > %NUL% 2>&1
if %errorlevel% NEQ 0 (
echo ...Administrator Privilege is required to install the NULL driver
echo ...Please consult the README.txt for install/uninstall instruction
set RETURNCODE=%errorlevel%
goto DISP
)
if exist %SystemRoot%\System32\reg.exe (
set REG=%SystemRoot%\System32\reg.exe
) else (
echo ...reg.exe does not exist... bail ...
set RETURNCODE=1
goto ERROR
)
if exist %SystemRoot%\System32\pnputil.exe (
set PNPUTIL=%SystemRoot%\System32\pnputil.exe
)

@rem ... Query version of the driver ??
if "%1" EQU "/version" (
set /p DriverVer=<.\null_driver_version.txt
echo !DriverVer!
goto VERSION
)

if "%1" EQU "/install" (
set INSTALL-OPT=install
) else (
if "%1" EQU "/uninstall" (
set INSTALL-OPT=uninstall
) else (
goto USAGE
)
)

@rem.
@rem Figure out "system type" and "os type"
@rem Zero out the environment variable here ....
@set TARGETOS=
@set NULLDRVINSTALL_VER=1.4
@set POSREADY2009=yes
@set NULLHIDFILESIZE=0

echo nulldriverinstall version: %NULLDRVINSTALL_VER%
@rem figure out if findstr.exe exist ....
if exist %SystemRoot%\System32\findstr.exe (
set FINDSTR="%SystemRoot%\System32\findstr.exe"
set POSREADY2009=no
) else (
echo POSREADY2009=%POSREADY2009%
set FINDSTR="%cd%\tools\x86\findString.exe"
)
@rem.

@rem Now we need to figure out if the environment variable PROCESSOR_ARCHITEW6432 is defined as AMD64
@rem if not, then this is a 32 bit install and we can use PROCESSOR_ARCHITECTURE 
@rem if it is defined, then this is 64 bit system
if "%PROCESSOR_ARCHITEW6432%"=="AMD64" (
@set DEVCON="%cd%\tools\x64\devcon_64.exe"
@set SYSTEMTYPE=x64
) else (
if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
@echo executing as standalone in 64 bit OS ...
@set DEVCON="%cd%\tools\x64\devcon_64.exe"
@set SYSTEMTYPE=x64
) else (
@set SYSTEMTYPE=x86
@set DEVCON="%cd%\tools\x86\devcon_86.exe"
)
)

if "%POSREADY2009%"=="yes" ( goto DONEOS )
ver | %FINDSTR% /C:"5.0" > %NUL% 2>&1
if %ERRORLEVEL%==0 goto WIN2K
ver | %FINDSTR% /C:"5.1" > %NUL%
if %ERRORLEVEL%==0 goto WINXP
ver | %FINDSTR% /C:"5.2" > %NUL%
if %ERRORLEVEL%==0 goto WINXP_64
ver | %FINDSTR% /C:"6.0" > %NUL%
if %ERRORLEVEL%==0 goto WINVISTA
ver | %FINDSTR% /C:"6.1" > %NUL%
if %ERRORLEVEL%==0 goto WIN7
ver | %FINDSTR% /C:"6.2" > %NUL%
if %ERRORLEVEL%==0 goto WIN8
ver | %FINDSTR% /C:"6.3" > %NUL% 2>&1
if %ERRORLEVEL%==0 goto WIN81
ver | %FINDSTR% /C:"10.0" > %NUL% 2>&1
if %ERRORLEVEL%==0 goto WIN10
goto DONEOS

:WIN2K
@set TARGETOS="Windows 2000"
echo Unsupported OS: %TARGETOS% ...
set RETURNCODE=1
goto ERROR

:WINXP
@set TARGETOS="Windows XP"
@set DEVMAN-REMOVEOPT=!DEVCON! -r remove
@set DEVMAN-INSTALLOPT=!DEVCON! dp_add
@set DEVMAN-UNINSTALLOPT=-f dp_delete
goto DONEOS

:WINXP_64
@set TARGETOS="Windows XP 64"
@set DEVMAN-REMOVEOPT=!DEVCON! -r remove
@set DEVMAN-INSTALLOPT=!DEVCON! dp_add
@set DEVMAN-UNINSTALLOPT=-f dp_delete
goto DONEOS

:WINVISTA
@set TARGETOS="Windows Vista"
@set DEVMAN-REMOVEOPT=!DEVCON! -r remove
@rem set DEVMAN-INSTALLOPT=!PNPUTIL! -i -a
@rem DEVMAN-UNINSTALLOPT=!PNPUTIL! -f -d
@set DEVMAN-INSTALLOPT=!DEVCON! dp_add
@set DEVMAN-UNINSTALLOPT=-f dp_delete
goto DONEOS

:WIN7
@set TARGETOS="Windows 7"
@set DEVMAN-REMOVEOPT=!DEVCON! -r remove
@rem set DEVMAN-INSTALLOPT=!PNPUTIL! -i -a
@rem DEVMAN-UNINSTALLOPT=!PNPUTIL! -f -d
@set DEVMAN-INSTALLOPT=!DEVCON! dp_add
@set DEVMAN-UNINSTALLOPT=-f dp_delete
goto DONEOS

:WIN8
@set TARGETOS="Windows 8"
@set DEVMAN-REMOVEOPT=!DEVCON! -r remove
@rem set DEVMAN-INSTALLOPT=!PNPUTIL! -i -a
@rem DEVMAN-UNINSTALLOPT=!PNPUTIL! -f -d
@set DEVMAN-INSTALLOPT=!DEVCON! dp_add
@set DEVMAN-UNINSTALLOPT=-f dp_delete
goto DONEOS

:WIN81
@set TARGETOS="Windows 8.1"
@set DEVMAN-REMOVEOPT=!DEVCON! -r remove
@rem set DEVMAN-INSTALLOPT=!PNPUTIL! -i -a
@rem DEVMAN-UNINSTALLOPT=!PNPUTIL! -f -d
@set DEVMAN-INSTALLOPT=!DEVCON! dp_add
@set DEVMAN-UNINSTALLOPT=-f dp_delete
goto DONEOS

:WIN10
@set TARGETOS="Windows 10"
@set DEVMAN-REMOVEOPT=!DEVCON! -r remove
@rem set DEVMAN-INSTALLOPT=!PNPUTIL! -i -a
@rem DEVMAN-UNINSTALLOPT=!PNPUTIL! -f -d
@set DEVMAN-INSTALLOPT=!DEVCON! dp_add
@set DEVMAN-UNINSTALLOPT=-f dp_delete
goto DONEOS

:DONEOS
@echo TARGETOS=!TARGETOS!
@echo SYSTEMTYPE=!SYSTEMTYPE!
@rem echo DEVCON=!DEVCON!
@rem echo PNPUTIL=!PNPUTIL!
@rem echo DEVMAN-REMOVEOPT=!DEVMAN-REMOVEOPT!
@rem echo DEVMAN-INSTALLOPT=!DEVMAN-INSTALLOPT!
@rem echo DEVMAN-UNINSTALLOPT=!DEVMAN-UNINSTALLOPT!
@rem, note, at this point, if target is Windows 2000, TARGETOS will not be set ....
@rem Now, set the path for the Driver Store location depending on the OS
@rem, this is to make sure to remove any devnode created on Windows 8.1 with the new HID Barcode Scanner inbox driver ...
!DEVMAN-REMOVEOPT! *HID\VID_0C2E*PID_*7 *HID\VID_0C2E*PID_*3 *HID\VID_0C2E*PID_*F *HID\VID_0C2E*PID_*1 *HID\VID_0536* *HID\VID_067E*
!DEVMAN-REMOVEOPT! *HID\VID_23D0*PID_*7 *HID\VID_23D0*PID_*3 *HID\VID_23D0*PID_*F *HID\VID_23D0*PID_*1
!DEVMAN-REMOVEOPT! *USB\VID_0C2E*PID_*7 *USB\VID_0C2E*PID_*3 *USB\VID_0C2E*PID_*F *USB\VID_0C2E*PID_*1 *USB\VID_0536* *USB\VID_067E*
!DEVMAN-REMOVEOPT! *USB\VID_23D0*PID_*7 *USB\VID_23D0*PID_*3 *USB\VID_23D0*PID_*F *USB\VID_23D0*PID_*1

@set WINDOWS_INF_DIR=%SystemRoot%\inf
@set NULLHIDFILELIST="%USERPROFILE%\nullhidfilelist.txt"
if exist %NULLHIDFILELIST% del /F /Q %NULLHIDFILELIST%
@pushd .
@cd /d %WINDOWS_INF_DIR%
if "%POSREADY2009%"=="yes" ( goto POSR2009-2 )
@%FINDSTR% "honeywell_scanner_hid.cat" oem*.inf >> %NULLHIDFILELIST%
@%FINDSTR% "honeywell_scanner_usb.cat" oem*.inf >> %NULLHIDFILELIST%
goto POSR2009-3
:POSR2009-2
@%FINDSTR% honeywell_scanner_hid.cat oem*.inf >> %NULLHIDFILELIST%
@%FINDSTR% honeywell_scanner_usb.cat oem*.inf >> %NULLHIDFILELIST%
:POSR2009-3
@popd
@rem for now, just go to the USERPROFILE directory and figure out the size
@rem
@pushd .
@cd /d %USERPROFILE%
@rem Now, that we are here. We need to find out if each of the above files
@rem is non-zero, and only perform devcon on non-zero file
@rem figure out the size for the files ...
for /f "usebackq" %%A in ('"nullhidfilelist.txt"') do @set NULLHIDFILESIZE=%%~zA
@popd
@rem
if %NULLHIDFILESIZE% equ 0 ( goto SKIP-NULLFILE )
@pushd .
cd /d %WINDOWS_INF_DIR%
@echo deleting NULL HID  size=%NULLHIDFILESIZE%....
for /f "tokens=1 delims=:" %%A in ('@type %NULLHIDFILELIST%') do ^
!DEVCON! !DEVMAN-UNINSTALLOPT! %%A
@popd
@rem
:SKIP-NULLFILE
@rem.
@rem ...get the current driver version first
set /p DriverVer=<.\null_driver_version.txt
@pushd .
@cd /d %SYSTEMTYPE%
if "%INSTALL-OPT%"=="install" (
@echo ...Installing NULL driver
@echo ...Please wait...This could take some time ...
if exist .\honeywell_scanner_hid.inf (
@echo ...Installing honeywell_scanner_hid.inf ....
%DEVMAN-INSTALLOPT% ".\honeywell_scanner_hid.inf"
) else (
echo ... cannot find .\honeywell_scanner_hid.inf ...
set RETURNCODE=1
goto ERROR
)
if exist .\honeywell_scanner_usb.inf (
@echo ...Installing honeywell_scanner_usb.inf ....
%DEVMAN-INSTALLOPT% ".\honeywell_scanner_usb.inf"
%REG% add HKLM\SYSTEM\CurrentControlSet\Services\honeywell_null\Parameters /v NullDriverVer /t REG_SZ /d %DriverVer% /f > %NUL% 2>&1
@rem ...making sure that this call is successful before go to END
if %ERRORLEVEL%==0 goto END
@rem ... if not, jump to error
set RETURNCODE=%ERRORLEVEL%
goto ERROR
)
echo ... cannot find .\honeywell_scanner_usb.inf ...
set RETURNCODE=1
goto ERROR
) else (
if "%INSTALL-OPT%"=="uninstall" (
%REG% add HKLM\SYSTEM\CurrentControlSet\Services\honeywell_null\Parameters /v NullDriverVer /t REG_SZ /d 0 /f > %NUL% 2>&1
if %ERRORLEVEL%==0 goto END
@rem ... if not, jump to error
set RETURNCODE=%ERRORLEVEL%
goto ERROR
) else (
goto USAGE
)
)

@rem make sure that user can see the above warning message.
:DISP
@pause
goto ERROR
:USAGE
set RETURNCODE=1
@rem display usage
@echo USAGE
@echo nulldrvinstall.bat [/version ^| /install ^| /uninstall]
goto ERROR
:END
@echo nulldrvinstall completes ...
@rem ... rescan new hardware
%DEVCON% rescan
:ERROR
:VERSION
@popd
exit /B !RETURNCODE!

