@echo off
setlocal ENABLEDELAYEDEXPANSION

REM Modifiable Parameter Types
REM "DRIVER=": Specify the name of the driver to be installed. 
REM "PORT=": Specifies the port name of the installed driver. 
REM "IP=": Specify the IP address of the installed driver. 
REM "PRINTER="Specifies the name by which the printer will be displayed.
set "DRIVER="
set "IP="
set "PORT="
set "PRINTER="

REM Your Installation Directory
set "defaultDir=%ProgramFiles%"
set "defaultFolder=DC8600_Windows-Driver_V2.135.0.0"
set "destination=!defaultDir!\!defaultFolder!"

:silentInstall
REM Run Silent Install
set "curDir=%~dp0"
set "logDir=!destination!\SetupLog.txt"
set "exePath=!curDir!\DASCOM_DC8600_Windows-Driver-V2.135.0.0.exe"
!exePath! /verysilent /IP="!IP!" /PORT="!PORT!" /DRIVER="!DRIVER!" /PRINTER=!PRINTER!

REM Output .bat run start time
call :startTime

:installStatus
REM Detecting Duplicate Installations
set "markFile=!destination!\marks"
if exist !markFile! (
    set /a lineNum=0
    for /f "usebackq delims=" %%a in ("!markFile!") do (
        set /a lineNum+=1
        set "line=%%a"
        call :checkMarksStatus !lineNum! !line! 0
		if !errorlevel! equ 0 (
			REM echo 2^>Verify: Detect Already Exists, exit installation.
			echo 2^>Verify: Detect Already Exists, exit installation.  >>!logDir!
			goto :endTime
		)
    )
)


REM VerifyFileIntegrity
REM Use Hash-Algorithm-MD5 to Verify
REM echo 0^>Verify: Hash Algorithm: MD5
echo 0^>Verify: Hash Algorithm: MD5 >>!logDir!
set /a "index=1"
set /a "fileSum=18"
set /a "fileEnd=fileSum+1"
set /a "fileCorrupted=0"

REM Key-MD5
set "_2k_DSMR600SM_EXE=7fa96e8e9f6cea8f00aa564b348b47c7"
set "_2k_DSMR600CL_DLL=5c7e2003ed5145ef559d55bc1442f514"
set "_2k_DSMR600FM_DLL=0757ccd547e5db9fa6d0a8b3fe56e84b"
set "_2k_DSMR600LM_DLL=067fea3fcbc5bb14f0c04aac817ee510e"
set "_2k_DSMR600RC_DLL=90a4ea7bf52b93202047c7d580069325"
set "_2k_DSMR600UI_DLL=11730d0e1c23f02641428bd08da39343"
set "_2k_DSMR600UNI_DLL=786c365d3e4d2f9b797e2f0fe5df930e"
set "_64_DSMR600SM_EXE=6ac6d1c2650e63261ad05cef25ae4d4b"
set "_64_DSMR600CL_DLL=16299de3e9e7dac5e0f4aa42c560a966"
set "_64_DSMR600FM_DLL=e378496ee0c8f368e90f944530bb1f37"
set "_64_DSMR600LM_DLL=10d401d04a11ac257957c58bc6280bb7"
set "_64_DSMR600RC_DLL=d93714b1c790ebc8df9d6224be0978e9"
set "_64_DSMR600UI_DLL=ec398882e8f5f6d92b9c2d2a2cfa76da"
set "_64_DSMR600UNI_DLL=b0b1fdb01484aa42df32463bb39457e7"
set "_i386_DSMR600TCPPortMon_DLL=8c0894554afa1e0ae52ab5dbf29bbf1c"
set "_i386_DSMR600TCPPortMonUI_DLL=155d083dfd61297b2f8ee707818e3b0f"
set "_x64_DSMR600TCPPortMon_DLL=a8346312656d503bdf7f96df0a43f4c5"
set "_x64_DSMR600TCPPortMonUI_DLL=8c48a78a53a88735ef6d3074f9184dbe"

set "unzipPath=!destination!\Driver\Printer Driver"
set "_2kPath=2k"
set "_64Path=64"
set "_i386Path=TcpPortMon\i386"
set "_x64Path=TcpPortMon\x64"
set "DSMR600SM_EXE=DSMR600SM.exe"
set "DSMR600CL_DLL=DSMR600CL.dll"
set "DSMR600FM_DLL=DSMR600FM.dll"
set "DSMR600LM_DLL=DSMR600LM.dll"
set "DSMR600RC_DLL=DSMR600RC.dll"
set "DSMR600UI_DLL=DSMR600UI.dll"
set "DSMR600UNI_DLL=DSMR600UNI.dll"
set "DSMR600TCPPortMon_DLL=DSMR600TCPPortMon.dll"
set "DSMR600TCPPortMonUI_DLL=DSMR600TCPPortMonUI.dll"

:1
set "compareMD5=!_2k_DSMR600SM_EXE!"
set "verifyPath=!unzipPath!\!_2kPath!\!DSMR600SM_EXE!"
goto :VerifyFileIntegrity

:2
set "compareMD5=!_2k_DSMR600CL_DLL!"
set "verifyPath=!unzipPath!\!_2kPath!\!DSMR600CL_DLL!"
goto :VerifyFileIntegrity

:3
set "compareMD5=!_2k_DSMR600FM_DLL!"
set "verifyPath=!unzipPath!\!_2kPath!\!DSMR600FM_DLL!"
goto :VerifyFileIntegrity

:4
set "compareMD5=!_2k_DSMR600LM_DLL!"
set "verifyPath=!unzipPath!\!_2kPath!\!DSMR600LM_DLL!"
goto :VerifyFileIntegrity

:5
set "compareMD5=!_2k_DSMR600RC_DLL!"
set "verifyPath=!unzipPath!\!_2kPath!\!DSMR600RC_DLL!"
goto :VerifyFileIntegrity

:6
set "compareMD5=!_2k_DSMR600UI_DLL!"
set "verifyPath=!unzipPath!\!_2kPath!\!DSMR600UI_DLL!"
goto :VerifyFileIntegrity

:7
set "compareMD5=!_2k_DSMR600UNI_DLL!"
set "verifyPath=!unzipPath!\!_2kPath!\!DSMR600UNI_DLL!"
goto :VerifyFileIntegrity

:8
set "compareMD5=!_64_DSMR600SM_EXE!"
set "verifyPath=!unzipPath!\!_64Path!\!DSMR600SM_EXE!"
goto :VerifyFileIntegrity

:9
set "compareMD5=!_64_DSMR600CL_DLL!"
set "verifyPath=!unzipPath!\!_64Path!\!DSMR600CL_DLL!"
goto :VerifyFileIntegrity

:10
set "compareMD5=!_64_DSMR600FM_DLL!"
set "verifyPath=!unzipPath!\!_64Path!\!DSMR600FM_DLL!"
goto :VerifyFileIntegrity

:11
set "compareMD5=!_64_DSMR600LM_DLL!"
set "verifyPath=!unzipPath!\!_64Path!\!DSMR600LM_DLL!"
goto :VerifyFileIntegrity

:12
set "compareMD5=!_64_DSMR600RC_DLL!"
set "verifyPath=!unzipPath!\!_64Path!\!DSMR600RC_DLL!"
goto :VerifyFileIntegrity

:13
set "compareMD5=!_64_DSMR600UI_DLL!"
set "verifyPath=!unzipPath!\!_64Path!\!DSMR600UI_DLL!"
goto :VerifyFileIntegrity

:14
set "compareMD5=!_64_DSMR600UNI_DLL!"
set "verifyPath=!unzipPath!\!_64Path!\!DSMR600UNI_DLL!"
goto :VerifyFileIntegrity

:15
set "compareMD5=!_i386_DSMR600TCPPortMon_DLL!"
set "verifyPath=!unzipPath!\!_i386Path!\!DSMR600TCPPortMon_DLL!"
goto :VerifyFileIntegrity

:16
set "compareMD5=!_i386_DSMR600TCPPortMonUI_DLL!"
set "verifyPath=!unzipPath!\!_i386Path!\!DSMR600TCPPortMonUI_DLL!"
goto :VerifyFileIntegrity

:17
set "compareMD5=!_x64_DSMR600TCPPortMon_DLL!"
set "verifyPath=!unzipPath!\!_x64Path!\!DSMR600TCPPortMon_DLL!"
goto :VerifyFileIntegrity

:18
set "compareMD5=!_x64_DSMR600TCPPortMonUI_DLL!"
set "verifyPath=!unzipPath!\!_x64Path!\!DSMR600TCPPortMonUI_DLL!"
goto :VerifyFileIntegrity

:VerifyFileIntegrity
REM echo 0^>Verify: File: !verifyPath!
echo 0^>Verify: File: !verifyPath! >> !logDir!
for /f "delims=" %%a in ('
    certutil -hashfile "!verifyPath!" MD5 ^| findstr /r "[0-9a-f]" ^| findstr /v "CertUtil"
') do (
    set "ret=%%a"
)
if not !ret!==!compareMD5! (
	set /a "fileCorrupted+=1"
    REM echo 2^>Verify - Result: [Corrupted], File -^> !verifyPath!
    echo 2^>Verify - Result: [Corrupted], File -^> !verifyPath! -^> !ret! >>  !logDir!
) 
set /a "index+=1"
if !index! equ !fileEnd! (
	goto :VerifyEnd
) else (
	goto :!index!
)

:VerifyEnd
set "dataTime=%time%"
if !fileCorrupted! equ 0 (
REM echo 0^>Verify: Corrupted file count: !fileCorrupted!
echo 0^>Verify: Corrupted file count: !fileCorrupted! >>!logDir!
) else (
REM echo 2^>Verify: Corrupted file count: !fileCorrupted!
echo 2^>Verify: Corrupted file count: !fileCorrupted! >>!logDir!
)
goto :endTime

:checkMarksStatus
setlocal
set lineNum=%1
set line=%2
set num=%3
set /a "groupNum=num/32 + 1"
if !groupNum! neq !lineNum! (
    endlocal & exit /b 1
)
set /a "bitMask=1<<(num%%32)"
set "hexValue=!line:0x=!"
set /a decValue=0x!hexValue!
set /a "result=decValue & bitMask"
if !result! neq 0 (
    endlocal & exit /b 0
) else (
    endlocal & exit /b 1
)

:startTime
set "dateTime=%time%"
REM echo 0^>Verify: Start time: !dateTime!
echo 0^>Verify: Start time: !dateTime! >>!logDir!
goto :eof

:endTime
set "dateTime=%time%"
REM echo 0^>Verify: Finish time: !dateTime!
echo 0^>Verify: Finish time: !dateTime! >>!logDir!
echo. >> !logDir!
goto :eof

