@echo off
chcp 437
:::Auto require administrator to avoid special path limitation
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

if '%errorlevel%' NEQ '0' (

    echo Applying run as administrator

    goto UACPrompt

) else ( goto gotAdmin )

:UACPrompt

    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"

    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"

    exit /B

:gotAdmin

    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )

    pushd "%CD%"

    CD /D "%~dp0"
echo got admin authoright

setlocal enabledelayedexpansion
set batPath=%~dp0%

set logFilePath=C:\ProgramData\RW101
md %logFilePath%

if exist %logFilePath%\Install_log.log (
	del %logFilePath%\Install_log.log 1>>nul 2>>nul
)

echo ############check driver is installed before or not ############
echo %date% %time%:"-------------------------------------------log start-----------------------------">> %logFilePath%\Install_log.log
echo %date% %time%:"---------------Check whether the modem exists before installation ---------------">> %logFilePath%\Install_log.log
cd %~dp0%
if exist %batPath%device_exist.log (
	del %batPath%device_exist.log 1>>nul 2>>nul
)
if exist %batPath%driver_temp.log (
	del %batPath%driver_temp.log 1>>nul 2>>nul
)
if exist %batPath%device_status.log (
	del %batPath%device_status.log 1>>nul 2>>nul
)

pnputil /enum-devices /connected | find /C "USB\VID_33F8&PID_1001" >> %batPath%device_exist.log
pnputil /enum-devices /connected | find /C "USB\VID_33F8&PID_01A4" >> %batPath%device_exist.log
for /f %%i in (%batPath%device_exist.log) do (
	set ExistDevice=%%i

	if "!ExistDevice!" NEQ "0" (
		goto break
	)
)

:CheckRecovery
del %batPath%device_exist.log 1>>nul 2>>nul
pnputil /enum-devices /connected | find /C "USB\VID_05C6&PID_9008" >> %batPath%device_exist.log
for /f %%i in (%batPath%device_exist.log) do (
	set NeedRecovery=%%i
	if "!NeedRecovery!" NEQ "0" (
		echo "Modem NOT Exist , need do recovery after install driver !!"
		goto break
	)
)

echo "Modem NOT Exist , not install drivers,the program will stop executing !!"
echo %date% %time%:   Modem NOT Exist , not install drivers,The program will stop executing !! >> %logFilePath%\Install_log.log
	timeout /t 10
exit

:break
echo Modem Exist: !ExistDevice!  >> %logFilePath%\Install_log.log
if "!ExistDevice!" NEQ "0" (
	echo "Modem Device Exist, check whether uninstall !!"
	echo %date% %time%:   Modem Device Exist, check whether uninstall !! >> %logFilePath%\Install_log.log
	call "UninstallTool.exe" -silent -101
        timeout /t 2 >> nul
	echo "Drivers have been uninstalled completely !!"
	echo %date% %time%:   Drivers have been uninstalled completely !! >> %logFilePath%\Install_log.log
	set /a Installmethod=0
) else (	
	echo "Modem Device NOT Exist , install drivers at once !!"
	echo %date% %time%:   Modem Device NOT Exist , install drivers at once !! >> %logFilePath%\Install_log.log
	set /a Installmethod=1
)

if exist %batPath%device_exist.log (
	del %batPath%device_exist.log 1>>nul 2>>nul
)

::GOTO :EOF
md "C:\Windows\Wwan"
echo ############ Install Modem Driver ############

:InstallrwserDriver
if exist "%~dp0FwUpdateDriver"\rwser.inf (
    pnputil.exe /add-driver "%~dp0FwUpdateDriver"\rwser.inf /install > driver_temp.log
) else (
    pnputil.exe /add-driver "%~dp0RwSer"\rwser.inf /install > driver_temp.log
)
if %ERRORLEVEL% EQU 0 (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	echo [STEP  PASS] Install driver [rwser] successful !!
	echo. >> %logFilePath%\Install_log.log
	echo "[STEP  PASS] Install driver [rwser] successful !!"
	echo %date% %time%:   [STEP  PASS] Install driver [rwser] successful !! >> %logFilePath%\Install_log.log
) else (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	if !DriverResult! NEQ 0 (
		echo [STEP  PASS] Install driver [rwser] successful !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  PASS] Install driver [rwser] successful !!"
		echo %date% %time%:   [STEP  PASS] Install driver [rwser] successful !! >> %logFilePath%\Install_log.log
	) else (
		echo [STEP  FAIL] Install driver [rwser] failed !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  FAIL] Install driver [rwser] failed !!"
		echo %date% %time%:   [STEP  FAIL] Install driver [rwser] failed !! >> %logFilePath%\Install_log.log
		set /a ERRORFLAG=1
	)
)
echo. >> %logFilePath%\Install_log.log
pnputil /scan-devices

timeout /t 2 >> nul

pnputil /enum-devices /connected | find /C "USB\VID_33F8&PID_1001" >> %batPath%device_exist.log
pnputil /enum-devices /connected | find /C "USB\VID_05C6&PID_9008" >> %batPath%device_exist.log
for /f %%i in (%batPath%device_exist.log) do (
	set ExistDevice=%%i

	if "!ExistDevice!" NEQ "0" (		
		GOTO :CheckDeviceAgain
	)
)

GOTO :EOF

:CheckDeviceAgain
if exist %batPath%device_exist.log (
	del %batPath%device_exist.log 1>>nul 2>>nul
)

:CheckWwanResetDriver
if exist "%~dp0WwanReset"\WwanReset.inf (
	GOTO :InstallWwanResetDriver
) else (
	GOTO :CheckrwgnssDriver
)

:InstallWwanResetDriver
pnputil.exe /add-driver "%~dp0WwanReset"\WwanReset.inf /install > driver_temp.log
if %ERRORLEVEL% EQU 0 (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	echo [STEP  PASS] Install driver [WwanReset] successful !!
	echo. >> %logFilePath%\Install_log.log
	echo "[STEP  PASS] Install driver [WwanReset] successful !!"
	echo %date% %time%:  [STEP  PASS] Install driver [WwanReset] successful !! >> %logFilePath%\Install_log.log
) else (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	if !DriverResult! NEQ 0 (
		echo [STEP  PASS] Install driver [WwanReset] successful !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  PASS] Install driver [WwanReset] successful !!"
		echo %date% %time%:  [STEP  PASS] Install driver [WwanReset] successful !! >> %logFilePath%\Install_log.log
	) else (
		echo [STEP  FAIL] Install driver [WwanReset] failed !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  FAIL] Install driver [WwanReset] failed !!"
		echo %date% %time%:  [STEP  FAIL] Install driver [WwanReset] failed !! >> %logFilePath%\Install_log.log
		set /a ERRORFLAG=1
	)
)
echo. >> %logFilePath%\Install_log.log
pnputil /scan-devices
timeout /t 2 >> nul

:CheckrwgnssDriver
if exist "%~dp0RwGNSS"\rwgnss.inf (
	GOTO :InstallrwgnssDriver
) else (
	GOTO :CheckRwWwanConfigDriver
)
:InstallrwgnssDriver
pnputil.exe /add-driver "%~dp0RwGNSS"\rwgnss.inf /install > driver_temp.log
if %ERRORLEVEL% EQU 0 (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	echo [STEP  PASS] Install driver [rwgnss] successful !!
	echo. >> %logFilePath%\Install_log.log
	echo "[STEP  PASS] Install driver [rwgnss] successful !!"
	echo %date% %time%:  [STEP  PASS] Install driver [rwgnss] successful !! >> %logFilePath%\Install_log.log
) else (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	if !DriverResult! NEQ 0 (
		echo [STEP  PASS] Install driver [rwgnss] successful !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  PASS] Install driver [rwgnss] successful !!"
		echo %date% %time%:  [STEP  PASS] Install driver [rwgnss] successful !! >> %logFilePath%\Install_log.log
	) else (
		echo [STEP  FAIL] Install driver [rwgnss] failed !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  FAIL] Install driver [rwgnss] failed !!"
		echo %date% %time%:  [STEP  FAIL] Install driver [rwgnss] failed !! >> %logFilePath%\Install_log.log
		set /a ERRORFLAG=1
	)
)
echo. >> %logFilePath%\Install_log.log
pnputil /scan-devices
timeout /t 2 >> nul

:CheckRwWwanConfigDriver
if exist "%~dp0RwWwanConfigDriver"\rwwwanConfig.inf (
	GOTO :InstallRwWwanConfigDriver
) else (
	GOTO :CheckFwUpdateDriver
)

:InstallRwWwanConfigDriver
pnputil.exe /add-driver "%~dp0RwWwanConfigDriver"\rwwwanConfig.inf /install > driver_temp.log
if %ERRORLEVEL% EQU 0 (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	echo [STEP  PASS] Install driver [RwWwanConfigDriver] successful !!
	echo. >> %logFilePath%\Install_log.log
	echo "[STEP  PASS] Install driver [RwWwanConfigDriver] successful !!"
	echo %date% %time%:  [STEP  PASS] Install driver [RwWwanConfigDriver] successful !! >> %logFilePath%\Install_log.log
) else (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	if !DriverResult! NEQ 0 (
		echo [STEP  PASS] Install driver [RwWwanConfigDriver] successful !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  PASS] Install driver [RwWwanConfigDriver] successful !!"
		echo %date% %time%:  [STEP  PASS] Install driver [RwWwanConfigDriver] successful !! >> %logFilePath%\Install_log.log
	) else (
		::echo [STEP  FAIL] Install driver [RwWwanConfigDriver] failed !!
		echo. >> %logFilePath%\Install_log.log
		::echo "[STEP  FAIL] Install driver [RwWwanConfigDriver] failed !!"
		echo %date% %time%:  [STEP  FAIL] Install driver [RwWwanConfigDriver] failed !! >> %logFilePath%\Install_log.log
		set /a ERRORFLAG=1
	)
)
echo. >> %logFilePath%\Install_log.log
pnputil /scan-devices
timeout /t 2 >> nul

:CheckFwUpdateDriver
if exist "%~dp0FwUpdateDriver"\FwUpdateDriver.inf (
	GOTO :InstallFwUpdateDriver
) else (
	GOTO :CopyVersionDLL
)

:InstallFwUpdateDriver
::New requirement requires support for configuring the FwUpdating Option parameter in ini
set key="FwUpdateOption"
cd /d %~dp0
for /f "tokens=1,2 delims==" %%i in (installerSettings.ini) do (
    if "%%i"==%key% set value=%%j)
echo %value%
if "%value%" EQU "NoFwUpdate" (
    goto CopyVersionDLL
)
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Rolling\FwUpdateDriver"  /f /v FwUpdateOption /t REG_SZ /d %value%
pnputil.exe /add-driver "%~dp0FwUpdateDriver"\FwUpdateDriver.inf /install > driver_temp.log
if %ERRORLEVEL% EQU 0 (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	echo [STEP  PASS] Install driver [FwUpdateDriver] successful !!
	echo. >> %logFilePath%\Install_log.log
	echo "[STEP  PASS] Install driver [FwUpdateDriver] successful !!"
	echo %date% %time%:   [STEP  PASS] Install driver [FwUpdateDriver] successful !! >> %logFilePath%\Install_log.log
) else (
	for /f "tokens=*" %%a in ('find /C "up-to-date" ^< "driver_temp.log"') do set DriverResult=%%a
	type driver_temp.log >> %logFilePath%\Install_log.log
	echo "Count the string "up-to-date" [!DriverResult!] !!"
	del driver_temp.log 1>>nul 2>>nul
	if !DriverResult! NEQ 0 (
		echo [STEP  PASS] Install driver [FwUpdateDriver] successful !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  PASS] Install driver [FwUpdateDriver] successful !!"
		echo %date% %time%:   [STEP  PASS] Install driver [FwUpdateDriver] successful !! >> %logFilePath%\Install_log.log
	) else (
		echo [STEP  FAIL] Install driver [FwUpdateDriver] failed !!
		echo. >> %logFilePath%\Install_log.log
		echo "[STEP  FAIL] Install driver [FwUpdateDriver] failed !!"
		echo %date% %time%:   [STEP  FAIL] Install driver [FwUpdateDriver] failed !! >> %logFilePath%\Install_log.log
		set /a ERRORFLAG=1
	)
)
echo. >> %logFilePath%\Install_log.log
pnputil /scan-devices
timeout /t 2 >> nul

if "!NeedRecovery!" EQU "1" (
	goto FwRecovery
) else (
	goto CopyVersionDLL
)

:FwRecovery
::Copy the exe and FLZ to dest path
md C:\Windows\Wwan\Firmware\
copy "%~dp0FwUpdateDriver"\FwPackage.flz C:\Windows\Wwan\Firmware\
echo %date% %time%:   Copy the FwPackage.flz to C:\Windows\Wwan\Firmware!! >> %logFilePath%\Install_log.log
md C:\Windows\Wwan\FwSwitchbin\
copy "%~dp0FwUpdateDriver"\FwUpdate.ini C:\Windows\Wwan\FwSwitchbin\
echo %date% %time%:   Copy the FwUpdate.ini to C:\Windows\Wwan\Firmware!! >> %logFilePath%\Install_log.log
copy "%~dp0FwUpdateDriver"\FwSwitchService.exe C:\Windows\Wwan\FwSwitchbin\
echo %date% %time%:   Copy the FwSwitchService.exe to C:\Windows\Wwan\Firmware!! >> %logFilePath%\Install_log.log
::Add the registry Key
REG ADD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Rolling\FwUpdateDriver /v NewPackageExt /t REG_DWORD /d 1 /f
REG ADD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Rolling\FwUpdateDriver /v SubsysID /t REG_SZ /d "33F81001" /f

::Start FwSwitchService
sc create "Rolling WWAN Flash Service" binpath="C:\Windows\Wwan\FwSwitchbin\FwSwitchService.exe" start=auto
sc start "Rolling WWAN Flash Service"

:CopyVersionDLL
del %batPath%device_status.log 1>>nul 2>>nul
echo "Driver Install Successfully!"
echo "If firmware upgrade is updating, please waiting for finish and restart system.Othewise,please restart your system after the installation successfully!"
echo %date% %time%:  If firmware upgrade is updating, please waiting for finish and restart system.Othewise,please restart your system after the installation successfully! !! >> %logFilePath%\Install_log.log
timeout /t 10

GOTO :EOF

::pause